forked from joshuafcole/async-node-events
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-emitter.js
More file actions
285 lines (255 loc) · 8.82 KB
/
event-emitter.js
File metadata and controls
285 lines (255 loc) · 8.82 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import {EventEmitter} from '../lib/index.js';
import {expect} from 'chai';
describe('EventEmitter', function() {
describe('#constructor', function() {
it('should construct without error', function() {
const emitter = new EventEmitter();
expect(emitter).to.not.be.undefined;
});
});
describe('#emit', () => {
it('should not error when emitting a nonexistent event async', async () => {
const emitter = new EventEmitter();
const finished = await emitter.emit('doesNotExist');
expect(finished).to.be.undefined;
});
it('should call once listeners and remove them', async () => {
const emitter = new EventEmitter();
let emitted = false;
emitter.once('test', function() {
emitted = true;
});
const finished = await emitter.emit('test');
expect(finished).to.be.undefined;
expect(emitted).to.be.true;
expect(emitter.listeners('test').length).to.equal(0);
});
it('should call regular listeners without removing them', async () => {
const emitter = new EventEmitter();
let emitted = false;
emitter.on('test', function() {
emitted = true;
});
const finished = await emitter.emit('test');
expect(finished).to.be.undefined;
expect(emitted).to.be.true;
expect(emitter._eventListeners.has('test')).to.be.true;
});
it('should call multiple listeners async', async () => {
let emissions = 0;
const emitter = new EventEmitter();
function emissionCounter() {
emissions++;
}
emitter.on('test', emissionCounter);
emitter.on('test', emissionCounter);
emitter.once('test', emissionCounter);
const finished = await emitter.emit('test');
expect(finished).to.be.undefined;
expect(emissions).to.equal(3);
});
it('should call multiple listeners sync', async () => {
let emissions = 0;
const emitter = new EventEmitter();
function emissionCounter() {
emissions++;
}
emitter.on('test', emissionCounter);
emitter.on('test', emissionCounter);
emitter.once('test', emissionCounter);
const finished = await emitter.emit('test');
expect(finished).to.be.undefined;
expect(emissions).to.equal(3);
});
it('should handle Object property event names', async () => {
let emissions = 0;
const emitter = new EventEmitter();
function emissionCounter() {
emissions++;
}
emitter.on('toString', emissionCounter);
emitter.on('toString', emissionCounter);
emitter.once('toString', emissionCounter);
const finished = await emitter.emit('toString');
expect(finished).to.be.undefined;
expect(emissions).to.equal(3);
});
it('should cancel events', async () => {
let emissions0 = 0;
let emissions1 = 0;
const emitter = new EventEmitter();
function emissionCounter0() {
emissions0++;
return false;
}
function emissionCounter1() {
emissions1++;
}
emitter.on('test', emissionCounter0);
emitter.on('test', emissionCounter1);
const finished = await emitter.emit('test');
expect(finished).to.be.false;
expect(emissions0).to.equal(1);
expect(emissions1).to.equal(0);
});
it('should cancel from once events', async () => {
let emissions0 = 0;
let emissions1 = 0;
const emitter = new EventEmitter();
function emissionCounter0() {
emissions0++;
return false;
}
function emissionCounter1() {
emissions1++;
}
emitter.once('test', emissionCounter0);
emitter.on('test', emissionCounter1);
const finished = await emitter.emit('test');
expect(finished).to.be.false;
expect(emissions0).to.equal(1);
expect(emissions1).to.equal(0);
});
});
describe('#listeners / #listenerCount', () => {
it('should return all registered listeners', async () => {
const emitter = new EventEmitter();
emitter.on('test', console.log);
emitter.on('test', console.info);
emitter.on('other', console.warn);
emitter.once('test', console.log);
expect(EventEmitter.listenerCount(emitter, 'test')).to.equal(3);
expect(EventEmitter.listenerCount(emitter, 'other')).to.equal(1);
expect(emitter.listeners('test')).to.have.length(3);
expect(emitter.listeners('other')).to.have.length(1);
});
});
describe('#on', () => {
it('should add a listener', async () => {
const emitter = new EventEmitter();
emitter.on('fake', console.log);
expect(emitter._eventListeners.get('fake')).to.have.length(1);
});
it('should respect the maxListeners property', async () => {
const emitter = new EventEmitter();
let emissions = 0;
emitter.setMaxListeners(2);
emitter.on('maxListenersPassed', function() {
emissions++;
});
emitter.on('fake', console.log);
emitter.on('fake', console.log);
emitter.on('fake', console.log);
expect(emissions).to.equal(1);
});
it('should call newListener', async () => {
const emitter = new EventEmitter();
let emissions = 0;
const eventName = 'test';
const listener = function() {};
emitter.on('newListener', function(_eventName, _listener) {
if(_eventName === eventName && _listener === listener) {
emissions++;
}
});
emitter.on(eventName, listener);
expect(emissions).to.equal(1);
});
it('should fail on async newListener', async () => {
const emitter = new EventEmitter();
emitter.on('newListener', async () => {});
let err;
try {
emitter.on('test', async () => {});
} catch(e) {
err = e;
}
expect(err).to.not.be.undefined;
});
});
describe('#once', () => {
it('should add a listener', async () => {
const emitter = new EventEmitter();
emitter.once('fake', console.log);
expect(emitter._onceListeners.get('fake')).to.have.length(1);
});
it('should respect the maxListeners property', async () => {
const emitter = new EventEmitter();
let emissions = 0;
emitter.setMaxListeners(2);
emitter.once('maxListenersPassed', function() {
emissions++;
});
emitter.once('fake', console.log);
emitter.once('fake', console.log);
emitter.once('fake', console.log);
expect(emissions).to.equal(1);
});
});
describe('#removeListener', () => {
it('should remove a listener', async () => {
const emitter = new EventEmitter();
function handleBlargh() {
}
emitter.on('blargh', handleBlargh);
emitter.on('blargh', console.log);
emitter.once('blargh', handleBlargh);
emitter.on('weagoo', handleBlargh);
emitter.removeListener('blargh', handleBlargh);
expect(emitter._eventListeners.get('blargh')).to.have.length(1);
expect(emitter._onceListeners.get('blargh')).to.be.undefined;
expect(emitter._eventListeners.get('weagoo')).to.have.length(1);
});
it('should be okay if no listeners exist to remove', () => {
const emitter = new EventEmitter();
function handleBlargh() {
}
emitter.on('weagoo', console.log);
emitter.removeListener('blargh', handleBlargh);
emitter.removeListener('weeagoo', handleBlargh);
expect(emitter.listeners('blargh')).to.have.length(0);
expect(emitter.listeners('weagoo')).to.have.length(1);
});
it('should call removeListener', async () => {
const emitter = new EventEmitter();
let emissions = 0;
const eventName = 'test';
const listener = function() {};
emitter.on('removeListener', function(_eventName, _listener) {
if(_eventName == eventName && _listener === listener) {
emissions++;
}
});
emitter.on(eventName, listener);
emitter.off(eventName, listener);
expect(emissions).to.equal(1);
});
it('should fail on async removeListener', async () => {
const emitter = new EventEmitter();
const listener = async () => {};
emitter.on('removeListener', async () => {});
let err;
try {
emitter.on('test', listener);
emitter.off('test', listener);
} catch(e) {
err = e;
}
expect(err).to.not.be.undefined;
});
});
describe('#removeAllListeners', () => {
it('should remove all listeners', async () => {
const emitter = new EventEmitter();
function handleBlargh() {
}
emitter.on('blargh', handleBlargh);
emitter.on('blargh', console.log);
emitter.once('blargh', handleBlargh);
emitter.on('weagoo', handleBlargh);
emitter.removeAllListeners('blargh');
expect(emitter.listeners('blargh')).to.have.length(0);
expect(emitter._eventListeners.get('weagoo')).to.have.length(1);
});
});
});