[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 | {
|
---|
| 27 | var ee = new EventEmitter();
|
---|
| 28 | var events_new_listener_emitted = [];
|
---|
| 29 | var listeners_new_listener_emitted = [];
|
---|
| 30 |
|
---|
| 31 | // Sanity check
|
---|
| 32 | assert.strictEqual(ee.addListener, ee.on);
|
---|
| 33 |
|
---|
| 34 | ee.on('newListener', function(event, listener) {
|
---|
| 35 | // Don't track newListener listeners.
|
---|
| 36 | if (event === 'newListener')
|
---|
| 37 | return;
|
---|
| 38 |
|
---|
| 39 | events_new_listener_emitted.push(event);
|
---|
| 40 | listeners_new_listener_emitted.push(listener);
|
---|
| 41 | });
|
---|
| 42 |
|
---|
| 43 | var hello = common.mustCall(function(a, b) {
|
---|
| 44 | assert.strictEqual('a', a);
|
---|
| 45 | assert.strictEqual('b', b);
|
---|
| 46 | });
|
---|
| 47 |
|
---|
| 48 | ee.once('newListener', function(name, listener) {
|
---|
| 49 | assert.strictEqual(name, 'hello');
|
---|
| 50 | assert.strictEqual(listener, hello);
|
---|
| 51 |
|
---|
| 52 | var listeners = this.listeners('hello');
|
---|
| 53 | assert.ok(Array.isArray(listeners));
|
---|
| 54 | assert.strictEqual(listeners.length, 0);
|
---|
| 55 | });
|
---|
| 56 |
|
---|
| 57 | ee.on('hello', hello);
|
---|
| 58 | ee.once('foo', assert.fail);
|
---|
| 59 |
|
---|
| 60 | assert.ok(Array.isArray(events_new_listener_emitted));
|
---|
| 61 | assert.strictEqual(events_new_listener_emitted.length, 2);
|
---|
| 62 | assert.strictEqual(events_new_listener_emitted[0], 'hello');
|
---|
| 63 | assert.strictEqual(events_new_listener_emitted[1], 'foo');
|
---|
| 64 |
|
---|
| 65 | assert.ok(Array.isArray(listeners_new_listener_emitted));
|
---|
| 66 | assert.strictEqual(listeners_new_listener_emitted.length, 2);
|
---|
| 67 | assert.strictEqual(listeners_new_listener_emitted[0], hello);
|
---|
| 68 | assert.strictEqual(listeners_new_listener_emitted[1], assert.fail);
|
---|
| 69 |
|
---|
| 70 | ee.emit('hello', 'a', 'b');
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // just make sure that this doesn't throw:
|
---|
| 74 | {
|
---|
| 75 | var f = new EventEmitter();
|
---|
| 76 |
|
---|
| 77 | f.setMaxListeners(0);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | {
|
---|
| 81 | var listen1 = function() {};
|
---|
| 82 | var listen2 = function() {};
|
---|
| 83 | var ee = new EventEmitter();
|
---|
| 84 |
|
---|
| 85 | ee.once('newListener', function() {
|
---|
| 86 | var listeners = ee.listeners('hello');
|
---|
| 87 | assert.ok(Array.isArray(listeners));
|
---|
| 88 | assert.strictEqual(listeners.length, 0);
|
---|
| 89 | ee.once('newListener', function() {
|
---|
| 90 | var listeners = ee.listeners('hello');
|
---|
| 91 | assert.ok(Array.isArray(listeners));
|
---|
| 92 | assert.strictEqual(listeners.length, 0);
|
---|
| 93 | });
|
---|
| 94 | ee.on('hello', listen2);
|
---|
| 95 | });
|
---|
| 96 | ee.on('hello', listen1);
|
---|
| 97 | // The order of listeners on an event is not always the order in which the
|
---|
| 98 | // listeners were added.
|
---|
| 99 | var listeners = ee.listeners('hello');
|
---|
| 100 | assert.ok(Array.isArray(listeners));
|
---|
| 101 | assert.strictEqual(listeners.length, 2);
|
---|
| 102 | assert.strictEqual(listeners[0], listen2);
|
---|
| 103 | assert.strictEqual(listeners[1], listen1);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | // Verify that the listener must be a function
|
---|
| 107 | assert.throws(function() {
|
---|
| 108 | var ee = new EventEmitter();
|
---|
| 109 |
|
---|
| 110 | ee.on('foo', null);
|
---|
| 111 | }, /^TypeError: The "listener" argument must be of type Function. Received type object$/);
|
---|