Tiny typed EventEmitter for TypeScript.
Using yarn
yarn add pietile-eventemitter
or using npm
npm install -S pietile-eventemitter
Subclass or incapsulate EventEmitter
specified with your events interface. Use on
and off
methods to setup event handlers and emit
to emit events.
import { EventEmitter } from 'pietile-eventemitter';
interface Events {
foo: () => void;
bar: (a: number) => void;
}
const emitter = new EventEmitter<Events>();
function onFoo() {
console.log('Foo');
}
emitter.on('foo', onFoo);
const handler = emitter.on('bar', (a: number) => {
console.log('Bar: ', a);
});
emitter.emit('foo');
emitter.emit('bar', 42);
emitter.off('foo', onFoo);
emitter.off('bar', handler);
Create new instance of EventEmitter. T
must be interface describing events - names and signatures.
Add handler
for event
.
Return handler
function. Useful for anonymous handler functions.
Remove handler
for event
Remove all handlers for all events
Emit even
with its arguments
ee-ts - Type-safe, isomorphic event emitters
Pietile EventEmitter is MIT License.