2012-02-27 20:09:33 +01:00
|
|
|
# Events
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-03 00:14:03 +01:00
|
|
|
Stability: 4 - API Frozen
|
|
|
|
|
2012-02-27 20:37:26 +01:00
|
|
|
<!--type=module-->
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
Many objects in io.js emit events: a `net.Server` emits an event each time
|
2010-11-21 23:22:34 +01:00
|
|
|
a peer connects to it, a `fs.readStream` emits an event when the file is
|
2010-11-14 13:51:57 +01:00
|
|
|
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
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
Typically, event names are represented by a camel-cased string, however,
|
2010-11-14 13:51:57 +01:00
|
|
|
there aren't any strict restrictions on that, as any string will be accepted.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-14 19:45:01 +01:00
|
|
|
Functions can then be attached to objects, to be executed when an event
|
2013-04-19 00:34:12 +02:00
|
|
|
is emitted. These functions are called _listeners_. Inside a listener
|
|
|
|
function, `this` refers to the `EventEmitter` that the listener was
|
|
|
|
attached to.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## Class: events.EventEmitter
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-14 13:51:57 +01:00
|
|
|
To access the EventEmitter class, `require('events').EventEmitter`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
When an `EventEmitter` instance experiences an error, the typical action is
|
2015-02-07 11:25:13 +01:00
|
|
|
to emit an `'error'` event. Error events are treated as a special case in
|
|
|
|
io.js. 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
|
|
|
|
2010-11-14 13:51:57 +01:00
|
|
|
All EventEmitters emit the event `'newListener'` when new listeners are
|
2012-08-01 00:57:15 +02:00
|
|
|
added and `'removeListener'` when a listener is removed.
|
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
|
|
|
|
2014-12-19 18:53:20 +01:00
|
|
|
Adds a listener to the end of the listeners array for the specified `event`.
|
|
|
|
No checks are made to see if the `listener` has already been added. Multiple
|
|
|
|
calls passing the same combination of `event` and `listener` will result in the
|
|
|
|
`listener` being added multiple times.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
server.on('connection', function (stream) {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-05-01 00:24:48 +02:00
|
|
|
Returns emitter, so calls can be chained.
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
### emitter.once(event, listener)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-08-31 15:12:34 +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.
|
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
server.once('connection', function (stream) {
|
|
|
|
console.log('Ah, we have our first user!');
|
|
|
|
});
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-05-01 00:24:48 +02:00
|
|
|
Returns emitter, so calls can be chained.
|
|
|
|
|
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.
|
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
var callback = function(stream) {
|
|
|
|
console.log('someone connected!');
|
|
|
|
};
|
|
|
|
server.on('connection', callback);
|
|
|
|
// ...
|
|
|
|
server.removeListener('connection', callback);
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-12-19 18:53:20 +01:00
|
|
|
`removeListener` will remove, at most, one instance of a listener from the
|
|
|
|
listener array. If any single listener has been added multiple times to the
|
|
|
|
listener array for the specified `event`, then `removeListener` must be called
|
|
|
|
multiple times to remove each instance.
|
|
|
|
|
2013-05-01 00:24:48 +02:00
|
|
|
Returns emitter, so calls can be chained.
|
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
|
|
|
|
2014-04-28 21:38:06 +02:00
|
|
|
Removes all listeners, or those of the specified event. It's not a good idea to
|
|
|
|
remove listeners that were added elsewhere in the code, especially when it's on
|
|
|
|
an emitter that you didn't create (e.g. sockets or file streams).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-05-01 00:24:48 +02:00
|
|
|
Returns emitter, so calls can be chained.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
### emitter.setMaxListeners(n)
|
2011-01-01 03:32:52 +01:00
|
|
|
|
|
|
|
By default EventEmitters will print a warning if more than 10 listeners are
|
2012-05-04 17:45:27 +02:00
|
|
|
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.
|
|
|
|
|
2013-05-01 00:26:14 +02:00
|
|
|
Returns emitter, so calls can be chained.
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2014-12-05 14:50:05 +01:00
|
|
|
### emitter.getMaxListeners()
|
|
|
|
|
|
|
|
Returns the current max listener value for the emitter which is either set by
|
|
|
|
`emitter.setMaxListeners(n)` or defaults to `EventEmitter.defaultMaxListeners`.
|
|
|
|
|
|
|
|
This can be useful to increment/decrement max listeners to avoid the warning
|
|
|
|
while not being irresponsible and setting a too big number.
|
|
|
|
|
|
|
|
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
|
|
|
|
emitter.once('event', function () {
|
|
|
|
// do stuff
|
|
|
|
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
|
|
|
});
|
|
|
|
|
2012-05-04 17:45:27 +02:00
|
|
|
### EventEmitter.defaultMaxListeners
|
|
|
|
|
|
|
|
`emitter.setMaxListeners(n)` sets the maximum on a per-instance basis.
|
|
|
|
This class property lets you set it for *all* `EventEmitter` instances,
|
|
|
|
current and future, effective immediately. Use with care.
|
|
|
|
|
|
|
|
Note that `emitter.setMaxListeners(n)` still has precedence over
|
|
|
|
`EventEmitter.defaultMaxListeners`.
|
2011-01-01 03:32:52 +01:00
|
|
|
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
### emitter.listeners(event)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-06-15 02:24:40 +02:00
|
|
|
Returns an array of listeners for the specified event.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
server.on('connection', function (stream) {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
2011-08-31 15:12:34 +02:00
|
|
|
console.log(util.inspect(server.listeners('connection'))); // [ [Function] ]
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-06-15 02:24:40 +02:00
|
|
|
|
2014-09-30 01:32:34 +02: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.
|
2010-11-14 13:51:57 +01:00
|
|
|
|
2013-05-01 00:24:48 +02:00
|
|
|
Returns `true` if event had listeners, `false` otherwise.
|
|
|
|
|
2013-02-14 09:48:11 +01:00
|
|
|
|
|
|
|
### Class Method: EventEmitter.listenerCount(emitter, event)
|
|
|
|
|
|
|
|
Return the number of listeners for a given event.
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
### Event: 'newListener'
|
2010-11-14 13:51:57 +01:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
* `event` {String} The event name
|
|
|
|
* `listener` {Function} The event handler function
|
2010-11-14 13:51:57 +01:00
|
|
|
|
2015-02-02 04:57:31 +01:00
|
|
|
This event is emitted *before* a listener is added. When this event is
|
|
|
|
triggered, the listener has not been added to the array of listeners for the
|
|
|
|
`event`. Any listeners added to the event `name` in the newListener event
|
|
|
|
callback will be added *before* the listener that is in the process of being
|
|
|
|
added.
|
2013-03-12 00:03:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
### Event: 'removeListener'
|
|
|
|
|
|
|
|
* `event` {String} The event name
|
|
|
|
* `listener` {Function} The event handler function
|
|
|
|
|
2015-02-02 04:57:31 +01:00
|
|
|
This event is emitted *after* a listener is removed. When this event is
|
|
|
|
triggered, the listener has been removed from the array of listeners for the
|
|
|
|
`event`.
|