Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vitalets/chnl
JavaScript event channels compatible with Chrome extensions API
https://github.com/vitalets/chnl
event-emitter eventbus eventemitter events listener pubsub subscription
Last synced: 2 days ago
JSON representation
JavaScript event channels compatible with Chrome extensions API
- Host: GitHub
- URL: https://github.com/vitalets/chnl
- Owner: vitalets
- Created: 2016-05-10T15:37:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-16T10:03:10.000Z (over 4 years ago)
- Last Synced: 2024-10-30T04:18:07.550Z (5 days ago)
- Topics: event-emitter, eventbus, eventemitter, events, listener, pubsub, subscription
- Language: JavaScript
- Homepage: https://vitalets.github.io/chnl
- Size: 1.65 MB
- Stars: 19
- Watchers: 5
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-browser-extensions-and-apps - chnl - implementation of events compatible with [Chrome Extensions Events API](https://developer.chrome.com/extensions/events#type-Event). (Tools / Development)
README
# chnl
[![Actions Status](https://github.com/vitalets/chnl/workflows/autotests/badge.svg)](https://github.com/vitalets/chnl/actions)
[![npm version](https://badge.fury.io/js/chnl.svg)](https://badge.fury.io/js/chnl)
[![license](https://img.shields.io/npm/l/chnl.svg)](https://www.npmjs.com/package/chnl)Implementation of event channels (aka pub/sub, dispatcher, emitter) inspired and
compatible with [Chrome extensions Events API](https://developer.chrome.com/extensions/events#type-Event).## Install
```
npm i chnl
```## Docs
https://vitalets.github.io/chnl## Usage
**foo.js**
```js
import Channel from 'chnl';// create channel
export const onData = new Channel();// subscribe to channel
onData.addListener(data => console.log(data));
```**bar.js**
```js
import {onData} from './foo';// dispatch event to channel
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:
```js
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();
// 1
// 2
onData.dispatch();
// 1
// 2
// 3
```## License
MIT @ [Vitaliy Potapov](https://github.com/vitalets)