A minimal event bus for you to implement the pub/sub pattern with.
I have made it a class so you can create a new bus per use case.
- Zero dependencies
- Simple & easy to use
- Feature complete
const salesEventBus = new TsBus();
function handler(eventData) {
console.log("Received event: ", eventData);
}
Register event handlers with .on
. You can register multiple handlers for a specific event.
You can register a handler with multiple events.
salesEventBus.on("MY_EVENT", handler);
Emit events with .emit
. Each handler function registered with the event name is called.
// Emit the event (as many times as you want). `handler` is called each time the event is emitted.
salesEventBus.emit("MY_EVENT");
salesEventBus.emit("MY_EVENT", "any data"); // Add a second argument to send data (any type)
De-register event handlers with .off
. Handlers that are currently running will not be terminated.
salesEventBus.off("MY_EVENT", handler);
// Handler will *not* be called again
salesEventBus.emit("MY_EVENT");
Use .once
to call a handler only the first time the relevant event is emitted.
// Register the event
salesEventBus.once("MY_EVENT", handler);
salesEventBus.emit("MY_EVENT");
// Handler will *not* be called again
salesEventBus.emit("MY_EVENT");