0.0.3 • Published 8 months ago

@eliasrrosa/event-emitter v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

Event Emitter

Apply the observer pattern. Add listeners and emit typed events.

Installation

npm i @eliasrrosa/event-emitter@latest

Usage

Instantiate new EventEmitter<EventType>() with the event type. EventType will be the payload of the event. Add listeners with callbacks, emit events and remove listeners using the listenerId.

const priceChangeEventEmitter = new EventEmitter<{ newPrice: number }>();

const listenerId = priceChangeEventEmitter.addListener({
  callback: (ev) => {
    console.log("New price alert! ", ev.newPrice);
  },
});

priceChangeEventEmitter.emit({
  payload: {
    newPrice: 200,
  },
});

priceChangeEventEmitter.removeListener(listenerId);
0.0.3

8 months ago

0.0.2

8 months ago