Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
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 assert = require('assert');
23
var events = require('../');
24
25
var e = new events.EventEmitter();
26
27
// default
28
for (var i = 0; i < 10; i++) {
29
e.on('default', function() {});
30
}
31
assert.ok(!e._events['default'].hasOwnProperty('warned'));
32
e.on('default', function() {});
33
assert.ok(e._events['default'].warned);
34
35
// specific
36
e.setMaxListeners(5);
37
for (var i = 0; i < 5; i++) {
38
e.on('specific', function() {});
39
}
40
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
41
e.on('specific', function() {});
42
assert.ok(e._events['specific'].warned);
43
44
// only one
45
e.setMaxListeners(1);
46
e.on('only one', function() {});
47
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
48
e.on('only one', function() {});
49
assert.ok(e._events['only one'].hasOwnProperty('warned'));
50
51
// unlimited
52
e.setMaxListeners(0);
53
for (var i = 0; i < 1000; i++) {
54
e.on('unlimited', function() {});
55
}
56
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
57
58
// process-wide
59
events.EventEmitter.defaultMaxListeners = 42;
60
e = new events.EventEmitter();
61
62
for (var i = 0; i < 42; ++i) {
63
e.on('fortytwo', function() {});
64
}
65
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
66
e.on('fortytwo', function() {});
67
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
68
delete e._events['fortytwo'].warned;
69
70
events.EventEmitter.defaultMaxListeners = 44;
71
e.on('fortytwo', function() {});
72
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
73
e.on('fortytwo', function() {});
74
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
75
76
// but _maxListeners still has precedence over defaultMaxListeners
77
events.EventEmitter.defaultMaxListeners = 42;
78
e = new events.EventEmitter();
79
e.setMaxListeners(1);
80
e.on('uno', function() {});
81
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
82
e.on('uno', function() {});
83
assert.ok(e._events['uno'].hasOwnProperty('warned'));
84
85
// chainable
86
assert.strictEqual(e, e.setMaxListeners(1));
87
88