evitter
v4.0.0
Published
EventEmitter with parametrized subscriptions
Downloads
7
Maintainers
Readme
evitter
EventEmitter with parametrized subscriptions
Features
- :scissors: Zero dependencies
- :rocket: Lightweight and fast
- :package: Easy to use out of the box
- :100: 100% test coverage
Getting started
- Install with npm:
npm install --save evitter
- Import as ES module:
import {EventEmitter} from 'evitter';
or as CommonJS module:
const {EventEmitter} = require('evitter');
API
import {EventEmitter} from 'evitter';
const emitter = new EventEmitter();
const callback = () => {};
let unsubscribe = emitter.on('event1', callback); // simple subscription
unsubscribe(); // remove subscription
emitter.on('event2', {param1: 1}, callback); // subscription with parameters
emitter.emit('event1', 2); // call all 'event1' events
emitter.emit('event2', {param1: 1}, {}); // call all 'event2' events that match parameters
emitter.emit('event2', {param1: 1, params2: 4}, []); // no subscriptions there
emitter.getCallbacks('event1'); // [callback]
emitter.getCallbacks('event2', {param1: 1}); // [callback]
emitter.getCallbacks('event2', {param1: 2}); // []
emitter.getCallbacks('event2', {param2: 'some'}); // []
emitter.off(); // unsubscribe from all events