source: trip-planner-front/node_modules/events/tests/modify-in-emit.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[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
22require('./common');
23var assert = require('assert');
24var events = require('../');
25
26var callbacks_called = [];
27
28var e = new events.EventEmitter();
29
30function callback1() {
31 callbacks_called.push('callback1');
32 e.on('foo', callback2);
33 e.on('foo', callback3);
34 e.removeListener('foo', callback1);
35}
36
37function callback2() {
38 callbacks_called.push('callback2');
39 e.removeListener('foo', callback2);
40}
41
42function callback3() {
43 callbacks_called.push('callback3');
44 e.removeListener('foo', callback3);
45}
46
47e.on('foo', callback1);
48assert.strictEqual(e.listeners('foo').length, 1);
49
50e.emit('foo');
51assert.strictEqual(e.listeners('foo').length, 2);
52assert.ok(Array.isArray(callbacks_called));
53assert.strictEqual(callbacks_called.length, 1);
54assert.strictEqual(callbacks_called[0], 'callback1');
55
56e.emit('foo');
57assert.strictEqual(e.listeners('foo').length, 0);
58assert.ok(Array.isArray(callbacks_called));
59assert.strictEqual(callbacks_called.length, 3);
60assert.strictEqual(callbacks_called[0], 'callback1');
61assert.strictEqual(callbacks_called[1], 'callback2');
62assert.strictEqual(callbacks_called[2], 'callback3');
63
64e.emit('foo');
65assert.strictEqual(e.listeners('foo').length, 0);
66assert.ok(Array.isArray(callbacks_called));
67assert.strictEqual(callbacks_called.length, 3);
68assert.strictEqual(callbacks_called[0], 'callback1');
69assert.strictEqual(callbacks_called[1], 'callback2');
70assert.strictEqual(callbacks_called[2], 'callback3');
71
72e.on('foo', callback1);
73e.on('foo', callback2);
74assert.strictEqual(e.listeners('foo').length, 2);
75e.removeAllListeners('foo');
76assert.strictEqual(e.listeners('foo').length, 0);
77
78// Verify that removing callbacks while in emit allows emits to propagate to
79// all listeners
80callbacks_called = [];
81
82e.on('foo', callback2);
83e.on('foo', callback3);
84assert.strictEqual(2, e.listeners('foo').length);
85e.emit('foo');
86assert.ok(Array.isArray(callbacks_called));
87assert.strictEqual(callbacks_called.length, 2);
88assert.strictEqual(callbacks_called[0], 'callback2');
89assert.strictEqual(callbacks_called[1], 'callback3');
90assert.strictEqual(0, e.listeners('foo').length);
Note: See TracBrowser for help on using the repository browser.