@dekkai/event-emitter
An event emitter class, supports omni-listeners and symbol events. Tested on browsers, node.js and deno.
Check out the full API Documentation
Installation
Browser/NodeJS
$ yarn add @dekkai/event-emitter
Deno
import {EventEmitter} from 'https://unpkg.com/@dekkai/event-emitter';
Usage
import {EventEmitter} from '@dekkai/event-emitter';
class MyEmitter extends EventEmitter {
}
const MyEmitter = EventEmitter.mixin(BaseClass);
class MyEmitter extends EventEmitter.mixin(BaseClass) {
}
const emitter = new MyEmitter();
const handler = (evt, num0, num1) => console.log(`Event [${evt.toString()}] result: ${num0 + num1}`);
emitter.on('add', handler);
const symbolEvent = Symbol('add-symbol');
emitter.on(symbolEvent, handler);
emitter.emit('add', 4, 6);
emitter.emit(symbolEvent, 12, 8);
emitter.off('add', handler);
emitter.off(symbolEvent, handler);
const omniHandler = evt => console.log(evt.toString());
emitter.on(MyEmitter.omniEvent, omniHandler);
emitter.emit('hello');
emitter.emit(Symbol('random symbol'));
emitter.off(MyEmitter.omniEvent, omniHandler);
Check out the full API Documentation