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 common = require('./common');
23
var assert = require('assert');
24
var events = require('../');
25
26
var count = 0;
27
28
function listener1() {
29
console.log('listener1');
30
count++;
31
}
32
33
function listener2() {
34
console.log('listener2');
35
count++;
36
}
37
38
function listener3() {
39
console.log('listener3');
40
count++;
41
}
42
43
function remove1() {
44
assert(0);
45
}
46
47
function remove2() {
48
assert(0);
49
}
50
51
var e1 = new events.EventEmitter();
52
e1.on('hello', listener1);
53
e1.on('removeListener', common.mustCall(function(name, cb) {
54
assert.equal(name, 'hello');
55
assert.equal(cb, listener1);
56
}));
57
e1.removeListener('hello', listener1);
58
assert.deepEqual([], e1.listeners('hello'));
59
60
var e2 = new events.EventEmitter();
61
e2.on('hello', listener1);
62
e2.on('removeListener', assert.fail);
63
e2.removeListener('hello', listener2);
64
assert.deepEqual([listener1], e2.listeners('hello'));
65
66
var e3 = new events.EventEmitter();
67
e3.on('hello', listener1);
68
e3.on('hello', listener2);
69
e3.on('removeListener', common.mustCall(function(name, cb) {
70
assert.equal(name, 'hello');
71
assert.equal(cb, listener1);
72
}));
73
e3.removeListener('hello', listener1);
74
assert.deepEqual([listener2], e3.listeners('hello'));
75
76
var e4 = new events.EventEmitter();
77
e4.on('removeListener', common.mustCall(function(name, cb) {
78
if (cb !== remove1) return;
79
this.removeListener('quux', remove2);
80
this.emit('quux');
81
}, 2));
82
e4.on('quux', remove1);
83
e4.on('quux', remove2);
84
e4.removeListener('quux', remove1);
85
86