1.0.6 • Published 6 years ago

@peledni/event-bus v1.0.6

Weekly downloads
1
License
-
Repository
-
Last release
6 years ago

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