[6a3a178] | 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 | var common = require('./common');
|
---|
| 23 | var assert = require('assert');
|
---|
| 24 | var EventEmitter = require('../');
|
---|
| 25 |
|
---|
| 26 | var listener1 = function listener1() {};
|
---|
| 27 | var listener2 = function listener2() {};
|
---|
| 28 |
|
---|
| 29 | {
|
---|
| 30 | var ee = new EventEmitter();
|
---|
| 31 | ee.on('hello', listener1);
|
---|
| 32 | ee.on('removeListener', common.mustCall(function(name, cb) {
|
---|
| 33 | assert.strictEqual(name, 'hello');
|
---|
| 34 | assert.strictEqual(cb, listener1);
|
---|
| 35 | }));
|
---|
| 36 | ee.removeListener('hello', listener1);
|
---|
| 37 | var listeners = ee.listeners('hello');
|
---|
| 38 | assert.ok(Array.isArray(listeners));
|
---|
| 39 | assert.strictEqual(listeners.length, 0);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | {
|
---|
| 43 | var ee = new EventEmitter();
|
---|
| 44 | ee.on('hello', listener1);
|
---|
| 45 | ee.on('removeListener', common.mustNotCall());
|
---|
| 46 | ee.removeListener('hello', listener2);
|
---|
| 47 |
|
---|
| 48 | var listeners = ee.listeners('hello');
|
---|
| 49 | assert.ok(Array.isArray(listeners));
|
---|
| 50 | assert.strictEqual(listeners.length, 1);
|
---|
| 51 | assert.strictEqual(listeners[0], listener1);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | {
|
---|
| 55 | var ee = new EventEmitter();
|
---|
| 56 | ee.on('hello', listener1);
|
---|
| 57 | ee.on('hello', listener2);
|
---|
| 58 |
|
---|
| 59 | var listeners;
|
---|
| 60 | ee.once('removeListener', common.mustCall(function(name, cb) {
|
---|
| 61 | assert.strictEqual(name, 'hello');
|
---|
| 62 | assert.strictEqual(cb, listener1);
|
---|
| 63 | listeners = ee.listeners('hello');
|
---|
| 64 | assert.ok(Array.isArray(listeners));
|
---|
| 65 | assert.strictEqual(listeners.length, 1);
|
---|
| 66 | assert.strictEqual(listeners[0], listener2);
|
---|
| 67 | }));
|
---|
| 68 | ee.removeListener('hello', listener1);
|
---|
| 69 | listeners = ee.listeners('hello');
|
---|
| 70 | assert.ok(Array.isArray(listeners));
|
---|
| 71 | assert.strictEqual(listeners.length, 1);
|
---|
| 72 | assert.strictEqual(listeners[0], listener2);
|
---|
| 73 | ee.once('removeListener', common.mustCall(function(name, cb) {
|
---|
| 74 | assert.strictEqual(name, 'hello');
|
---|
| 75 | assert.strictEqual(cb, listener2);
|
---|
| 76 | listeners = ee.listeners('hello');
|
---|
| 77 | assert.ok(Array.isArray(listeners));
|
---|
| 78 | assert.strictEqual(listeners.length, 0);
|
---|
| 79 | }));
|
---|
| 80 | ee.removeListener('hello', listener2);
|
---|
| 81 | listeners = ee.listeners('hello');
|
---|
| 82 | assert.ok(Array.isArray(listeners));
|
---|
| 83 | assert.strictEqual(listeners.length, 0);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | {
|
---|
| 87 | var ee = new EventEmitter();
|
---|
| 88 |
|
---|
| 89 | function remove1() {
|
---|
| 90 | assert.fail('remove1 should not have been called');
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | function remove2() {
|
---|
| 94 | assert.fail('remove2 should not have been called');
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | ee.on('removeListener', common.mustCall(function(name, cb) {
|
---|
| 98 | if (cb !== remove1) return;
|
---|
| 99 | this.removeListener('quux', remove2);
|
---|
| 100 | this.emit('quux');
|
---|
| 101 | }, 2));
|
---|
| 102 | ee.on('quux', remove1);
|
---|
| 103 | ee.on('quux', remove2);
|
---|
| 104 | ee.removeListener('quux', remove1);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | {
|
---|
| 108 | var ee = new EventEmitter();
|
---|
| 109 | ee.on('hello', listener1);
|
---|
| 110 | ee.on('hello', listener2);
|
---|
| 111 |
|
---|
| 112 | var listeners;
|
---|
| 113 | ee.once('removeListener', common.mustCall(function(name, cb) {
|
---|
| 114 | assert.strictEqual(name, 'hello');
|
---|
| 115 | assert.strictEqual(cb, listener1);
|
---|
| 116 | listeners = ee.listeners('hello');
|
---|
| 117 | assert.ok(Array.isArray(listeners));
|
---|
| 118 | assert.strictEqual(listeners.length, 1);
|
---|
| 119 | assert.strictEqual(listeners[0], listener2);
|
---|
| 120 | ee.once('removeListener', common.mustCall(function(name, cb) {
|
---|
| 121 | assert.strictEqual(name, 'hello');
|
---|
| 122 | assert.strictEqual(cb, listener2);
|
---|
| 123 | listeners = ee.listeners('hello');
|
---|
| 124 | assert.ok(Array.isArray(listeners));
|
---|
| 125 | assert.strictEqual(listeners.length, 0);
|
---|
| 126 | }));
|
---|
| 127 | ee.removeListener('hello', listener2);
|
---|
| 128 | listeners = ee.listeners('hello');
|
---|
| 129 | assert.ok(Array.isArray(listeners));
|
---|
| 130 | assert.strictEqual(listeners.length, 0);
|
---|
| 131 | }));
|
---|
| 132 | ee.removeListener('hello', listener1);
|
---|
| 133 | listeners = ee.listeners('hello');
|
---|
| 134 | assert.ok(Array.isArray(listeners));
|
---|
| 135 | assert.strictEqual(listeners.length, 0);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | {
|
---|
| 139 | var ee = new EventEmitter();
|
---|
| 140 | var listener3 = common.mustCall(function() {
|
---|
| 141 | ee.removeListener('hello', listener4);
|
---|
| 142 | }, 2);
|
---|
| 143 | var listener4 = common.mustCall();
|
---|
| 144 |
|
---|
| 145 | ee.on('hello', listener3);
|
---|
| 146 | ee.on('hello', listener4);
|
---|
| 147 |
|
---|
| 148 | // listener4 will still be called although it is removed by listener 3.
|
---|
| 149 | ee.emit('hello');
|
---|
| 150 | // This is so because the interal listener array at time of emit
|
---|
| 151 | // was [listener3,listener4]
|
---|
| 152 |
|
---|
| 153 | // Interal listener array [listener3]
|
---|
| 154 | ee.emit('hello');
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | {
|
---|
| 158 | var ee = new EventEmitter();
|
---|
| 159 |
|
---|
| 160 | ee.once('hello', listener1);
|
---|
| 161 | ee.on('removeListener', common.mustCall(function(eventName, listener) {
|
---|
| 162 | assert.strictEqual(eventName, 'hello');
|
---|
| 163 | assert.strictEqual(listener, listener1);
|
---|
| 164 | }));
|
---|
| 165 | ee.emit('hello');
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | {
|
---|
| 169 | var ee = new EventEmitter();
|
---|
| 170 |
|
---|
| 171 | assert.strictEqual(ee, ee.removeListener('foo', function() {}));
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | // Verify that the removed listener must be a function
|
---|
| 175 | assert.throws(function() {
|
---|
| 176 | var ee = new EventEmitter();
|
---|
| 177 |
|
---|
| 178 | ee.removeListener('foo', null);
|
---|
| 179 | }, /^TypeError: The "listener" argument must be of type Function\. Received type object$/);
|
---|
| 180 |
|
---|
| 181 | {
|
---|
| 182 | var ee = new EventEmitter();
|
---|
| 183 | var listener = function() {};
|
---|
| 184 | ee._events = undefined;
|
---|
| 185 | var e = ee.removeListener('foo', listener);
|
---|
| 186 | assert.strictEqual(e, ee);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | {
|
---|
| 190 | var ee = new EventEmitter();
|
---|
| 191 |
|
---|
| 192 | ee.on('foo', listener1);
|
---|
| 193 | ee.on('foo', listener2);
|
---|
| 194 | var listeners = ee.listeners('foo');
|
---|
| 195 | assert.ok(Array.isArray(listeners));
|
---|
| 196 | assert.strictEqual(listeners.length, 2);
|
---|
| 197 | assert.strictEqual(listeners[0], listener1);
|
---|
| 198 | assert.strictEqual(listeners[1], listener2);
|
---|
| 199 |
|
---|
| 200 | ee.removeListener('foo', listener1);
|
---|
| 201 | assert.strictEqual(ee._events.foo, listener2);
|
---|
| 202 |
|
---|
| 203 | ee.on('foo', listener1);
|
---|
| 204 | listeners = ee.listeners('foo');
|
---|
| 205 | assert.ok(Array.isArray(listeners));
|
---|
| 206 | assert.strictEqual(listeners.length, 2);
|
---|
| 207 | assert.strictEqual(listeners[0], listener2);
|
---|
| 208 | assert.strictEqual(listeners[1], listener1);
|
---|
| 209 |
|
---|
| 210 | ee.removeListener('foo', listener1);
|
---|
| 211 | assert.strictEqual(ee._events.foo, listener2);
|
---|
| 212 | }
|
---|