Skip to content

Smallest possible event emitter with full types support

Notifications You must be signed in to change notification settings

pallad-ts/micro-events

Repository files navigation

micro-events ❄️

Minimal, type friendly event emitter


CircleCI npm version Coverage Status License: MIT

Example code

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

About

Smallest possible event emitter with full types support

Resources

Stars

Watchers

Forks

Packages

No packages published