Minimal, type friend event emitter to prevent using events without correct types
Join our discord server
npm install @pallad/micro-events
const events = new Events<{
progress: [number]
}>();
// register listener on event
events.on('progress', (progress) => {
console.log('progress', progress);
});
events.once('progress', (progress) => {
console.log('progress once', progress);
});
events.emit('progress', 10);
events.emit('progress', 100);
// progress 10
// progress once 10
// progress 100
events.on('progress', (progress) => {
console.log('progress', progress);
});
events.on('progress', () => {
// consuming argument is not needed
});
events.emit('progress', 50); // notifies all registered `progress` listeners
const listener = () => {
console.log('progress');
}
events.on('progress', listener);
events.off('progress', listener);
events.emit('progress', 100);
// No listeners called