0.0.3 • Published 1 year ago

@dotchat/bus v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

About The Project

Bus is an event system to handle subjects. You can subscribe and call subjects.

Getting Started

This is how you can getting started and install Dotchat bus. You can install this package from npm or yarn.

npm i @dotchat/bus

or

yarn add @dotchat/bus

Usage

After you installed the package you can use some available functions but first let import the package.

const { bus } = require("@dotchat/bus");

or in typescript or ecmascript

import { bus } from "@dotchat/bus":

Subscribe

You can subscribe to a subject.

bus.subscribe({
  subject: "#/parrent/child",
  callback: {
    start: () => {
      //
    },
    next: (sub) => {
      //
    },
    error: (sub) => {
      //
    },
    complete: () => {
      //
    },
  },
});

And maybe you need to unsubscribe it.

const sub = bus.subscribe({
  // ....
});

sub.unsubscribe();

Call

You can call a subject with any params and types (optional).

bus.call({
  subject: "#/parrent/child",
  param: "ANY THINGS WITH ANY TYPES",
});

You can call subject with three types but default is next.

bus.call({
  subject: "#/parrent/child",
  param: "ANY THINGS WITH ANY TYPES",
  type: "complete",
});

Callback

You can call a subject and get back data from subscribers.

bus.subscribe({
  subject: "message",
  callback: {
    next: (sub) => {
      console.log(sub.data); // log: hello
      sub.next("hi");
    },
  },
});
bus
  .call({
    subject: "message",
    param: "hello",
  })
  .subscribe({
    next: (sub) => {
      console.log(sub.data); // log: hi
    },
  });

useBus

You can subscribe to many subjects.

useBus({
  "#/country/city": (sub) => {
    //
  },
});

Definations

Maybe the understanding and concept and words are complicated. Let's explain some.

Subject

Subjects are like function name. And you can call them with this name.

Types

There is 4 types of subscription but you can access with three of theme.

start

When subscribe called for first time with no data.

next

When subscribe called with data.

error

When subscribe called with data when an error happend.

complete

When subscribe called for last time and wants to removed.