# chnl

> Implementation of event channels compatible with Chrome extensions events API

Latest version **1.2.0** (published 2020-05-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install chnl
pnpm add chnl
yarn add chnl
bun add chnl
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2020-05-06 |
| First published | 2016-05-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 18 |
| Author | Vitaliy Potapov |
| Maintainers | vitalets |

## Links

- npm: https://www.npmjs.com/package/chnl
- Repository: https://github.com/vitalets/chnl
- Homepage: https://github.com/vitalets/chnl#readme
- Issues: https://github.com/vitalets/chnl/issues
- npm.io page: https://npm.io/package/chnl

## Recent versions

- 1.2.0 (latest) — 2020-05-06
- 1.1.1 — 2020-03-30
- 1.1.0 — 2020-01-09
- 1.0.0 — 2019-11-21
- 0.6.1 — 2019-11-21
- 0.6.0 — 2019-11-21
- 0.5.0 — 2019-06-13
- 0.4.2 — 2018-11-30
- 0.4.1 — 2018-09-14
- 0.4.0 — 2017-07-11
- 0.3.0 — 2017-07-06
- 0.2.5 — 2016-10-05
- 0.2.4 — 2016-10-03
- 0.2.3 — 2016-09-12
- 0.2.2 — 2016-09-12
- … 17 more at https://npm.io/package/chnl/versions

## README

# chnl
[![Build Status](https://travis-ci.org/vitalets/chnl.svg?branch=master)](https://travis-ci.org/vitalets/chnl)
[![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 (pub/sub, dispatcher, emitter) inspired and
compatible with [Chrome extensions events API](https://developer.chrome.com/extensions/events#type-Event).

## Install
```
npm i chnl --save
```

## 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)

---
_Source: https://npm.io/package/chnl · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
