chnl
Implementation of event channels (pub/sub, dispatcher, emitter) inspired and
compatible with Chrome extensions events API.
Install
npm i chnl --save
Docs
https://vitalets.github.io/chnl
Usage
foo.js
import Channel from 'chnl';
export const onData = new Channel();
onData.addListener(data => console.log(data));
bar.js
import {onData} from './foo';
onData.dispatch({foo: 'bar'});
Adding/removing listeners in dispatching loop
Chnl makes a copy of the listeners before starting dispatching loop.
So modifying listeners list (adding/removing) in dispatching loop will affect only the next dispatch:
const onData = new Channel();
const listener1 = () => console.log(1);
const listener2 = () => {
console.log(2);
onData.addListener(listener3);
};
const listener3 = () => console.log(3);
onData.addListener(listener1);
onData.addListener(listener2);
onData.dispatch();
onData.dispatch();
License
MIT @ Vitaliy Potapov