@ra-solutions/ts-event-bus

1.2.0 • Public • Published

Node.js CI

Typescript event bus

Simple event bus that was written in typescript

Install

npm install @ra-solutions/ts-event-bus

Usage

import { eventBus } from "@ra-solutions/ts-event-bus";

const myEventBus = eventBus();

/**
 * register a simple subscriber to the "sayHello"-event 
 */
const unsubscribeSayHello = myEventBus.subscribeEvent('sayHello', (name: string) => {
  console.log(`Hello ${name}`);
});

/**
 * fires the "sayHello"-event with the name "tom"
 */
myEventBus.fireEvent('sayHello', 'tom');

/**
 * call the unsubscribe function, returned form subscribeEvent to unsubscribe from the event
 */
unsubscribeSayHello();

You could use multiple instances of the event bus.

Recommendation

Instead of calling fireEvent with the event name directly, it is recommended to create a caller function for each event type:

function sayHello(name: string){
  myEventBus.fireEvent('sayHello', name);
}

Readme

Keywords

Package Sidebar

Install

npm i @ra-solutions/ts-event-bus

Weekly Downloads

1

Version

1.2.0

License

ISC

Unpacked Size

16.2 kB

Total Files

25

Last publish

Collaborators

  • principatra