@peledni/event-bus
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

npm version

@peledni/event-bus

A typescript angular library that lets you emit and subscribe to events from anywhere in your code base.

To install

npm i @peledni/event-bus

Usage

This library provides an angular service called "EventBusService". Once injected into your service or component, the library provides 3 methods:

  • on(eventName: string, callbackFunction: (eventData: T) => void): number - subscribes to an event and will call the provided callbackFunction once an event of that name has been emitted. Returns a subscriptionEventId to be used later with unsubscribe
  • off(eventSubscriptionId: number): boolean - Unsubscribes from an event according to the specified subscriptionEventId. Returns a boolean if unsubscribe was successful.
  • emit(eventName: string, eventData?: T) - Emits an event, will call subscribers of that event.

Example

Assuming EventBusService was injected and saved privately as _eventBusService

let eventSubscriptionId = this._eventBusService.on('someEvent', function (eventData) {
  console.log(`Received event data: ${eventData}`);
});

this._eventBusService.emit('someEvent', 'hello world');
let unsubscribeSuccessful = this._eventBusService.off(eventSubscriptionId);
console.log(`Unsubscribed? ${unsubscribeSuccessful}`);

Readme

Keywords

none

Package Sidebar

Install

npm i @peledni/event-bus

Weekly Downloads

0

Version

1.0.6

License

none

Unpacked Size

66.3 kB

Total Files

20

Last publish

Collaborators

  • peledni