/
listen.js
48 lines (36 loc) · 1.29 KB
/
listen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* jshint node: true */
'use strict';
var EventEmitter = require('events').EventEmitter;
/**
## cog/listen
```js
var listen = require('cog/listen');
```
### listen(target, events, capture?)
The `listen` function of cog provides a mechanism for capturing specific
events (named in the events array) and routing them through an
`EventEmitter` that is returned from the function.
While at a base level this has little apparent advantage over the using
the native `addEventListener` and `removeEventListener` methods available
in the browser, the listen function also provides a patched in `stop`
method which will decouple all event listeners from their target.
**/
module.exports = function(target, events, capture) {
var emitter = new EventEmitter();
var handlers;
// iterate through the events and capture the handler functions
handlers = (events || []).map(function(eventName) {
var handler = emitter.emit.bind(emitter, eventName);
// create the event listener
target.addEventListener(eventName, handler, capture);
// return the handler
return handler;
});
// patch in the stop method
emitter.stop = function() {
handlers.forEach(function(handler, index) {
target.removeEventListener(events[index], handler);
});
};
return emitter;
};