Small events library for JavaScript
import events from '@railsmob/events';
let counter = 0;
const increment = () => counter = counter + 1;
const log = () => console.log('counter = ', counter);
events.on('increment', increment);
events.on('increment', log);
events.emit('increment');
// counter = 1
events.emit('increment');
// counter = 2
events.off('increment', increment); // unbind specific listener
events.off('increment'); // unbin all listeners
yarn add @railsmob/events
const log = info => console.log('Logger: ', info);
events.on('log', log);
events.emit('log', 'hello world');
// Logger: hello world
events.off('log', log); // unbind specific listener
events.off('log'); // unbin all listeners for 'log' event
events.emit('log', 123); // a second argument is optional
events.once('log', () => console.log('Log once'));
MIT