Ontime EventEmitter
The class is used to implement pattern Mediator. The common idea of the class has been taken from NodeJS native class EventEmitter and class BackboneJS Events.
How to install
npm install ontime-eventemitter
How to use
import { EvenEmitter } from 'ontime-eventemitter';
const emitter: EvenEmitter = new EvenEmitter();
// create callback function
const cb = (name: string): void => console.log('Hello', name);
// subscribe callback
emitter.on('say', cb);
// send event
emitter.emit('say', 'John'); // -> Hello John
emitter.emit('say', 'Helen'); // -> Hello Helen
// unsubscribe callback
emitter.off('say', cb);
Methods
on
on(name: name, callback: Function) - Subscribe callback. The callback will be invoked whenever the event is fired.
once
once(name: name, callback: Function) - Just like "on", but causes the callback to fire only once before being removed.
off
off(name: name, callback: Function) - Unsubscribe previously callback.
emit
emit(name: name, [...args]) - Emit callbacks for the given event. Subsequent arguments to trigger will be passed along to the event callbacks.
stopListening
stopListening() - Remove all listeners.