1.2.0 • Published 3 years ago

@ra-solutions/ts-event-bus v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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);
}
1.2.0

3 years ago

1.1.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago