// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021var assert = require('assert');22var events = require('../');2324var callbacks_called = [];2526var e = new events.EventEmitter();2728function callback1() {29callbacks_called.push('callback1');30e.on('foo', callback2);31e.on('foo', callback3);32e.removeListener('foo', callback1);33}3435function callback2() {36callbacks_called.push('callback2');37e.removeListener('foo', callback2);38}3940function callback3() {41callbacks_called.push('callback3');42e.removeListener('foo', callback3);43}4445e.on('foo', callback1);46assert.equal(1, e.listeners('foo').length);4748e.emit('foo');49assert.equal(2, e.listeners('foo').length);50assert.deepEqual(['callback1'], callbacks_called);5152e.emit('foo');53assert.equal(0, e.listeners('foo').length);54assert.deepEqual(['callback1', 'callback2', 'callback3'], callbacks_called);5556e.emit('foo');57assert.equal(0, e.listeners('foo').length);58assert.deepEqual(['callback1', 'callback2', 'callback3'], callbacks_called);5960e.on('foo', callback1);61e.on('foo', callback2);62assert.equal(2, e.listeners('foo').length);63e.removeAllListeners('foo');64assert.equal(0, e.listeners('foo').length);6566// Verify that removing callbacks while in emit allows emits to propagate to67// all listeners68callbacks_called = [];6970e.on('foo', callback2);71e.on('foo', callback3);72assert.equal(2, e.listeners('foo').length);73e.emit('foo');74assert.deepEqual(['callback2', 'callback3'], callbacks_called);75assert.equal(0, e.listeners('foo').length);767778