Browse Source

event: Document the mutability of listeners()

v0.9.1-release
isaacs 13 years ago
parent
commit
e72addcf8e
  1. 20
      doc/api/events.markdown

20
doc/api/events.markdown

@ -64,6 +64,9 @@ Remove a listener from the listener array for the specified event.
Removes all listeners, or those of the specified event. Removes all listeners, or those of the specified event.
Note that this will **invalidate** any arrays that have previously been
returned by `emitter.listeners(event)`.
### emitter.setMaxListeners(n) ### emitter.setMaxListeners(n)
@ -75,14 +78,27 @@ that to be increased. Set to zero for unlimited.
### emitter.listeners(event) ### emitter.listeners(event)
Returns an array of listeners for the specified event. This array can be Returns an array of listeners for the specified event.
manipulated, e.g. to remove listeners.
server.on('connection', function (stream) { server.on('connection', function (stream) {
console.log('someone connected!'); console.log('someone connected!');
}); });
console.log(util.inspect(server.listeners('connection'))); // [ [Function] ] console.log(util.inspect(server.listeners('connection'))); // [ [Function] ]
This array **may** be a mutable reference to the same underlying list of
listeners that is used by the event subsystem. However, certain
actions (specifically, removeAllListeners) will invalidate this
reference.
If you would like to get a copy of the listeners at a specific point in
time that is guaranteed not to change, make a copy, for example by doing
`emitter.listeners(event).slice(0)`.
In a future release of node, this behavior **may** change to always
return a copy, for consistency. In your programs, please do not rely on
being able to modify the EventEmitter listeners using array methods.
Always use the 'on' method to add new listeners.
### emitter.emit(event, [arg1], [arg2], [...]) ### emitter.emit(event, [arg1], [arg2], [...])
Execute each of the listeners in order with the supplied arguments. Execute each of the listeners in order with the supplied arguments.

Loading…
Cancel
Save