Skip to content

Commit

Permalink
events: unwrap #once listeners in #listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
omsmith committed Apr 11, 2016
1 parent 3307c30 commit 3465da2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ EventEmitter.prototype.listeners = function listeners(type) {
if (!evlistener)
ret = [];
else if (typeof evlistener === 'function')
ret = [evlistener];
ret = [unwrapListener(evlistener)];
else
ret = arrayClone(evlistener, evlistener.length);
ret = arrayMap(evlistener, unwrapListener);
}

return ret;
Expand Down Expand Up @@ -452,6 +452,14 @@ function arrayClone(arr, i) {
return copy;
}

function arrayMap(arr, fn) {
var ret = new Array(arr.length);
for (var i = 0; i < ret.length; ++i) {
ret[i] = fn(arr[i]);
}
return ret;
}

function unwrapListener(listener) {
return listener.listener ? listener.listener : listener;
}
9 changes: 9 additions & 0 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ var e3ListenersCopy = e3.listeners('foo');
e3.on('foo', listener2);
assert.deepEqual(e3.listeners('foo'), [listener, listener2]);
assert.deepEqual(e3ListenersCopy, [listener]);

var e4 = new events.EventEmitter();
e4.once('foo', listener);
assert.deepEqual(e4.listeners('foo'), [listener]);

var e5 = new events.EventEmitter();
e5.on('foo', listener);
e5.once('foo', listener2);
assert.deepEqual(e5.listeners('foo'), [listener, listener2]);

0 comments on commit 3465da2

Please sign in to comment.