Tiny, typed event emitter utility for Node.js and browsers. No dependencies.
Need a single event and type, only? Check out Signal instead
npm install @soncodi/ee --save
import { EE } from '@soncodi/ee';
// map event names to their callback param types
interface Events {
a: number;
b: string;
c: undefined;
}
const ee = new EE<Events>();
const cb = (num: number) => console.log('A', num);
ee.on('a', cb);
ee.on('b', str => console.log('B', str));
ee.once('c', () => console.log('C'));
ee.emit('a', 123);
ee.emit('b', 'hello');
ee.emit('c');
ee.off('a', cb);
ee.event('b', 123); // error
ee.event('d', 123); // error
Attaches an event handler to be called whenever the event fires.
Attaches a one-time handler which is unbound after it fires the first time.
Detaches one instance of a given handler from the event emitter. If no handler is provided, detaches all handlers.
Fires the event synchronously, triggering any attached handlers with the given arg
.
Fires the event asynchronously, triggering any attached handlers with the given arg
. Useful when attaching handlers later in the same event loop turn.