Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

docs: emitter.listeners return a copy of original array rather than an array reference #9022

Closed
StevenRoan opened this issue Jan 13, 2015 · 1 comment

Comments

@StevenRoan
Copy link

As title, the document use the description

  • Returns an array of listeners for the specified event.

However, return an array usually indicating return an array reference, and here is different.
Maybe we can consider to modify this part of document, Thanks!

Example code

var EventEmitter = require('events').EventEmitter;
var eventEmitter = new EventEmitter();
var arrListeners = eventEmitter.listeners('test-event');
console.log(arrListeners.length);
eventEmitter.addListener('test-event', function(){
  //Do sth.
});
console.log(arrListeners.length); // log 0 because the this array reference is different with current one. 
arrListeners = eventEmitter.listeners('test-event');
console.log(arrListeners.length);

get output
0
0
1

@bsteephenson
Copy link

https://github.com/joyent/node/blob/master/lib/events.js#L155
addListener is optimized to not even make an array if there's only one listener. Calling .listeners(), it just makes an array on the fly. Changing the documentation rather than the feature makes more sense.

bsteephenson added a commit to bsteephenson/node that referenced this issue Mar 3, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue nodejs#9022
bsteephenson added a commit to bsteephenson/node that referenced this issue Mar 3, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue nodejs#9022
jasnell pushed a commit to jasnell/node-joyent that referenced this issue Jun 29, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue nodejs#9022
jasnell pushed a commit to jasnell/node-joyent that referenced this issue Jul 1, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue nodejs#9022
jasnell pushed a commit that referenced this issue Jul 10, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue #9022

Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #25635
@jasnell jasnell closed this as completed Jul 10, 2015
jasnell pushed a commit to jasnell/node-joyent that referenced this issue Jul 10, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue nodejs#9022

Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#25635
jasnell pushed a commit that referenced this issue Aug 4, 2015
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue #9022

Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #25591
jBarz pushed a commit to ibmruntimes/node that referenced this issue Nov 4, 2016
Clarifies that emitter.listener() returns a copy, not a reference
Resolves issue nodejs#9022

Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#25591
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants