arbitrary-emitter
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -24,2 +24,15 @@ 'use strict' | ||
function addOnce (key, method) { | ||
const link = links.get(key) || createNewLink(key) | ||
const wrap = {} | ||
const fn = () => { | ||
method() | ||
wrap.remove() | ||
} | ||
link.add(fn) | ||
wrap.remove = () => { | ||
link.delete(fn) | ||
} | ||
} | ||
function trigger (key) { | ||
@@ -31,3 +44,3 @@ const link = links.get(key) | ||
return { add, trigger } | ||
return { add, addOnce, trigger } | ||
} |
{ | ||
"name": "arbitrary-emitter", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Event emitter with Map/Set sugar", | ||
@@ -5,0 +5,0 @@ "main": "arbitrary-emitter.js", |
arbitrary-emitter | ||
================= | ||
Event emitter with Map/Set sugar for browser and node.js apps | ||
**arbitrary-emitter** allows to use arbitrary values as keys for your events | ||
**arbitrary-emitter** is written in vanilla ES6, so maybe you want to transpile it before using it. | ||
**Project in active development, API may change** | ||
## Create a new emitter | ||
```js | ||
const emitter = arbitraryEmitter() | ||
``` | ||
## Emitter API | ||
- [emitter.add](#emitter-add-api) | ||
- [emitter.addOnce](#emitter-addonce-api) | ||
- [emitter.trigger](#emitter-trigger-api) | ||
- [Testing](#testing) | ||
<a name="emitter-add-api"></a> | ||
### add(key, method) | ||
Add a listener for `key` which will trigger `method` function. | ||
`key` can be any type of value. | ||
`add` returns unsubscribe method | ||
```js | ||
const obj = {} | ||
let unsubscribe = emitter.add(obj, () => doSomething()) | ||
emitter.trigger(obj) // will `doSomething` | ||
unsubscribe() | ||
emitter.trigger(obj) // won't do anything | ||
``` | ||
<a name="emitter-addonce-api"></a> | ||
### addOnce(key, method) | ||
Add a listener for `key` which will trigger `method` function just one time, then listener will be removed. | ||
`key` can be any type of value | ||
```js | ||
const obj = {} | ||
emitter.addOnce(obj, () => doSomethingOnce()) | ||
emitter.trigger(obj) // will `doSomething` | ||
emitter.trigger(obj) // won't do anything | ||
``` | ||
<a name="emitter-trigger-api"></a> | ||
## emitter.trigger(key) | ||
Trigger methods binded to `key` | ||
```js | ||
emitter.trigger(obj) | ||
``` | ||
<a name="testing"></a> | ||
## Testing | ||
### Node | ||
```sh | ||
npm test | ||
``` | ||
[![Build Status](https://travis-ci.org/jacoborus/arbitrary-emitter.svg?branch=master)](https://travis-ci.org/jacoborus/arbitrary-emitter) | ||
@@ -5,0 +82,0 @@ |
23
test.js
@@ -6,12 +6,27 @@ 'use strict' | ||
test('hello world', t => { | ||
test('add and trigger', t => { | ||
const emitter = ae() | ||
const obj = {} | ||
let control = 0 | ||
emitter.add(obj, () => ++control) | ||
let unsubscribe = emitter.add(obj, () => ++control) | ||
emitter.trigger(obj) | ||
t.is(control, 1) | ||
t.is(control, 1, 'trigger') | ||
emitter.trigger(obj) | ||
t.is(control, 2) | ||
t.is(control, 2, 'trigger') | ||
unsubscribe() | ||
emitter.trigger(obj) | ||
t.is(control, 2, 'unsubscribe') | ||
t.end() | ||
}) | ||
test('add once', t => { | ||
const emitter = ae() | ||
const obj = {} | ||
let control = 0 | ||
emitter.addOnce(obj, () => ++control) | ||
emitter.trigger(obj) | ||
t.is(control, 1, 'trigger once') | ||
emitter.trigger(obj) | ||
t.is(control, 1, 'automatic unsubscription') | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5132
66
88