0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/doc/api/events.markdown

112 lines
3.6 KiB
Markdown
Raw Normal View History

2012-02-27 20:09:33 +01:00
# Events
2010-10-28 14:18:16 +02:00
Stability: 4 - API Frozen
<!--type=module-->
Many objects in Node emit events: a `net.Server` emits an event each time
a peer connects to it, a `fs.readStream` emits an event when the file is
opened. All objects which emit events are instances of `events.EventEmitter`.
You can access this module by doing: `require("events");`
2010-10-28 14:18:16 +02:00
Typically, event names are represented by a camel-cased string, however,
there aren't any strict restrictions on that, as any string will be accepted.
2010-10-28 14:18:16 +02:00
Functions can then be attached to objects, to be executed when an event
2010-10-28 14:18:16 +02:00
is emitted. These functions are called _listeners_.
2012-02-27 20:09:33 +01:00
## Class: events.EventEmitter
2010-10-28 14:18:16 +02:00
To access the EventEmitter class, `require('events').EventEmitter`.
2010-10-28 14:18:16 +02:00
When an `EventEmitter` instance experiences an error, the typical action is
to emit an `'error'` event. Error events are treated as a special case in node.
If there is no listener for it, then the default action is to print a stack
trace and exit the program.
2010-10-28 14:18:16 +02:00
All EventEmitters emit the event `'newListener'` when new listeners are
added.
2010-10-28 14:18:16 +02:00
2012-02-27 20:09:33 +01:00
### emitter.addListener(event, listener)
### emitter.on(event, listener)
2010-10-28 14:18:16 +02:00
Adds a listener to the end of the listeners array for the specified event.
server.on('connection', function (stream) {
console.log('someone connected!');
});
2010-10-28 14:18:16 +02:00
2012-02-27 20:09:33 +01:00
### emitter.once(event, listener)
2010-10-28 14:18:16 +02:00
Adds a **one time** listener for the event. This listener is
invoked only the next time the event is fired, after which
2010-10-28 14:18:16 +02:00
it is removed.
server.once('connection', function (stream) {
console.log('Ah, we have our first user!');
});
2010-10-28 14:18:16 +02:00
2012-02-27 20:09:33 +01:00
### emitter.removeListener(event, listener)
2010-10-28 14:18:16 +02:00
Remove a listener from the listener array for the specified event.
**Caution**: changes array indices in the listener array behind the listener.
var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
2010-10-28 14:18:16 +02:00
2012-02-27 20:09:33 +01:00
### emitter.removeAllListeners([event])
2010-10-28 14:18:16 +02:00
Removes all listeners, or those of the specified event.
2010-10-28 14:18:16 +02:00
Note that this will **invalidate** any arrays that have previously been
returned by `emitter.listeners(event)`.
2010-10-28 14:18:16 +02:00
2012-02-27 20:09:33 +01:00
### emitter.setMaxListeners(n)
By default EventEmitters will print a warning if more than 10 listeners are
added for a particular event. This is a useful default which helps finding memory leaks.
Obviously not all Emitters should be limited to 10. This function allows
that to be increased. Set to zero for unlimited.
2012-02-27 20:09:33 +01:00
### emitter.listeners(event)
2010-10-28 14:18:16 +02:00
Returns an array of listeners for the specified event.
2010-10-28 14:18:16 +02:00
server.on('connection', function (stream) {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection'))); // [ [Function] ]
2010-10-28 14:18:16 +02:00
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.
2012-02-27 20:09:33 +01:00
### emitter.emit(event, [arg1], [arg2], [...])
2010-10-28 14:18:16 +02:00
Execute each of the listeners in order with the supplied arguments.
2012-02-27 20:09:33 +01:00
### Event: 'newListener'
2012-02-27 20:09:33 +01:00
* `event` {String} The event name
* `listener` {Function} The event handler function
This event is emitted any time someone adds a new listener.