Skip to content

alisterg/ts-bus

Repository files navigation

TsBus

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

How to use

const salesEventBus = new TsBus();

function handler(eventData) {
  console.log("Received event: ", eventData);
}

.on

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

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)

.off

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");

.once

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");

About

Zero dependency event bus for TypeScript / JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published