Skip to content

🐝 Small events library for JavaScript

License

Notifications You must be signed in to change notification settings

railsmob/events

Repository files navigation

Events

downloads version license coverage PRs Welcome

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

Table of Contents

Install

yarn add @railsmob/events

Add Listener

const log = info => console.log('Logger: ', info);
events.on('log', log);
events.emit('log', 'hello world');
// Logger: hello world

Remove Listener

events.off('log', log); // unbind specific listener
events.off('log'); // unbin all listeners for 'log' event

Emit Event

events.emit('log', 123); // a second argument is optional

Once

events.once('log', () => console.log('Log once'));

LICENSE

MIT

About

🐝 Small events library for JavaScript

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published