0.0.3 • Published 8 months ago
@eliasrrosa/event-emitter v0.0.3
Event Emitter
Apply the observer pattern. Add listeners and emit typed events.
Installation
npm i @eliasrrosa/event-emitter@latestUsage
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);