arbitrary-emitter
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -51,6 +51,11 @@ 'use strict' | ||
off (key) { | ||
links.delete(key) | ||
off (key, action) { | ||
if (1 in arguments) { | ||
let link = links.get(key) | ||
if (link) link.delete(action) | ||
} else { | ||
links.delete(key) | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "arbitrary-emitter", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Event emitter with Map/Set sugar", | ||
@@ -5,0 +5,0 @@ "main": "arbitrary-emitter.js", |
@@ -47,3 +47,3 @@ arbitrary-emitter | ||
<a name="emitter-addonce-api"></a> | ||
### addOnce(key, method) | ||
### once(key, method) | ||
@@ -55,3 +55,3 @@ Add a listener for `key` which will trigger `method` function just one time, then listener will be removed. | ||
const obj = {} | ||
emitter.addOnce(obj, () => doSomethingOnce()) | ||
emitter.once(obj, () => doSomethingOnce()) | ||
emitter.emit(obj) // will `doSomething` | ||
@@ -76,8 +76,9 @@ emitter.emit(obj) // won't do anything | ||
<a name="emitter-off-api"></a> | ||
## emitter.off(key) | ||
## emitter.off(key[, action]) | ||
Remove all listeners binded to `key` | ||
Remove `action` from listener `key`. If no action is specified will remove all listeners binded to `key` | ||
```js | ||
emitter.off(obj) | ||
emitter.off(key, action) // will remove action from listener `key` | ||
emitter.off(key) // will remove the listener `key` and all actions binded to it | ||
``` | ||
@@ -84,0 +85,0 @@ |
22
test.js
@@ -33,3 +33,3 @@ 'use strict' | ||
test('off', t => { | ||
test('off listener', t => { | ||
const emitter = ae() | ||
@@ -48,2 +48,22 @@ const obj = {} | ||
test('off action', t => { | ||
const emitter = ae() | ||
const obj = {} | ||
let control = { | ||
a: 0, | ||
b: 0 | ||
} | ||
const fn = () => ++control.b | ||
emitter.on(obj, () => ++control.a) | ||
emitter.on(obj, fn) | ||
emitter.emit(obj) | ||
t.is(control.a, 1, 'control a') | ||
t.is(control.b, 1, 'control b') | ||
emitter.off(obj, fn) | ||
emitter.emit(obj) | ||
t.is(control.a, 2, 'control a') | ||
t.is(control.b, 1, 'control b') | ||
t.end() | ||
}) | ||
test('emit with arguments', t => { | ||
@@ -50,0 +70,0 @@ const emitter = ae() |
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
7134
130
102