Skip to content

bencelaszlo/chiisai-event-emitter

Repository files navigation

Chiisai Event Emitter

A minimal and performant event emitter library for Node.JS.


chiisai-event-emitter

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

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)