events-once.js
6.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
var common = require('./common');
var EventEmitter = require('../').EventEmitter;
var once = require('../').once;
var has = require('has');
var assert = require('assert');
function Event(type) {
this.type = type;
}
function EventTargetMock() {
this.events = {};
this.addEventListener = common.mustCall(this.addEventListener);
this.removeEventListener = common.mustCall(this.removeEventListener);
}
EventTargetMock.prototype.addEventListener = function addEventListener(name, listener, options) {
if (!(name in this.events)) {
this.events[name] = { listeners: [], options: options || {} }
}
this.events[name].listeners.push(listener);
};
EventTargetMock.prototype.removeEventListener = function removeEventListener(name, callback) {
if (!(name in this.events)) {
return;
}
var event = this.events[name];
var stack = event.listeners;
for (var i = 0, l = stack.length; i < l; i++) {
if (stack[i] === callback) {
stack.splice(i, 1);
if (stack.length === 0) {
delete this.events[name];
}
return;
}
}
};
EventTargetMock.prototype.dispatchEvent = function dispatchEvent(arg) {
if (!(arg.type in this.events)) {
return true;
}
var event = this.events[arg.type];
var stack = event.listeners.slice();
for (var i = 0, l = stack.length; i < l; i++) {
stack[i].call(null, arg);
if (event.options.once) {
this.removeEventListener(arg.type, stack[i]);
}
}
return !arg.defaultPrevented;
};
function onceAnEvent() {
var ee = new EventEmitter();
process.nextTick(function () {
ee.emit('myevent', 42);
});
return once(ee, 'myevent').then(function (args) {
var value = args[0]
assert.strictEqual(value, 42);
assert.strictEqual(ee.listenerCount('error'), 0);
assert.strictEqual(ee.listenerCount('myevent'), 0);
});
}
function onceAnEventWithTwoArgs() {
var ee = new EventEmitter();
process.nextTick(function () {
ee.emit('myevent', 42, 24);
});
return once(ee, 'myevent').then(function (value) {
assert.strictEqual(value.length, 2);
assert.strictEqual(value[0], 42);
assert.strictEqual(value[1], 24);
});
}
function catchesErrors() {
var ee = new EventEmitter();
var expected = new Error('kaboom');
var err;
process.nextTick(function () {
ee.emit('error', expected);
});
return once(ee, 'myevent').then(function () {
throw new Error('should reject')
}, function (err) {
assert.strictEqual(err, expected);
assert.strictEqual(ee.listenerCount('error'), 0);
assert.strictEqual(ee.listenerCount('myevent'), 0);
});
}
function stopListeningAfterCatchingError() {
var ee = new EventEmitter();
var expected = new Error('kaboom');
var err;
process.nextTick(function () {
ee.emit('error', expected);
ee.emit('myevent', 42, 24);
});
// process.on('multipleResolves', common.mustNotCall());
return once(ee, 'myevent').then(common.mustNotCall, function (err) {
// process.removeAllListeners('multipleResolves');
assert.strictEqual(err, expected);
assert.strictEqual(ee.listenerCount('error'), 0);
assert.strictEqual(ee.listenerCount('myevent'), 0);
});
}
function onceError() {
var ee = new EventEmitter();
var expected = new Error('kaboom');
process.nextTick(function () {
ee.emit('error', expected);
});
var promise = once(ee, 'error');
assert.strictEqual(ee.listenerCount('error'), 1);
return promise.then(function (args) {
var err = args[0]
assert.strictEqual(err, expected);
assert.strictEqual(ee.listenerCount('error'), 0);
assert.strictEqual(ee.listenerCount('myevent'), 0);
});
}
function onceWithEventTarget() {
var et = new EventTargetMock();
var event = new Event('myevent');
process.nextTick(function () {
et.dispatchEvent(event);
});
return once(et, 'myevent').then(function (args) {
var value = args[0];
assert.strictEqual(value, event);
assert.strictEqual(has(et.events, 'myevent'), false);
});
}
function onceWithEventTargetError() {
var et = new EventTargetMock();
var error = new Event('error');
process.nextTick(function () {
et.dispatchEvent(error);
});
return once(et, 'error').then(function (args) {
var err = args[0];
assert.strictEqual(err, error);
assert.strictEqual(has(et.events, 'error'), false);
});
}
function prioritizesEventEmitter() {
var ee = new EventEmitter();
ee.addEventListener = assert.fail;
ee.removeAllListeners = assert.fail;
process.nextTick(function () {
ee.emit('foo');
});
return once(ee, 'foo');
}
var allTests = [
onceAnEvent(),
onceAnEventWithTwoArgs(),
catchesErrors(),
stopListeningAfterCatchingError(),
onceError(),
onceWithEventTarget(),
onceWithEventTargetError(),
prioritizesEventEmitter()
];
var hasBrowserEventTarget = false;
try {
hasBrowserEventTarget = typeof (new window.EventTarget().addEventListener) === 'function' &&
new window.Event('xyz').type === 'xyz';
} catch (err) {}
if (hasBrowserEventTarget) {
var onceWithBrowserEventTarget = function onceWithBrowserEventTarget() {
var et = new window.EventTarget();
var event = new window.Event('myevent');
process.nextTick(function () {
et.dispatchEvent(event);
});
return once(et, 'myevent').then(function (args) {
var value = args[0];
assert.strictEqual(value, event);
assert.strictEqual(has(et.events, 'myevent'), false);
});
}
var onceWithBrowserEventTargetError = function onceWithBrowserEventTargetError() {
var et = new window.EventTarget();
var error = new window.Event('error');
process.nextTick(function () {
et.dispatchEvent(error);
});
return once(et, 'error').then(function (args) {
var err = args[0];
assert.strictEqual(err, error);
assert.strictEqual(has(et.events, 'error'), false);
});
}
common.test.comment('Testing with browser built-in EventTarget');
allTests.push([
onceWithBrowserEventTarget(),
onceWithBrowserEventTargetError()
]);
}
module.exports = Promise.all(allTests);