@pallad/micro-events
v1.1.0
Published
Minimal, type friendly event emitter
Downloads
28
Readme
Minimal, type friend event emitter to prevent using events without correct types
Community
Join our discord server
Installation
npm install @pallad/micro-events
Usage
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
Registering listener
events.on('progress', (progress) => {
console.log('progress', progress);
});
events.on('progress', () => {
// consuming argument is not needed
});
Emitting event
events.emit('progress', 50); // notifies all registered `progress` listeners
Removing listener
const listener = () => {
console.log('progress');
}
events.on('progress', listener);
events.off('progress', listener);
events.emit('progress', 100);
// No listeners called