Skip to content

Latest commit

 

History

History
88 lines (63 loc) · 2.01 KB

README.md

File metadata and controls

88 lines (63 loc) · 2.01 KB

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