# weak-emitter

> Contexts based event emitter on ES6 WeakMaps

Latest version **5.2.1** (published 2020-03-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install weak-emitter
pnpm add weak-emitter
yarn add weak-emitter
bun add weak-emitter
```

## 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 | 5.2.1 |
| Published | 2020-03-21 |
| First published | 2020-01-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 39 |
| Author | Jacobo Tabernero |
| Maintainers | jacoborus |
| Keywords | event emitter, map, ES6 |

## Links

- npm: https://www.npmjs.com/package/weak-emitter
- Repository: https://github.com/jacoborus/weak-emitter
- Issues: https://github.com/jacoborus/weak-emitter/issues
- npm.io page: https://npm.io/package/weak-emitter

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 5.2.1 (latest) — 2020-03-21
- 5.2.0 — 2020-02-24
- 5.1.0 — 2020-02-18
- 5.0.0 — 2020-02-17
- 4.2.0 — 2020-02-03
- 4.1.2 — 2020-02-03
- 4.1.1 — 2020-02-03
- 4.1.0 — 2020-01-28
- 3.0.0 — 2020-01-08

## README

weak-emitter
============

Contexts based event emitter on ES6 WeakMaps

[![Build Status](https://travis-ci.org/jacoborus/weak-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/weak-emitter) [![npm version](https://badge.fury.io/js/weak-emitter.svg)](https://www.npmjs.com/package/weak-emitter)


```js
const context = {}
const eventKey = 'propName'
emitter.on(context, eventKey, (...args) => console.log(args))
emitter.emit(context, eventKey, 'first', 2, true)
// ['first', 2, true]
```


## Install

```sh
npm i weak-emitter --save
# or: yarn add weak-emitter
```

## Emitter API

- [on](#emitter-on-api)
- [emit](#emitter-emit-api)
- [off](#emitter-off-api)

**Argument types:**

- `context: object`
- `eventKey: string | object`
- `handler: (...args: []) => void`

<a name="emitter-on-api"></a>
### on(context, eventKey, handler)

Create new event with `eventKey` in `context` that will trigger the `handler`.
Every handler will be added once, despite the number of times it was added to the event. Handlers are invoked in the order they were added.

Returns an event controller that has three methods:

- emit(...args): trigger handler
- off(): remove event
- tranfer(destination): moves the event to `destination` context

```js
const context = {}
const eventKey = 'propName'
const evController = emitter.on(context, eventKey, (...args) => doSomething(args))

const ctx2 = {}
evController.transfer(ctx2)
evController.off()

```


<a name="emitter-emit-api"></a>
### emit(context, eventKey[, ...args])

Invoke all handlers in `context` tagged with `key`, passing the rest of the arguments

```js
const context = {}
const eventKey = 'propName'
emitter.on(context, eventKey, (...args) => console.log(args))
emitter.emit(context, eventKey, 'first', 2, true)
// ['first', 2, true]
```


<a name="emitter-off-api"></a>
### off(context, key, handler)

Removes `handler` from the event tagged with `key` in `context`

```js
emitter.off(context, eventKey, handler)
```


<a name="testing"></a>
## Testing

```sh
npm test
```

<br><br>

---

© 2020 [Jacobo Tabernero](http://jacoborus.codes) - Released under [MIT License](https://raw.github.com/jacoborus/weak-emitter/master/LICENSE)

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