Skip to content

VeryWow/js-simple-events

Repository files navigation

js-simple-events

Yet another simple event management system

npm i -S js-simple-events

About

This is just a simple class that helps to manage events in a simple way without dependencies. It also supports TypeScript!

And it's really light - <1kb in size!

Methods

Method Params Description
emit event, payload Emit the event with the given payload.
fire event, payload Alias for emit
on event, callback Listen for the event with the given callback.
listen event, callback Alias for on
once event, callback Listen for the event once, after handling - remove the listener.
off event, callback Remove event listener(s) for the event.
remove event, callback Alias for off

Examples

// Import and initialize
import EventManager from 'js-simple-events'

const eventManager = new EventManager();


// Define handlers
const eventHandler = (payload) => console.log('Yay, events work!', payload);

eventManager.on('test', eventHandler);
eventManager.once('test', () => console.log('This will be called just once!'));

// Emit events
eventManager.emit('test', 'Hello!');
// -> Yay, events work! Hello!
// -> This will be called just once!

eventManager.emit('test', 'Hello!');
// -> Yay, events work! Hello!
// (The 'once' handler isn't fired)

Plugins