2012-02-27 20:09:33 +01:00
|
|
|
|
# Events
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
|
2016-07-16 00:35:38 +02: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")
|
2018-05-07 21:17:09 +02:00
|
|
|
|
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
|
2016-06-29 19:50:23 +02:00
|
|
|
|
functions to be attached to named events emitted by the object. Typically,
|
2015-12-29 19:04:13 +01:00
|
|
|
|
event names are camel-cased strings but any valid JavaScript property key
|
|
|
|
|
can be used.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
When the `EventEmitter` object emits an event, all of the functions attached
|
2015-12-29 19:04:13 +01:00
|
|
|
|
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');
|
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
|
2019-10-25 00:19:07 +02:00
|
|
|
|
passed to the listener functions. Keep in mind that when
|
2018-05-05 09:53:43 +02:00
|
|
|
|
an ordinary listener function is called, the standard `this` keyword
|
|
|
|
|
is intentionally set to reference the `EventEmitter` instance to which the
|
2015-12-29 19:04:13 +01:00
|
|
|
|
listener is attached.
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
myEmitter.on('event', function(a, b) {
|
2018-05-05 09:53:43 +02:00
|
|
|
|
console.log(a, b, this, this === myEmitter);
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints:
|
|
|
|
|
// a b MyEmitter {
|
|
|
|
|
// domain: null,
|
|
|
|
|
// _events: { event: [Function] },
|
|
|
|
|
// _eventsCount: 1,
|
2018-05-05 09:53:43 +02:00
|
|
|
|
// _maxListeners: undefined } true
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
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);
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: a b {}
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
|
|
## Asynchronous vs. Synchronous
|
|
|
|
|
|
2017-11-06 10:37:50 +01:00
|
|
|
|
The `EventEmitter` calls all listeners synchronously in the order in which
|
2019-10-25 00:19:07 +02:00
|
|
|
|
they were registered. This ensures the proper sequencing of
|
|
|
|
|
events and helps avoid race conditions and logic errors. When appropriate,
|
2015-12-29 19:04:13 +01:00
|
|
|
|
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();
|
2017-03-12 12:11:13 +01:00
|
|
|
|
let m = 0;
|
2016-01-17 18:39:07 +01:00
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
|
console.log(++m);
|
|
|
|
|
});
|
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: 1
|
2016-01-17 18:39:07 +01:00
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: 2
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
|
|
Using the `eventEmitter.once()` method, it is possible to register a listener
|
2016-06-29 19:50:23 +02:00
|
|
|
|
that is called at most once for a particular event. Once the event is emitted,
|
|
|
|
|
the listener is unregistered and *then* called.
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const myEmitter = new MyEmitter();
|
2017-03-12 12:11:13 +01:00
|
|
|
|
let m = 0;
|
2016-01-17 18:39:07 +01:00
|
|
|
|
myEmitter.once('event', () => {
|
|
|
|
|
console.log(++m);
|
|
|
|
|
});
|
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: 1
|
2016-01-17 18:39:07 +01:00
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Ignored
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
|
|
## Error events
|
|
|
|
|
|
|
|
|
|
When an error occurs within an `EventEmitter` instance, the typical action is
|
2016-06-29 19:50:23 +02:00
|
|
|
|
for an `'error'` event to be emitted. These are treated as special cases
|
2015-12-29 19:04:13 +01:00
|
|
|
|
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!'));
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Throws and crashes Node.js
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
2017-11-09 17:58:42 +01:00
|
|
|
|
To guard against crashing the Node.js process the [`domain`][] module can be
|
2018-06-04 20:58:47 +02:00
|
|
|
|
used. (Note, however, that the `domain` module is deprecated.)
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
As a best practice, listeners should always be added for the `'error'` events.
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
myEmitter.on('error', (err) => {
|
2017-03-12 12:14:00 +01:00
|
|
|
|
console.error('whoops! there was an error');
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: whoops! there was an error
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2013-05-01 00:24:48 +02:00
|
|
|
|
|
2019-12-12 21:08:42 +01:00
|
|
|
|
It is possible to monitor `'error'` events without consuming the emitted error
|
|
|
|
|
by installing a listener using the symbol `errorMonitor`.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
myEmitter.on(EventEmitter.errorMonitor, (err) => {
|
|
|
|
|
MyMonitoringTool.log(err);
|
|
|
|
|
});
|
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
|
// Still throws and crashes Node.js
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-19 13:55:18 +02:00
|
|
|
|
## Capture Rejections of Promises
|
|
|
|
|
|
|
|
|
|
> Stability: 1 - captureRejections is experimental.
|
|
|
|
|
|
|
|
|
|
Using `async` functions with event handlers is problematic, because it
|
|
|
|
|
can lead to an unhandled rejection in case of a thrown exception:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
ee.on('something', async (value) => {
|
|
|
|
|
throw new Error('kaboom');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `captureRejections` option in the `EventEmitter` constructor or the global
|
|
|
|
|
setting change this behavior, installing a `.then(undefined, handler)`
|
|
|
|
|
handler on the `Promise`. This handler routes the exception
|
|
|
|
|
asynchronously to the [`Symbol.for('nodejs.rejection')`][rejection] method
|
|
|
|
|
if there is one, or to [`'error'`][error] event handler if there is none.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const ee1 = new EventEmitter({ captureRejections: true });
|
|
|
|
|
ee1.on('something', async (value) => {
|
|
|
|
|
throw new Error('kaboom');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ee1.on('error', console.log);
|
|
|
|
|
|
|
|
|
|
const ee2 = new EventEmitter({ captureRejections: true });
|
|
|
|
|
ee2.on('something', async (value) => {
|
|
|
|
|
throw new Error('kaboom');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ee2[Symbol.for('nodejs.rejection')] = console.log;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Setting `EventEmitter.captureRejections = true` will change the default for all
|
|
|
|
|
new instances of `EventEmitter`.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
EventEmitter.captureRejections = true;
|
|
|
|
|
const ee1 = new EventEmitter();
|
|
|
|
|
ee1.on('something', async (value) => {
|
|
|
|
|
throw new Error('kaboom');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ee1.on('error', console.log);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `'error'` events that are generated by the `captureRejections` behavior
|
|
|
|
|
do not have a catch handler to avoid infinite error loops: the
|
|
|
|
|
recommendation is to **not use `async` functions as `'error'` event handlers**.
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
## Class: `EventEmitter`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
2019-05-19 13:55:18 +02:00
|
|
|
|
changes:
|
2019-12-13 17:02:36 +01:00
|
|
|
|
- version: v13.4.0
|
2019-05-19 13:55:18 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/27867
|
|
|
|
|
description: Added captureRejections option.
|
2016-07-21 15:58:56 +02:00
|
|
|
|
-->
|
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
|
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
|
All `EventEmitter`s emit the event `'newListener'` when new listeners are
|
2016-06-29 19:50:23 +02:00
|
|
|
|
added and `'removeListener'` when existing listeners are removed.
|
2013-05-01 00:24:48 +02:00
|
|
|
|
|
2019-05-19 13:55:18 +02:00
|
|
|
|
It supports the following option:
|
|
|
|
|
|
|
|
|
|
* `captureRejections` {boolean} It enables
|
|
|
|
|
[automatic capturing of promise rejection][capturerejections].
|
|
|
|
|
Default: `false`.
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### Event: `'newListener'`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-13 23:40:12 +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
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
The `EventEmitter` instance will emit its own `'newListener'` event *before*
|
|
|
|
|
a listener is added to its internal array of listeners.
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
|
|
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');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints:
|
|
|
|
|
// B
|
|
|
|
|
// A
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### Event: `'removeListener'`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.3
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v6.1.0, v4.7.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6394
|
|
|
|
|
description: For listeners attached using `.once()`, the `listener` argument
|
|
|
|
|
now yields the original listener function.
|
2016-07-21 15:58:56 +02:00
|
|
|
|
-->
|
2014-12-19 18:53:20 +01:00
|
|
|
|
|
2017-12-13 23:40:12 +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
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
The `'removeListener'` event is emitted *after* the `listener` is removed.
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `EventEmitter.listenerCount(emitter, eventName)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.12
|
|
|
|
|
deprecated: v4.0.0
|
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2019-10-01 17:57:09 +02:00
|
|
|
|
> Stability: 0 - Deprecated: Use [`emitter.listenerCount()`][] instead.
|
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `emitter` {EventEmitter} The emitter to query
|
|
|
|
|
* `eventName` {string|symbol} The event name
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
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'));
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: 2
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `EventEmitter.defaultMaxListeners`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.2
|
|
|
|
|
-->
|
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`
|
2017-03-21 01:18:33 +01:00
|
|
|
|
property can be used. If this value is not a positive number, a `TypeError`
|
|
|
|
|
will be thrown.
|
2015-12-29 19:04:13 +01:00
|
|
|
|
|
|
|
|
|
Take caution when setting the `EventEmitter.defaultMaxListeners` because the
|
2017-11-06 11:15:04 +01:00
|
|
|
|
change affects *all* `EventEmitter` instances, including those created before
|
2015-12-29 19:04:13 +01:00
|
|
|
|
the change is made. However, calling [`emitter.setMaxListeners(n)`][] still has
|
|
|
|
|
precedence over `EventEmitter.defaultMaxListeners`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-06-20 21:54:55 +02:00
|
|
|
|
This is not a hard limit. The `EventEmitter` instance will allow
|
2015-12-29 19:04:13 +01:00
|
|
|
|
more listeners to be added but will output a trace warning to stderr indicating
|
2016-06-29 19:50:23 +02:00
|
|
|
|
that a "possible EventEmitter memory leak" has been detected. For any single
|
2015-12-29 19:04:13 +01:00
|
|
|
|
`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-08-27 10:45:34 +02:00
|
|
|
|
The [`--trace-warnings`][] command line flag can be used to display the
|
|
|
|
|
stack trace for such warnings.
|
|
|
|
|
|
|
|
|
|
The emitted warning can be inspected with [`process.on('warning')`][] and will
|
|
|
|
|
have the additional `emitter`, `type` and `count` properties, referring to
|
|
|
|
|
the event emitter instance, the event’s name and the number of attached
|
|
|
|
|
listeners, respectively.
|
2016-08-30 19:15:34 +02:00
|
|
|
|
Its `name` property is set to `'MaxListenersExceededWarning'`.
|
2016-08-27 10:45:34 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `EventEmitter.errorMonitor`
|
2019-12-12 21:08:42 +01:00
|
|
|
|
<!-- YAML
|
2020-01-07 08:33:44 +01:00
|
|
|
|
added: v13.6.0
|
2019-12-12 21:08:42 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
This symbol shall be used to install a listener for only monitoring `'error'`
|
|
|
|
|
events. Listeners installed using this symbol are called before the regular
|
|
|
|
|
`'error'` listeners are called.
|
|
|
|
|
|
|
|
|
|
Installing a listener using this symbol does not change the behavior once an
|
|
|
|
|
`'error'` event is emitted, therefore the process will still crash if no
|
|
|
|
|
regular `'error'` listener is installed.
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.addListener(eventName, listener)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* `listener` {Function}
|
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
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.emit(eventName[, ...args])`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `eventName` {string|symbol}
|
2019-09-06 07:42:22 +02:00
|
|
|
|
* `...args` {any}
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* Returns: {boolean}
|
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
|
|
|
|
|
2019-06-22 00:37:37 +02:00
|
|
|
|
```js
|
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
// First listener
|
|
|
|
|
myEmitter.on('event', function firstListener() {
|
|
|
|
|
console.log('Helloooo! first listener');
|
|
|
|
|
});
|
|
|
|
|
// Second listener
|
|
|
|
|
myEmitter.on('event', function secondListener(arg1, arg2) {
|
|
|
|
|
console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
|
|
|
|
|
});
|
|
|
|
|
// Third listener
|
|
|
|
|
myEmitter.on('event', function thirdListener(...args) {
|
|
|
|
|
const parameters = args.join(', ');
|
|
|
|
|
console.log(`event with parameters ${parameters} in third listener`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(myEmitter.listeners('event'));
|
|
|
|
|
|
|
|
|
|
myEmitter.emit('event', 1, 2, 3, 4, 5);
|
|
|
|
|
|
|
|
|
|
// Prints:
|
|
|
|
|
// [
|
|
|
|
|
// [Function: firstListener],
|
|
|
|
|
// [Function: secondListener],
|
|
|
|
|
// [Function: thirdListener]
|
|
|
|
|
// ]
|
|
|
|
|
// Helloooo! first listener
|
|
|
|
|
// event with parameters 1, 2 in second listener
|
|
|
|
|
// event with parameters 1, 2, 3, 4, 5 in third listener
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.eventNames()`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v6.0.0
|
|
|
|
|
-->
|
2016-03-09 06:29:38 +01:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* Returns: {Array}
|
2018-04-11 20:07:14 +02:00
|
|
|
|
|
2016-03-09 06:29:38 +01:00
|
|
|
|
Returns an array listing the events for which the emitter has registered
|
2018-04-29 19:46:41 +02:00
|
|
|
|
listeners. The values in the array will be strings or `Symbol`s.
|
2016-03-09 06:29:38 +01:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
myEE.on('foo', () => {});
|
|
|
|
|
myEE.on('bar', () => {});
|
|
|
|
|
|
|
|
|
|
const sym = Symbol('symbol');
|
|
|
|
|
myEE.on(sym, () => {});
|
|
|
|
|
|
2016-04-27 11:55:34 +02:00
|
|
|
|
console.log(myEE.eventNames());
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
|
2016-03-09 06:29:38 +01:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.getMaxListeners()`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.0.0
|
|
|
|
|
-->
|
2014-12-05 14:50:05 +01:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* Returns: {integer}
|
2018-04-11 20:07:14 +02:00
|
|
|
|
|
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
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.listenerCount(eventName)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v3.2.0
|
|
|
|
|
-->
|
2012-05-04 17:45:27 +02:00
|
|
|
|
|
2017-12-13 23:40:12 +01:00
|
|
|
|
* `eventName` {string|symbol} The name of the event being listened for
|
2018-04-11 20:07:14 +02:00
|
|
|
|
* Returns: {integer}
|
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
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.listeners(eventName)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6881
|
|
|
|
|
description: For listeners attached using `.once()` this returns the
|
|
|
|
|
original listeners instead of wrapper functions now.
|
2016-07-21 15:58:56 +02:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* Returns: {Function[]}
|
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')));
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: [ [Function] ]
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.off(eventName, listener)`
|
2018-04-25 16:46:54 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v10.0.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* `listener` {Function}
|
|
|
|
|
* Returns: {EventEmitter}
|
|
|
|
|
|
|
|
|
|
Alias for [`emitter.removeListener()`][].
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.on(eventName, listener)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.101
|
|
|
|
|
-->
|
2012-06-15 02:24:40 +02:00
|
|
|
|
|
2017-12-13 23:40:12 +01:00
|
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 20:07:14 +02:00
|
|
|
|
* Returns: {EventEmitter}
|
2016-04-04 04:55:22 +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
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2013-02-14 09:48:11 +01:00
|
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
|
By default, event listeners are invoked in the order they are added. The
|
|
|
|
|
`emitter.prependListener()` method can be used as an alternative to add the
|
|
|
|
|
event listener to the beginning of the listeners array.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
myEE.on('foo', () => console.log('a'));
|
|
|
|
|
myEE.prependListener('foo', () => console.log('b'));
|
|
|
|
|
myEE.emit('foo');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints:
|
|
|
|
|
// b
|
|
|
|
|
// a
|
2016-04-04 04:55:22 +02:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.once(eventName, listener)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
2013-02-14 09:48:11 +01:00
|
|
|
|
|
2017-12-13 23:40:12 +01:00
|
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 20:07:14 +02:00
|
|
|
|
* Returns: {EventEmitter}
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
2017-12-06 19:15:58 +01:00
|
|
|
|
Adds a **one-time** `listener` function for the event named `eventName`. The
|
2016-04-25 15:35:30 +02:00
|
|
|
|
next time `eventName` is triggered, this listener is removed and then invoked.
|
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
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2010-11-14 13:51:57 +01:00
|
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
|
By default, event listeners are invoked in the order they are added. The
|
|
|
|
|
`emitter.prependOnceListener()` method can be used as an alternative to add the
|
|
|
|
|
event listener to the beginning of the listeners array.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
myEE.once('foo', () => console.log('a'));
|
|
|
|
|
myEE.prependOnceListener('foo', () => console.log('b'));
|
|
|
|
|
myEE.emit('foo');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints:
|
|
|
|
|
// b
|
|
|
|
|
// a
|
2016-04-04 04:55:22 +02:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.prependListener(eventName, listener)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v6.0.0
|
|
|
|
|
-->
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
2017-12-13 23:40:12 +01:00
|
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 20:07:14 +02:00
|
|
|
|
* Returns: {EventEmitter}
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
|
|
|
|
Adds the `listener` function to the *beginning* of the listeners array for the
|
|
|
|
|
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
|
|
|
|
|
times.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
server.prependListener('connection', (stream) => {
|
|
|
|
|
console.log('someone connected!');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.prependOnceListener(eventName, listener)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v6.0.0
|
|
|
|
|
-->
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
2017-12-13 23:40:12 +01:00
|
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 20:07:14 +02:00
|
|
|
|
* Returns: {EventEmitter}
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
2017-12-06 19:15:58 +01:00
|
|
|
|
Adds a **one-time** `listener` function for the event named `eventName` to the
|
2016-04-25 15:35:30 +02:00
|
|
|
|
*beginning* of the listeners array. The next time `eventName` is triggered, this
|
|
|
|
|
listener is removed, and then invoked.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
server.prependOnceListener('connection', (stream) => {
|
|
|
|
|
console.log('Ah, we have our first user!');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2016-04-04 04:55:22 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.removeAllListeners([eventName])`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* Returns: {EventEmitter}
|
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
|
|
|
|
|
2019-06-20 21:54:55 +02:00
|
|
|
|
It is bad practice to remove listeners added elsewhere in the code,
|
2015-12-29 19:04:13 +01:00
|
|
|
|
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
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2013-03-12 00:03:56 +01:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.removeListener(eventName, listener)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.26
|
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* `listener` {Function}
|
|
|
|
|
* Returns: {EventEmitter}
|
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
|
2017-03-12 12:11:13 +01:00
|
|
|
|
const 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
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
|
`removeListener()` will remove, at most, one instance of a listener from the
|
2015-11-04 17:45:27 +01:00
|
|
|
|
listener array. If any single listener has been added multiple times to the
|
2018-04-09 18:30:22 +02:00
|
|
|
|
listener array for the specified `eventName`, then `removeListener()` must be
|
2016-03-22 22:21:46 +01:00
|
|
|
|
called multiple times to remove each instance.
|
2015-07-12 18:10:57 +02:00
|
|
|
|
|
2019-06-20 21:54:55 +02:00
|
|
|
|
Once an event has been emitted, all listeners attached to it at the
|
2018-02-12 08:31:55 +01:00
|
|
|
|
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.
|
2016-02-12 10:48:21 +01:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
|
2017-03-12 12:11:13 +01:00
|
|
|
|
const callbackA = () => {
|
2016-02-12 10:48:21 +01:00
|
|
|
|
console.log('A');
|
|
|
|
|
myEmitter.removeListener('event', callbackB);
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-12 12:11:13 +01:00
|
|
|
|
const callbackB = () => {
|
2016-02-12 10:48:21 +01:00
|
|
|
|
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');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints:
|
|
|
|
|
// A
|
|
|
|
|
// B
|
2016-02-12 10:48:21 +01:00
|
|
|
|
|
|
|
|
|
// 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');
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints:
|
|
|
|
|
// A
|
2016-02-12 10:48:21 +01:00
|
|
|
|
```
|
|
|
|
|
|
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,
|
2016-06-29 19:50:23 +02:00
|
|
|
|
but it means that any copies of the listener array as returned by
|
2015-12-29 19:04:13 +01:00
|
|
|
|
the `emitter.listeners()` method will need to be recreated.
|
|
|
|
|
|
2018-10-19 20:57:19 +02:00
|
|
|
|
When a single function has been added as a handler multiple times for a single
|
|
|
|
|
event (as in the example below), `removeListener()` will remove the most
|
|
|
|
|
recently added instance. In the example the `once('ping')`
|
|
|
|
|
listener is removed:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
function pong() {
|
|
|
|
|
console.log('pong');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ee.on('ping', pong);
|
|
|
|
|
ee.once('ping', pong);
|
|
|
|
|
ee.removeListener('ping', pong);
|
|
|
|
|
|
|
|
|
|
ee.emit('ping');
|
|
|
|
|
ee.emit('ping');
|
|
|
|
|
```
|
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2015-07-12 18:10:57 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.setMaxListeners(n)`
|
2016-07-21 15:58:56 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.5
|
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `n` {integer}
|
|
|
|
|
* Returns: {EventEmitter}
|
2015-07-12 18:10:57 +02:00
|
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
|
By default `EventEmitter`s will print a warning if more than `10` listeners are
|
2015-12-29 19:04:13 +01:00
|
|
|
|
added for a particular event. This is a useful default that helps finding
|
2020-03-24 14:26:10 +01:00
|
|
|
|
memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
|
|
|
|
|
modified for this specific `EventEmitter` instance. The value can be set to
|
|
|
|
|
`Infinity` (or `0`) to indicate an unlimited number of listeners.
|
2015-08-20 00:37:52 +02:00
|
|
|
|
|
2016-06-29 19:50:23 +02:00
|
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2015-08-20 00:37:52 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter.rawListeners(eventName)`
|
2017-11-25 19:26:28 +01:00
|
|
|
|
<!-- YAML
|
2018-01-10 01:23:55 +01:00
|
|
|
|
added: v9.4.0
|
2017-11-25 19:26:28 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* Returns: {Function[]}
|
2017-11-25 19:26:28 +01:00
|
|
|
|
|
|
|
|
|
Returns a copy of the array of listeners for the event named `eventName`,
|
2018-04-09 18:30:22 +02:00
|
|
|
|
including any wrappers (such as those created by `.once()`).
|
2017-11-25 19:26:28 +01:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
|
emitter.once('log', () => console.log('log once'));
|
|
|
|
|
|
|
|
|
|
// Returns a new Array with a function `onceWrapper` which has a property
|
|
|
|
|
// `listener` which contains the original listener bound above
|
|
|
|
|
const listeners = emitter.rawListeners('log');
|
|
|
|
|
const logFnWrapper = listeners[0];
|
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
|
// Logs "log once" to the console and does not unbind the `once` event
|
2017-11-25 19:26:28 +01:00
|
|
|
|
logFnWrapper.listener();
|
|
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
|
// Logs "log once" to the console and removes the listener
|
2017-11-25 19:26:28 +01:00
|
|
|
|
logFnWrapper();
|
|
|
|
|
|
|
|
|
|
emitter.on('log', () => console.log('log persistently'));
|
2018-12-03 17:15:45 +01:00
|
|
|
|
// Will return a new Array with a single function bound by `.on()` above
|
2017-11-25 19:26:28 +01:00
|
|
|
|
const newListeners = emitter.rawListeners('log');
|
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// Logs "log persistently" twice
|
2017-11-25 19:26:28 +01:00
|
|
|
|
newListeners[0]();
|
|
|
|
|
emitter.emit('log');
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
### `emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])`
|
2019-05-19 13:55:18 +02:00
|
|
|
|
<!-- YAML
|
2019-12-13 17:02:36 +01:00
|
|
|
|
added: v13.4.0
|
2019-05-19 13:55:18 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
> Stability: 1 - captureRejections is experimental.
|
|
|
|
|
|
|
|
|
|
* `err` Error
|
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
|
* `...args` {any}
|
|
|
|
|
|
|
|
|
|
The `Symbol.for('nodejs.rejection')` method is called in case a
|
|
|
|
|
promise rejection happens when emitting an event and
|
|
|
|
|
[`captureRejections`][capturerejections] is enabled on the emitter.
|
|
|
|
|
It is possible to use [`events.captureRejectionSymbol`][rejectionsymbol] in
|
|
|
|
|
place of `Symbol.for('nodejs.rejection')`.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const { EventEmitter, captureRejectionSymbol } = require('events');
|
|
|
|
|
|
|
|
|
|
class MyClass extends EventEmitter {
|
|
|
|
|
constructor() {
|
|
|
|
|
super({ captureRejections: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[captureRejectionSymbol](err, event, ...args) {
|
|
|
|
|
console.log('rejection happened for', event, 'with', err, ...args);
|
|
|
|
|
this.destroy(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy(err) {
|
|
|
|
|
// Tear the resource down here.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
## `events.once(emitter, name)`
|
2019-02-13 13:30:28 +01:00
|
|
|
|
<!-- YAML
|
2019-03-27 22:43:03 +01:00
|
|
|
|
added: v11.13.0
|
2019-02-13 13:30:28 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2019-02-13 13:30:28 +01:00
|
|
|
|
* `emitter` {EventEmitter}
|
|
|
|
|
* `name` {string}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
2019-08-19 00:13:26 +02:00
|
|
|
|
Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
|
2019-02-13 13:30:28 +01:00
|
|
|
|
event or that is rejected when the `EventEmitter` emits `'error'`.
|
|
|
|
|
The `Promise` will resolve with an array of all the arguments emitted to the
|
|
|
|
|
given event.
|
|
|
|
|
|
2019-08-19 00:13:26 +02:00
|
|
|
|
This method is intentionally generic and works with the web platform
|
2019-09-23 15:57:03 +02:00
|
|
|
|
[EventTarget][WHATWG-EventTarget] interface, which has no special
|
2019-08-19 00:13:26 +02:00
|
|
|
|
`'error'` event semantics and does not listen to the `'error'` event.
|
|
|
|
|
|
2019-02-13 13:30:28 +01:00
|
|
|
|
```js
|
|
|
|
|
const { once, EventEmitter } = require('events');
|
|
|
|
|
|
|
|
|
|
async function run() {
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
|
ee.emit('myevent', 42);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [value] = await once(ee, 'myevent');
|
|
|
|
|
console.log(value);
|
|
|
|
|
|
|
|
|
|
const err = new Error('kaboom');
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
|
ee.emit('error', err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await once(ee, 'myevent');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log('error happened', err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run();
|
|
|
|
|
```
|
2019-09-23 15:57:03 +02:00
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
## `events.captureRejections`
|
2019-05-19 13:55:18 +02:00
|
|
|
|
<!-- YAML
|
2019-12-13 17:02:36 +01:00
|
|
|
|
added: v13.4.0
|
2019-05-19 13:55:18 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
> Stability: 1 - captureRejections is experimental.
|
|
|
|
|
|
|
|
|
|
Value: {boolean}
|
|
|
|
|
|
|
|
|
|
Change the default `captureRejections` option on all new `EventEmitter` objects.
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
## `events.captureRejectionSymbol`
|
2019-05-19 13:55:18 +02:00
|
|
|
|
<!-- YAML
|
2019-12-13 17:02:36 +01:00
|
|
|
|
added: v13.4.0
|
2019-05-19 13:55:18 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
> Stability: 1 - captureRejections is experimental.
|
|
|
|
|
|
|
|
|
|
Value: `Symbol.for('nodejs.rejection')`
|
|
|
|
|
|
|
|
|
|
See how to write a custom [rejection handler][rejection].
|
|
|
|
|
|
2019-12-24 06:35:16 +01:00
|
|
|
|
## `events.on(emitter, eventName)`
|
2019-05-30 17:58:55 +02:00
|
|
|
|
<!-- YAML
|
2020-01-07 08:33:44 +01:00
|
|
|
|
added: v13.6.0
|
2019-05-30 17:58:55 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `emitter` {EventEmitter}
|
|
|
|
|
* `eventName` {string|symbol} The name of the event being listened for
|
|
|
|
|
* Returns: {AsyncIterator} that iterates `eventName` events emitted by the `emitter`
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const { on, EventEmitter } = require('events');
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
// Emit later on
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
|
ee.emit('foo', 'bar');
|
|
|
|
|
ee.emit('foo', 42);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for await (const event of on(ee, 'foo')) {
|
|
|
|
|
// The execution of this inner block is synchronous and it
|
|
|
|
|
// processes one event at a time (even with await). Do not use
|
|
|
|
|
// if concurrent execution is required.
|
|
|
|
|
console.log(event); // prints ['bar'] [42]
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Returns an `AsyncIterator` that iterates `eventName` events. It will throw
|
|
|
|
|
if the `EventEmitter` emits `'error'`. It removes all listeners when
|
|
|
|
|
exiting the loop. The `value` returned by each iteration is an array
|
|
|
|
|
composed of the emitted event arguments.
|
|
|
|
|
|
2019-09-23 15:57:03 +02:00
|
|
|
|
[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`--trace-warnings`]: cli.html#cli_trace_warnings
|
2015-11-14 04:21:49 +01:00
|
|
|
|
[`EventEmitter.defaultMaxListeners`]: #events_eventemitter_defaultmaxlisteners
|
2015-12-29 19:04:13 +01:00
|
|
|
|
[`domain`]: domain.html
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`emitter.listenerCount()`]: #events_emitter_listenercount_eventname
|
2018-04-25 16:46:54 +02:00
|
|
|
|
[`emitter.removeListener()`]: #events_emitter_removelistener_eventname_listener
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`emitter.setMaxListeners(n)`]: #events_emitter_setmaxlisteners_n
|
|
|
|
|
[`fs.ReadStream`]: fs.html#fs_class_fs_readstream
|
|
|
|
|
[`net.Server`]: net.html#net_class_net_server
|
2016-08-27 10:45:34 +02:00
|
|
|
|
[`process.on('warning')`]: process.html#process_event_warning
|
2015-12-29 19:04:13 +01:00
|
|
|
|
[stream]: stream.html
|
2019-05-19 13:55:18 +02:00
|
|
|
|
[capturerejections]: #events_capture_rejections_of_promises
|
|
|
|
|
[rejection]: #events_emitter_symbol_for_nodejs_rejection_err_eventname_args
|
|
|
|
|
[rejectionsymbol]: #events_events_capturerejectionsymbol
|
|
|
|
|
[error]: #events_error_events
|