ontime-eventemitter
v1.0.0
Published
EventEmitter to implement mediator pattern
Downloads
4
Readme
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.
npm install ontime-eventemitter
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);
on(name: name, callback: Function) - Subscribe callback. The callback will be invoked whenever the event is fired.
once(name: name, callback: Function) - Just like "on", but causes the callback to fire only once before being removed.
off(name: name, callback: Function) - Unsubscribe previously callback.
emit(name: name, [...args]) - Emit callbacks for the given event. Subsequent arguments to trigger will be passed along to the event callbacks.
stopListening() - Remove all listeners.