1 |
3a515b92
|
cagy
|
// 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 e = new EventEmitter();
|
27 |
|
|
|
28 |
|
|
e.once('hello', common.mustCall());
|
29 |
|
|
|
30 |
|
|
e.emit('hello', 'a', 'b');
|
31 |
|
|
e.emit('hello', 'a', 'b');
|
32 |
|
|
e.emit('hello', 'a', 'b');
|
33 |
|
|
e.emit('hello', 'a', 'b');
|
34 |
|
|
|
35 |
|
|
function remove() {
|
36 |
|
|
assert.fail('once->foo should not be emitted');
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
e.once('foo', remove);
|
40 |
|
|
e.removeListener('foo', remove);
|
41 |
|
|
e.emit('foo');
|
42 |
|
|
|
43 |
|
|
e.once('e', common.mustCall(function() {
|
44 |
|
|
e.emit('e');
|
45 |
|
|
}));
|
46 |
|
|
|
47 |
|
|
e.once('e', common.mustCall());
|
48 |
|
|
|
49 |
|
|
e.emit('e');
|
50 |
|
|
|
51 |
|
|
// Verify that the listener must be a function
|
52 |
|
|
assert.throws(function() {
|
53 |
|
|
var ee = new EventEmitter();
|
54 |
|
|
|
55 |
|
|
ee.once('foo', null);
|
56 |
|
|
}, /^TypeError: The "listener" argument must be of type Function. Received type object$/);
|
57 |
|
|
|
58 |
|
|
{
|
59 |
|
|
// once() has different code paths based on the number of arguments being
|
60 |
|
|
// emitted. Verify that all of the cases are covered.
|
61 |
|
|
var maxArgs = 4;
|
62 |
|
|
|
63 |
|
|
for (var i = 0; i <= maxArgs; ++i) {
|
64 |
|
|
var ee = new EventEmitter();
|
65 |
|
|
var args = ['foo'];
|
66 |
|
|
|
67 |
|
|
for (var j = 0; j < i; ++j)
|
68 |
|
|
args.push(j);
|
69 |
|
|
|
70 |
|
|
ee.once('foo', common.mustCall(function() {
|
71 |
|
|
var params = Array.prototype.slice.call(arguments);
|
72 |
|
|
var restArgs = args.slice(1);
|
73 |
|
|
assert.ok(Array.isArray(params));
|
74 |
|
|
assert.strictEqual(params.length, restArgs.length);
|
75 |
|
|
for (var index = 0; index < params.length; index++) {
|
76 |
|
|
var param = params[index];
|
77 |
|
|
assert.strictEqual(param, restArgs[index]);
|
78 |
|
|
}
|
79 |
|
|
}));
|
80 |
|
|
|
81 |
|
|
EventEmitter.prototype.emit.apply(ee, args);
|
82 |
|
|
}
|
83 |
|
|
}
|