Permalink
Browse files

New: `listeners` method

  • Loading branch information...
jacoborus committed Jun 4, 2016
1 parent 249ad03 commit f6f1843e9ec674b3e8a121b071efe8b413d616c0
Showing with 48 additions and 1 deletion.
  1. +16 −0 README.md
  2. +7 −0 arbitrary-emitter.js
  3. +1 −1 package.json
  4. +24 −0 tests/tests.js
View
@@ -86,6 +86,22 @@ emitter.off() // will remove all the listeners from all the eventKeys and the ev
<a name="emitter-listeners-api"></a>
### listeners(eventKey)
Returns a copy of the array of listeners for the event tagged `eventKey`
```js
const key = {}
const f1 = () => console.log('f1')
const f2 = () => console.log('f2')
emitter.on(key, f1)
emitter.on(key, f2)
emitter.listeners(key)[0] === f1 // true
emitter.listeners(key)[1] === f2 // true
```
<a name="testing"></a>
## Testing
View
@@ -89,7 +89,14 @@ function arbitrary () {
} else if (events.has(key)) {
events.get(key).rm(lis)
}
},
listeners (key) {
const e = events.get(key)
if (!e) return []
else return e.listeners.slice(0).reverse()
}
}
}
View
@@ -1,6 +1,6 @@
{
"name": "arbitrary-emitter",
"version": "0.10.2",
"version": "0.11.0",
"description": "Event emitter with ES6 Map sugar for modern browsers and node.js",
"main": "arbitrary-emitter.js",
"scripts": {
View
@@ -143,3 +143,27 @@ test('remove listener in a event with muliple listeners', t => {
t.is(out[3], 2)
t.end()
})
test('get listeners', t => {
const emitter = ae()
const key = {}
const key2 = {}
let c1 = 0
let c2 = 0
const f1 = () => ++c1
const f2 = () => ++c2
emitter.on(key, f1)
emitter.on(key, f2)
emitter.on(key2, f2)
emitter.emit(key)
t.is(c1, 1)
t.is(c2, 1)
const list = emitter.listeners(key)
t.is(list[0], f1)
t.is(list[1], f2)
const list2 = emitter.listeners(key2)
t.is(list2[0], f2)
const other = emitter.listeners({})
t.is(other.length, 0)
t.end()
})

0 comments on commit f6f1843

Please sign in to comment.