Lightweight zero-dependency event emitter with flexible event type matching
Installation: npm i @t8/event-patterns
Initialization:
import {EventEmitter} from '@t8/event-patterns';
let eventEmitter = new EventEmitter();
Adding a handler of a specific event type:
eventEmitter.on('task started', event => {
console.log(event);
});
Of all events matching the pattern:
eventEmitter.on(/^task\s/, event => {
console.log(event);
});
With captured parameters:
eventEmitter.on(/^(\S+)\s(?<status>.*)$/, event => {
console.log(event.params[0], event.params.status);
});
Adding a handler of all events dispatched to the eventEmitter
instance:
let listener = eventEmitter.on('*', event => {
console.log(event);
});
Dispatching an event of a specific type and properties:
eventEmitter.emit('task started', {x: 42});
Removing a previously declared listener:
listener.remove();