chiisai-event-emitter
v2.1.0
Published
A minimal event emitter.
Downloads
41
Maintainers
Readme
Chiisai Event Emitter
A minimal and performant event emitter library for Node.JS.
chiisai-event-emitter
Install
npm install chiisai-event-emitter
Usage
Subscribe to an Event
Use EventEmitter.subscribe(eventName, callback)
to subscribe to an event.
import { EventEmitter } from 'chiisai-event-emitter';
const eventEmitter = new EventEmitter();
eventEmitter.subscribe('event', () => console.log('event-handler called!'));
eventEmitter.emit('event')
// event-handler called!
Unsubscibe
EventEmitter.subscribe(eventName, callback)
method returns a function which can be called to unsubscribe the callback from the event.
const unsubscibe = eventEmitter.subscribe('event', () => console.log('event-handler called!'));
unsubscribe();
eventEmitter.emit('event')
// (nothing happened)
Delete an Event
EventEmitter.clear(eventName)
method removes an event and all callbacks subscibed to this event.
eventEmitter.subscribe('event', () => console.log('event-handler called!'));
eventEmitter.subscribe('event', () => console.log('another event-handler called!'));
eventEmitter.clear('event')
eventEmitter.emit('event')
// (nothing happened)