2012-02-27 20:09:33 +01:00
|
|
|
# Events
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2012-02-27 20:37:26 +01:00
|
|
|
<!--type=module-->
|
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Much of the Node.js core API is built around an idiomatic asynchronous
|
|
|
|
event-driven architecture in which certain kinds of objects (called "emitters")
|
|
|
|
periodically emit named events that cause Function objects ("listeners") to be
|
|
|
|
called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
For instance: a [`net.Server`][] object emits an event each time a peer
|
|
|
|
connects to it; a [`fs.ReadStream`][] emits an event when the file is opened;
|
|
|
|
a [stream][] emits an event whenever data is available to be read.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
All objects that emit events are instances of the `EventEmitter` class. These
|
|
|
|
objects expose an `eventEmitter.on()` function that allows one or more
|
|
|
|
Functions to be attached to named events emitted by the object. Typically,
|
|
|
|
event names are camel-cased strings but any valid JavaScript property key
|
|
|
|
can be used.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
When the `EventEmitter` object emits an event, all of the Functions attached
|
|
|
|
to that specific event are called _synchronously_. Any values returned by the
|
|
|
|
called listeners are _ignored_ and will be discarded.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
The following example shows a simple `EventEmitter` instance with a single
|
|
|
|
listener. The `eventEmitter.on()` method is used to register listeners, while
|
|
|
|
the `eventEmitter.emit()` method is used to trigger the event.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
function MyEmitter() {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
}
|
|
|
|
util.inherits(MyEmitter, EventEmitter);
|
|
|
|
|
|
|
|
const myEmitter = new MyEmitter();
|
2016-01-24 10:15:51 +01:00
|
|
|
myEmitter.on('event', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('an event occurred!');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Any object can become an `EventEmitter` through inheritance. The example above
|
|
|
|
uses the traditional Node.js style prototypical inheritance using
|
|
|
|
the `util.inherits()` method. It is, however, possible to use ES6 classes as
|
|
|
|
well:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const EventEmitter = require('events');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
class MyEmitter extends EventEmitter {}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
2016-01-24 10:15:51 +01:00
|
|
|
myEmitter.on('event', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('an event occurred!');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
## Passing arguments and `this` to listeners
|
|
|
|
|
|
|
|
The `eventEmitter.emit()` method allows an arbitrary set of arguments to be
|
|
|
|
passed to the listener functions. It is important to keep in mind that when an
|
|
|
|
ordinary listener function is called by the `EventEmitter`, the standard `this`
|
|
|
|
keyword is intentionally set to reference the `EventEmitter` to which the
|
|
|
|
listener is attached.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', function(a, b) {
|
|
|
|
console.log(a, b, this);
|
|
|
|
// Prints:
|
|
|
|
// a b MyEmitter {
|
|
|
|
// domain: null,
|
|
|
|
// _events: { event: [Function] },
|
|
|
|
// _eventsCount: 1,
|
|
|
|
// _maxListeners: undefined }
|
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
It is possible to use ES6 Arrow Functions as listeners, however, when doing so,
|
|
|
|
the `this` keyword will no longer reference the `EventEmitter` instance:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', (a, b) => {
|
|
|
|
console.log(a, b, this);
|
|
|
|
// Prints: a b {}
|
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
## Asynchronous vs. Synchronous
|
|
|
|
|
|
|
|
The `EventListener` calls all listeners synchronously in the order in which
|
|
|
|
they were registered. This is important to ensure the proper sequencing of
|
|
|
|
events and to avoid race conditions or logic errors. When appropriate,
|
|
|
|
listener functions can switch to an asynchronous mode of operation using
|
|
|
|
the `setImmediate()` or `process.nextTick()` methods:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', (a, b) => {
|
|
|
|
setImmediate(() => {
|
|
|
|
console.log('this happens asynchronously');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
## Handling events only once
|
|
|
|
|
|
|
|
When a listener is registered using the `eventEmitter.on()` method, that
|
|
|
|
listener will be invoked _every time_ the named event is emitted.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
var m = 0;
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log(++m);
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints: 1
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints: 2
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
Using the `eventEmitter.once()` method, it is possible to register a listener
|
|
|
|
that is immediately unregistered after it is called.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
var m = 0;
|
|
|
|
myEmitter.once('event', () => {
|
|
|
|
console.log(++m);
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints: 1
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Ignored
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
## Error events
|
|
|
|
|
|
|
|
When an error occurs within an `EventEmitter` instance, the typical action is
|
|
|
|
for an `'error'` event to be emitted. These are treated as a special case
|
|
|
|
within Node.js.
|
|
|
|
|
|
|
|
If an `EventEmitter` does _not_ have at least one listener registered for the
|
|
|
|
`'error'` event, and an `'error'` event is emitted, the error is thrown, a
|
|
|
|
stack trace is printed, and the Node.js process exits.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Throws and crashes Node.js
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
To guard against crashing the Node.js process, developers can either register
|
|
|
|
a listener for the `process.on('uncaughtException')` event or use the
|
|
|
|
[`domain`][] module (_Note, however, that the `domain` module has been
|
|
|
|
deprecated_).
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
2015-12-29 19:04:13 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
process.on('uncaughtException', (err) => {
|
|
|
|
console.log('whoops! there was an error');
|
|
|
|
});
|
2015-12-29 19:04:13 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Prints: whoops! there was an error
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
As a best practice, developers should always register listeners for the
|
|
|
|
`'error'` event:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('error', (err) => {
|
|
|
|
console.log('whoops! there was an error');
|
|
|
|
});
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Prints: whoops! there was an error
|
|
|
|
```
|
2013-05-01 00:24:48 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
## Class: EventEmitter
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
The `EventEmitter` class is defined and exposed by the `events` module:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
All EventEmitters emit the event `'newListener'` when new listeners are
|
|
|
|
added and `'removeListener'` when a listener is removed.
|
2013-05-01 00:24:48 +02:00
|
|
|
|
2015-11-04 17:45:27 +01:00
|
|
|
### Event: 'newListener'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
* `eventName` {String|Symbol} The name of the event being listened for
|
2015-11-04 17:45:27 +01:00
|
|
|
* `listener` {Function} The event handler function
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
The `EventEmitter` instance will emit it's own `'newListener'` event *before*
|
|
|
|
a listener is added to it's internal array of listeners.
|
|
|
|
|
|
|
|
Listeners registered for the `'newListener'` event will be passed the event
|
|
|
|
name and a reference to the listener being added.
|
|
|
|
|
|
|
|
The fact that the event is triggered before adding the listener has a subtle
|
|
|
|
but important side effect: any *additional* listeners registered to the same
|
|
|
|
`name` *within* the `'newListener'` callback will be inserted *before* the
|
|
|
|
listener that is in the process of being added.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
// Only do this once so we don't loop forever
|
|
|
|
myEmitter.once('newListener', (event, listener) => {
|
|
|
|
if (event === 'event') {
|
|
|
|
// Insert a new listener in front
|
2015-12-29 19:04:13 +01:00
|
|
|
myEmitter.on('event', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('B');
|
2015-12-29 19:04:13 +01:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log('A');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints:
|
|
|
|
// B
|
|
|
|
// A
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 17:45:27 +01:00
|
|
|
### Event: 'removeListener'
|
2014-12-19 18:53:20 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
* `eventName` {String|Symbol} The event name
|
2015-11-04 17:45:27 +01:00
|
|
|
* `listener` {Function} The event handler function
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
The `'removeListener'` event is emitted *after* a listener is removed.
|
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### EventEmitter.listenerCount(emitter, eventName)
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
Stability: 0 - Deprecated: Use [`emitter.listenerCount()`][] instead.
|
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
A class method that returns the number of listeners for the given `eventName`
|
2015-12-29 19:04:13 +01:00
|
|
|
registered on the given `emitter`.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', () => {});
|
|
|
|
myEmitter.on('event', () => {});
|
|
|
|
console.log(EventEmitter.listenerCount(myEmitter, 'event'));
|
|
|
|
// Prints: 2
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 17:45:27 +01:00
|
|
|
### EventEmitter.defaultMaxListeners
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
By default, a maximum of `10` listeners can be registered for any single
|
|
|
|
event. This limit can be changed for individual `EventEmitter` instances
|
|
|
|
using the [`emitter.setMaxListeners(n)`][] method. To change the default
|
|
|
|
for *all* `EventEmitter` instances, the `EventEmitter.defaultMaxListeners`
|
|
|
|
property can be used.
|
|
|
|
|
|
|
|
Take caution when setting the `EventEmitter.defaultMaxListeners` because the
|
|
|
|
change effects *all* `EventEmitter` instances, including those created before
|
|
|
|
the change is made. However, calling [`emitter.setMaxListeners(n)`][] still has
|
|
|
|
precedence over `EventEmitter.defaultMaxListeners`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Note that this is not a hard limit. The `EventEmitter` instance will allow
|
|
|
|
more listeners to be added but will output a trace warning to stderr indicating
|
|
|
|
that a `possible EventEmitter memory leak` has been detected. For any single
|
|
|
|
`EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()`
|
|
|
|
methods can be used to temporarily avoid this warning:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
|
|
|
|
emitter.once('event', () => {
|
|
|
|
// do stuff
|
|
|
|
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
|
|
|
});
|
|
|
|
```
|
2011-01-01 03:32:52 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.addListener(eventName, listener)
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Alias for `emitter.on(eventName, listener)`.
|
2015-11-04 17:45:27 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.emit(eventName[, arg1][, arg2][, ...])
|
2015-11-04 17:45:27 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Synchronously calls each of the listeners registered for the event named
|
|
|
|
`eventName`, in the order they were registered, passing the supplied arguments
|
|
|
|
to each.
|
2015-11-04 17:45:27 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Returns `true` if the event had listeners, `false` otherwise.
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2016-03-09 06:29:38 +01:00
|
|
|
### emitter.eventNames()
|
|
|
|
|
|
|
|
Returns an array listing the events for which the emitter has registered
|
|
|
|
listeners. The values in the array will be strings or Symbols.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
myEE.on('foo', () => {});
|
|
|
|
myEE.on('bar', () => {});
|
|
|
|
|
|
|
|
const sym = Symbol('symbol');
|
|
|
|
myEE.on(sym, () => {});
|
|
|
|
|
|
|
|
console.log(myErr.eventNames());
|
|
|
|
// Prints ['foo', 'bar', Symbol('symbol')]
|
|
|
|
```
|
|
|
|
|
2014-12-05 14:50:05 +01:00
|
|
|
### emitter.getMaxListeners()
|
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Returns the current max listener value for the `EventEmitter` which is either
|
|
|
|
set by [`emitter.setMaxListeners(n)`][] or defaults to
|
2015-11-14 04:21:49 +01:00
|
|
|
[`EventEmitter.defaultMaxListeners`][].
|
2014-12-05 14:50:05 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.listenerCount(eventName)
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
* `eventName` {Value} The name of the event being listened for
|
2011-01-01 03:32:52 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Returns the number of listeners listening to the event named `eventName`.
|
2011-01-01 03:32:52 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.listeners(eventName)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Returns a copy of the array of listeners for the event named `eventName`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.on('connection', (stream) => {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
|
|
|
console.log(util.inspect(server.listeners('connection')));
|
|
|
|
// Prints: [ [Function] ]
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.on(eventName, listener)
|
2012-06-15 02:24:40 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Adds the `listener` function to the end of the listeners array for the
|
2016-03-22 22:21:46 +01:00
|
|
|
event named `eventName`. No checks are made to see if the `listener` has
|
|
|
|
already been added. Multiple calls passing the same combination of `eventName`
|
|
|
|
and `listener` will result in the `listener` being added, and called, multiple
|
2015-12-29 19:04:13 +01:00
|
|
|
times.
|
2015-08-11 20:31:50 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.on('connection', (stream) => {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
|
|
|
```
|
2015-08-11 20:31:50 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Returns a reference to the `EventEmitter` so calls can be chained.
|
2013-02-14 09:48:11 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.once(eventName, listener)
|
2013-02-14 09:48:11 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Adds a **one time** `listener` function for the event named `eventName`. This
|
|
|
|
listener is invoked only the next time `eventName` is triggered, after which
|
|
|
|
it is removed.
|
2013-02-14 09:48:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.once('connection', (stream) => {
|
|
|
|
console.log('Ah, we have our first user!');
|
|
|
|
});
|
|
|
|
```
|
2010-11-14 13:51:57 +01:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Returns a reference to the `EventEmitter` so calls can be chained.
|
2010-11-14 13:51:57 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.removeAllListeners([eventName])
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Removes all listeners, or those of the specified `eventName`.
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
Note that it is bad practice to remove listeners added elsewhere in the code,
|
|
|
|
particularly when the `EventEmitter` instance was created by some other
|
|
|
|
component or module (e.g. sockets or file streams).
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Returns a reference to the `EventEmitter` so calls can be chained.
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
### emitter.removeListener(eventName, listener)
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2016-03-22 22:21:46 +01:00
|
|
|
Removes the specified `listener` from the listener array for the event named
|
|
|
|
`eventName`.
|
2015-07-12 18:10:57 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-01-24 10:15:51 +01:00
|
|
|
var callback = (stream) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('someone connected!');
|
|
|
|
};
|
|
|
|
server.on('connection', callback);
|
|
|
|
// ...
|
|
|
|
server.removeListener('connection', callback);
|
|
|
|
```
|
2015-07-12 18:10:57 +02:00
|
|
|
|
2015-11-04 17:45:27 +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
|
2016-03-22 22:21:46 +01:00
|
|
|
listener array for the specified `eventName`, then `removeListener` must be
|
|
|
|
called multiple times to remove each instance.
|
2015-07-12 18:10:57 +02:00
|
|
|
|
2016-02-12 10:48:21 +01:00
|
|
|
Note that once an event has been emitted, all listeners attached to it at the
|
|
|
|
time of emitting will be called in order. This implies that any `removeListener()`
|
|
|
|
or `removeAllListeners()` calls *after* emitting and *before* the last listener
|
|
|
|
finishes execution will not remove them from `emit()` in progress. Subsequent
|
|
|
|
events will behave as expected.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
|
|
|
var callbackA = () => {
|
|
|
|
console.log('A');
|
|
|
|
myEmitter.removeListener('event', callbackB);
|
|
|
|
};
|
|
|
|
|
|
|
|
var callbackB = () => {
|
|
|
|
console.log('B');
|
|
|
|
};
|
|
|
|
|
|
|
|
myEmitter.on('event', callbackA);
|
|
|
|
|
|
|
|
myEmitter.on('event', callbackB);
|
|
|
|
|
|
|
|
// callbackA removes listener callbackB but it will still be called.
|
2016-03-22 22:18:02 +01:00
|
|
|
// Internal listener array at time of emit [callbackA, callbackB]
|
2016-02-12 10:48:21 +01:00
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints:
|
|
|
|
// A
|
|
|
|
// B
|
|
|
|
|
|
|
|
// callbackB is now removed.
|
2016-03-22 22:18:02 +01:00
|
|
|
// Internal listener array [callbackA]
|
2016-02-12 10:48:21 +01:00
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints:
|
|
|
|
// A
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Because listeners are managed using an internal array, calling this will
|
|
|
|
change the position indices of any listener registered *after* the listener
|
|
|
|
being removed. This will not impact the order in which listeners are called,
|
|
|
|
but it will means that any copies of the listener array as returned by
|
|
|
|
the `emitter.listeners()` method will need to be recreated.
|
|
|
|
|
|
|
|
Returns a reference to the `EventEmitter` so calls can be chained.
|
2015-07-12 18:10:57 +02:00
|
|
|
|
2015-11-04 17:45:27 +01:00
|
|
|
### emitter.setMaxListeners(n)
|
2015-07-12 18:10:57 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
By default EventEmitters will print a warning if more than `10` listeners are
|
|
|
|
added for a particular event. This is a useful default that helps finding
|
|
|
|
memory leaks. Obviously, not all events should be limited to just 10 listeners.
|
|
|
|
The `emitter.setMaxListeners()` method allows the limit to be modified for this
|
|
|
|
specific `EventEmitter` instance. The value can be set to `Infinity` (or `0`)
|
|
|
|
for to indicate an unlimited number of listeners.
|
2015-08-20 00:37:52 +02:00
|
|
|
|
2015-12-29 19:04:13 +01:00
|
|
|
Returns a reference to the `EventEmitter` so calls can be chained.
|
2015-08-20 00:37:52 +02:00
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
[`net.Server`]: net.html#net_class_net_server
|
|
|
|
[`fs.ReadStream`]: fs.html#fs_class_fs_readstream
|
|
|
|
[`emitter.setMaxListeners(n)`]: #events_emitter_setmaxlisteners_n
|
|
|
|
[`EventEmitter.defaultMaxListeners`]: #events_eventemitter_defaultmaxlisteners
|
2015-12-29 19:04:13 +01:00
|
|
|
[`emitter.listenerCount()`]: #events_emitter_listenercount_event
|
|
|
|
[`domain`]: domain.html
|
|
|
|
[stream]: stream.html
|