1.0.0 • Published 10 months ago
@typedts/typesub v1.0.0
TypeSub
TypeSub is a very basic yet strongly typed pubsub system for typescript
Running Tests
To run tests, run the following command
pnpm run test
Installation
Install typesub with npm, yarn or pnpm etc
pnpm install @typedts/typesub
Examples
Create a pubsub instance using a type
type MyEvents = {
LOADING_DONE: {
id: string;
place: string;
}
}
const pubsub = createPubsub<MyEvents>();
Now you can listen on your events safely
pubusb.listen("LOADING_DONE", (data) => {
console.log(data.id);
console.log(data.place);
});
// Registering multiple listeners</span>
pubusb.listen("LOADING_DONE", (data) => {
console.log("Second listener");
});
To publish events
pubsub.publish("LOADING_DONE", {
id: "1",
place: "base.ts",
});
Unsubscribing
Unsubscribe to events that you no more want or dont want to listen to
const unsub = pubsub.listen("LOADING_DONE", (data) => ...);
unsub();