Skip to content

dotchat/bus

Repository files navigation


Dotchat bus


You can subscribe to subjects or call subjects to bus event system in Dotchat

Table of contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Definations

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":

(back to top)

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) => {
    //
  },
});

(back to top)

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.

(back to top)

Releases

No releases published

Packages

No packages published