Emitten
Emitten
is a very basic event emitter for TypeScript.
This simple class
allows you to subscribe/unsubscribe to a defined library of events.
Get started
Add Emitten
to your project.
npm install emitten
yarn add emitten
Import and start emit’in!
import {Emitten} from 'emitten';
type EventMap = {
change: (value: string) => void;
count: (value?: number) => void;
collect: (...values: boolean[]) => void;
other: (required: string, ...optional: string[]) => void;
};
const myEmitter = new Emitten<EventMap>();
const handleChange: EventMap['change'] = (value) => {};
myEmitter.on('change', handleChange);
myEmitter.emit('change', 'Hello world');
myEmitter.off('change', handleChange);
myEmitter.once('count', (value = 0) => {});
myEmitter.emit('count', 123);
const dispose = myEmitter.on('collect', (...values) => {});
myEmitter.emit('collect', true, false, !true, !false);
dispose();
const currentEvents = myEmitter.activeEvents;
myEmitter.empty();
For more guidance, please take a look at the Examples document
.