Table of contents
Bus is an event system to handle subjects. You can subscribe and call subjects.
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
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":
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();
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",
});
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
},
});
You can subscribe to many subjects.
useBus({
"#/country/city": (sub) => {
//
},
});
Maybe the understanding and concept and words are complicated. Let's explain some.
Subjects are like function name. And you can call them with this name.
There is 4 types of subscription but you can access with three of theme.
When subscribe called for first time with no data.
When subscribe called with data.
When subscribe called with data when an error happend.
When subscribe called for last time and wants to removed.