Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: unwrap #once listeners in #listeners #6881

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,9 @@ EventEmitter.prototype.listeners = function listeners(type) {
if (!evlistener)
ret = [];
else if (typeof evlistener === 'function')
ret = [evlistener];
ret = [evlistener.listener || evlistener];
else
ret = arrayClone(evlistener, evlistener.length);
ret = unwrapListeners(evlistener);
Copy link
Contributor

@yorkie yorkie Aug 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the arrayClone function and do replace all arrayClone() to unwrapListeners()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the other usages of arrayClone expect the wrapped listeners.

Copy link
Contributor

@yorkie yorkie Aug 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However I think the only difference between is that arrayClone supports a copy with a specified length, 2nd argument and the assignment from .listener of elements.

The function upwrapListeners seems to be only used inside to EE.prototype.listeners, so could we declare this function inside the prototype method to be unwrap? Both the functions defined at bottom looks like those pure collection ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect declaring unwrapListeners within listeners (I believe that's what you're suggesting?) to result in a performance degradation.

}

return ret;
Expand Down Expand Up @@ -475,3 +475,11 @@ function arrayClone(arr, i) {
copy[i] = arr[i];
return copy;
}

function unwrapListeners(arr) {
const ret = new Array(arr.length);
for (var i = 0; i < ret.length; ++i) {
ret[i] = arr[i].listener || arr[i];
}
return ret;
}
13 changes: 13 additions & 0 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ function listener2() {}
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
assert.deepStrictEqual(eeListenersCopy, [listener]);
}

{
const ee = new events.EventEmitter();
ee.once('foo', listener);
assert.deepStrictEqual(ee.listeners('foo'), [listener]);
}

{
const ee = new events.EventEmitter();
ee.on('foo', listener);
ee.once('foo', listener2);
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
}