| 1 | // Copyright Joyent, Inc. and other Node contributors.
|
|---|
| 2 | //
|
|---|
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a
|
|---|
| 4 | // copy of this software and associated documentation files (the
|
|---|
| 5 | // "Software"), to deal in the Software without restriction, including
|
|---|
| 6 | // without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit
|
|---|
| 8 | // persons to whom the Software is furnished to do so, subject to the
|
|---|
| 9 | // following conditions:
|
|---|
| 10 | //
|
|---|
| 11 | // The above copyright notice and this permission notice shall be included
|
|---|
| 12 | // in all copies or substantial portions of the Software.
|
|---|
| 13 | //
|
|---|
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|---|
| 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|---|
| 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|---|
| 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|---|
| 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|---|
| 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 21 |
|
|---|
| 22 | require('./common');
|
|---|
| 23 | var assert = require('assert');
|
|---|
| 24 | var events = require('../');
|
|---|
| 25 |
|
|---|
| 26 | var callbacks_called = [];
|
|---|
| 27 |
|
|---|
| 28 | var e = new events.EventEmitter();
|
|---|
| 29 |
|
|---|
| 30 | function callback1() {
|
|---|
| 31 | callbacks_called.push('callback1');
|
|---|
| 32 | e.on('foo', callback2);
|
|---|
| 33 | e.on('foo', callback3);
|
|---|
| 34 | e.removeListener('foo', callback1);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | function callback2() {
|
|---|
| 38 | callbacks_called.push('callback2');
|
|---|
| 39 | e.removeListener('foo', callback2);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | function callback3() {
|
|---|
| 43 | callbacks_called.push('callback3');
|
|---|
| 44 | e.removeListener('foo', callback3);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | e.on('foo', callback1);
|
|---|
| 48 | assert.strictEqual(e.listeners('foo').length, 1);
|
|---|
| 49 |
|
|---|
| 50 | e.emit('foo');
|
|---|
| 51 | assert.strictEqual(e.listeners('foo').length, 2);
|
|---|
| 52 | assert.ok(Array.isArray(callbacks_called));
|
|---|
| 53 | assert.strictEqual(callbacks_called.length, 1);
|
|---|
| 54 | assert.strictEqual(callbacks_called[0], 'callback1');
|
|---|
| 55 |
|
|---|
| 56 | e.emit('foo');
|
|---|
| 57 | assert.strictEqual(e.listeners('foo').length, 0);
|
|---|
| 58 | assert.ok(Array.isArray(callbacks_called));
|
|---|
| 59 | assert.strictEqual(callbacks_called.length, 3);
|
|---|
| 60 | assert.strictEqual(callbacks_called[0], 'callback1');
|
|---|
| 61 | assert.strictEqual(callbacks_called[1], 'callback2');
|
|---|
| 62 | assert.strictEqual(callbacks_called[2], 'callback3');
|
|---|
| 63 |
|
|---|
| 64 | e.emit('foo');
|
|---|
| 65 | assert.strictEqual(e.listeners('foo').length, 0);
|
|---|
| 66 | assert.ok(Array.isArray(callbacks_called));
|
|---|
| 67 | assert.strictEqual(callbacks_called.length, 3);
|
|---|
| 68 | assert.strictEqual(callbacks_called[0], 'callback1');
|
|---|
| 69 | assert.strictEqual(callbacks_called[1], 'callback2');
|
|---|
| 70 | assert.strictEqual(callbacks_called[2], 'callback3');
|
|---|
| 71 |
|
|---|
| 72 | e.on('foo', callback1);
|
|---|
| 73 | e.on('foo', callback2);
|
|---|
| 74 | assert.strictEqual(e.listeners('foo').length, 2);
|
|---|
| 75 | e.removeAllListeners('foo');
|
|---|
| 76 | assert.strictEqual(e.listeners('foo').length, 0);
|
|---|
| 77 |
|
|---|
| 78 | // Verify that removing callbacks while in emit allows emits to propagate to
|
|---|
| 79 | // all listeners
|
|---|
| 80 | callbacks_called = [];
|
|---|
| 81 |
|
|---|
| 82 | e.on('foo', callback2);
|
|---|
| 83 | e.on('foo', callback3);
|
|---|
| 84 | assert.strictEqual(2, e.listeners('foo').length);
|
|---|
| 85 | e.emit('foo');
|
|---|
| 86 | assert.ok(Array.isArray(callbacks_called));
|
|---|
| 87 | assert.strictEqual(callbacks_called.length, 2);
|
|---|
| 88 | assert.strictEqual(callbacks_called[0], 'callback2');
|
|---|
| 89 | assert.strictEqual(callbacks_called[1], 'callback3');
|
|---|
| 90 | assert.strictEqual(0, e.listeners('foo').length);
|
|---|