| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f6ee099 commit 2a442b3
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -377,6 +377,43 @@ listener array. If any single listener has been added multiple times to the | |||
| 377 | 377 | listener array for the specified `event`, then `removeListener` must be called | |
| 378 | 378 | multiple times to remove each instance. | |
| 379 | 379 | ||
| 380 | + Note that once an event has been emitted, all listeners attached to it at the | ||
| 381 | + time of emitting will be called in order. This implies that any `removeListener()` | ||
| 382 | + or `removeAllListeners()` calls *after* emitting and *before* the last listener | ||
| 383 | + finishes execution will not remove them from `emit()` in progress. Subsequent | ||
| 384 | + events will behave as expected. | ||
| 385 | + | ||
| 386 | + ```js | ||
| 387 | + const myEmitter = new MyEmitter(); | ||
| 388 | + | ||
| 389 | + var callbackA = () => { | ||
| 390 | + console.log('A'); | ||
| 391 | + myEmitter.removeListener('event', callbackB); | ||
| 392 | + }; | ||
| 393 | + | ||
| 394 | + var callbackB = () => { | ||
| 395 | + console.log('B'); | ||
| 396 | + }; | ||
| 397 | + | ||
| 398 | + myEmitter.on('event', callbackA); | ||
| 399 | + | ||
| 400 | + myEmitter.on('event', callbackB); | ||
| 401 | + | ||
| 402 | + // callbackA removes listener callbackB but it will still be called. | ||
| 403 | + // Interal listener array at time of emit [callbackA, callbackB] | ||
| 404 | + myEmitter.emit('event'); | ||
| 405 | + // Prints: | ||
| 406 | + // A | ||
| 407 | + // B | ||
| 408 | + | ||
| 409 | + // callbackB is now removed. | ||
| 410 | + // Interal listener array [callbackA] | ||
| 411 | + myEmitter.emit('event'); | ||
| 412 | + // Prints: | ||
| 413 | + // A | ||
| 414 | + | ||
| 415 | + ``` | ||
| 416 | + | ||
| 380 | 417 | Because listeners are managed using an internal array, calling this will | |
| 381 | 418 | change the position indices of any listener registered *after* the listener | |
| 382 | 419 | being removed. This will not impact the order in which listeners are called, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,3 +83,22 @@ e5.once('removeListener', common.mustCall(function(name, cb) { | |||
| 83 | 83 | })); | |
| 84 | 84 | e5.removeListener('hello', listener1); | |
| 85 | 85 | assert.deepEqual([], e5.listeners('hello')); | |
| 86 | + | ||
| 87 | + const e6 = new events.EventEmitter(); | ||
| 88 | + | ||
| 89 | + const listener3 = common.mustCall(() => { | ||
| 90 | + e6.removeListener('hello', listener4); | ||
| 91 | + }, 2); | ||
| 92 | + | ||
| 93 | + const listener4 = common.mustCall(() => {}, 1); | ||
| 94 | + | ||
| 95 | + e6.on('hello', listener3); | ||
| 96 | + e6.on('hello', listener4); | ||
| 97 | + | ||
| 98 | + // listener4 will still be called although it is removed by listener 3. | ||
| 99 | + e6.emit('hello'); | ||
| 100 | + // This is so because the interal listener array at time of emit | ||
| 101 | + // was [listener3,listener4] | ||
| 102 | + | ||
| 103 | + // Interal listener array [listener3] | ||
| 104 | + e6.emit('hello'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments