[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 | require('./common');
|
---|
| 23 | var assert = require('assert');
|
---|
| 24 | var events = require('../');
|
---|
| 25 | var util = require('util');
|
---|
| 26 |
|
---|
| 27 | function listener() {}
|
---|
| 28 | function listener2() {}
|
---|
| 29 | function listener3() {
|
---|
| 30 | return 0;
|
---|
| 31 | }
|
---|
| 32 | function listener4() {
|
---|
| 33 | return 1;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | function TestStream() {}
|
---|
| 37 | util.inherits(TestStream, events.EventEmitter);
|
---|
| 38 |
|
---|
| 39 | {
|
---|
| 40 | var ee = new events.EventEmitter();
|
---|
| 41 | ee.on('foo', listener);
|
---|
| 42 | var fooListeners = ee.listeners('foo');
|
---|
| 43 |
|
---|
| 44 | var listeners = ee.listeners('foo');
|
---|
| 45 | assert.ok(Array.isArray(listeners));
|
---|
| 46 | assert.strictEqual(listeners.length, 1);
|
---|
| 47 | assert.strictEqual(listeners[0], listener);
|
---|
| 48 |
|
---|
| 49 | ee.removeAllListeners('foo');
|
---|
| 50 | listeners = ee.listeners('foo');
|
---|
| 51 | assert.ok(Array.isArray(listeners));
|
---|
| 52 | assert.strictEqual(listeners.length, 0);
|
---|
| 53 |
|
---|
| 54 | assert.ok(Array.isArray(fooListeners));
|
---|
| 55 | assert.strictEqual(fooListeners.length, 1);
|
---|
| 56 | assert.strictEqual(fooListeners[0], listener);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | {
|
---|
| 60 | var ee = new events.EventEmitter();
|
---|
| 61 | ee.on('foo', listener);
|
---|
| 62 |
|
---|
| 63 | var eeListenersCopy = ee.listeners('foo');
|
---|
| 64 | assert.ok(Array.isArray(eeListenersCopy));
|
---|
| 65 | assert.strictEqual(eeListenersCopy.length, 1);
|
---|
| 66 | assert.strictEqual(eeListenersCopy[0], listener);
|
---|
| 67 |
|
---|
| 68 | var listeners = ee.listeners('foo');
|
---|
| 69 | assert.ok(Array.isArray(listeners));
|
---|
| 70 | assert.strictEqual(listeners.length, 1);
|
---|
| 71 | assert.strictEqual(listeners[0], listener);
|
---|
| 72 |
|
---|
| 73 | eeListenersCopy.push(listener2);
|
---|
| 74 | listeners = ee.listeners('foo');
|
---|
| 75 |
|
---|
| 76 | assert.ok(Array.isArray(listeners));
|
---|
| 77 | assert.strictEqual(listeners.length, 1);
|
---|
| 78 | assert.strictEqual(listeners[0], listener);
|
---|
| 79 |
|
---|
| 80 | assert.strictEqual(eeListenersCopy.length, 2);
|
---|
| 81 | assert.strictEqual(eeListenersCopy[0], listener);
|
---|
| 82 | assert.strictEqual(eeListenersCopy[1], listener2);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | {
|
---|
| 86 | var ee = new events.EventEmitter();
|
---|
| 87 | ee.on('foo', listener);
|
---|
| 88 | var eeListenersCopy = ee.listeners('foo');
|
---|
| 89 | ee.on('foo', listener2);
|
---|
| 90 |
|
---|
| 91 | var listeners = ee.listeners('foo');
|
---|
| 92 | assert.ok(Array.isArray(listeners));
|
---|
| 93 | assert.strictEqual(listeners.length, 2);
|
---|
| 94 | assert.strictEqual(listeners[0], listener);
|
---|
| 95 | assert.strictEqual(listeners[1], listener2);
|
---|
| 96 |
|
---|
| 97 | assert.ok(Array.isArray(eeListenersCopy));
|
---|
| 98 | assert.strictEqual(eeListenersCopy.length, 1);
|
---|
| 99 | assert.strictEqual(eeListenersCopy[0], listener);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | {
|
---|
| 103 | var ee = new events.EventEmitter();
|
---|
| 104 | ee.once('foo', listener);
|
---|
| 105 | var listeners = ee.listeners('foo');
|
---|
| 106 | assert.ok(Array.isArray(listeners));
|
---|
| 107 | assert.strictEqual(listeners.length, 1);
|
---|
| 108 | assert.strictEqual(listeners[0], listener);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | {
|
---|
| 112 | var ee = new events.EventEmitter();
|
---|
| 113 | ee.on('foo', listener);
|
---|
| 114 | ee.once('foo', listener2);
|
---|
| 115 |
|
---|
| 116 | var listeners = ee.listeners('foo');
|
---|
| 117 | assert.ok(Array.isArray(listeners));
|
---|
| 118 | assert.strictEqual(listeners.length, 2);
|
---|
| 119 | assert.strictEqual(listeners[0], listener);
|
---|
| 120 | assert.strictEqual(listeners[1], listener2);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | {
|
---|
| 124 | var ee = new events.EventEmitter();
|
---|
| 125 | ee._events = undefined;
|
---|
| 126 | var listeners = ee.listeners('foo');
|
---|
| 127 | assert.ok(Array.isArray(listeners));
|
---|
| 128 | assert.strictEqual(listeners.length, 0);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | {
|
---|
| 132 | var s = new TestStream();
|
---|
| 133 | var listeners = s.listeners('foo');
|
---|
| 134 | assert.ok(Array.isArray(listeners));
|
---|
| 135 | assert.strictEqual(listeners.length, 0);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | {
|
---|
| 140 | var ee = new events.EventEmitter();
|
---|
| 141 | ee.on('foo', listener);
|
---|
| 142 | var wrappedListener = ee.rawListeners('foo');
|
---|
| 143 | assert.strictEqual(wrappedListener.length, 1);
|
---|
| 144 | assert.strictEqual(wrappedListener[0], listener);
|
---|
| 145 | assert.notStrictEqual(wrappedListener, ee.rawListeners('foo'));
|
---|
| 146 | ee.once('foo', listener);
|
---|
| 147 | var wrappedListeners = ee.rawListeners('foo');
|
---|
| 148 | assert.strictEqual(wrappedListeners.length, 2);
|
---|
| 149 | assert.strictEqual(wrappedListeners[0], listener);
|
---|
| 150 | assert.notStrictEqual(wrappedListeners[1], listener);
|
---|
| 151 | assert.strictEqual(wrappedListeners[1].listener, listener);
|
---|
| 152 | assert.notStrictEqual(wrappedListeners, ee.rawListeners('foo'));
|
---|
| 153 | ee.emit('foo');
|
---|
| 154 | assert.strictEqual(wrappedListeners.length, 2);
|
---|
| 155 | assert.strictEqual(wrappedListeners[1].listener, listener);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | {
|
---|
| 159 | var ee = new events.EventEmitter();
|
---|
| 160 | ee.once('foo', listener3);
|
---|
| 161 | ee.on('foo', listener4);
|
---|
| 162 | var rawListeners = ee.rawListeners('foo');
|
---|
| 163 | assert.strictEqual(rawListeners.length, 2);
|
---|
| 164 | assert.strictEqual(rawListeners[0](), 0);
|
---|
| 165 | var rawListener = ee.rawListeners('foo');
|
---|
| 166 | assert.strictEqual(rawListener.length, 1);
|
---|
| 167 | assert.strictEqual(rawListener[0](), 1);
|
---|
| 168 | }
|
---|