forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest-events-disposable.js
More file actions
48 lines (41 loc) · 1.59 KB
/
test-events-disposable.js
File metadata and controls
48 lines (41 loc) · 1.59 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
'use strict';
const common = require('../common');
const { strictEqual, throws } = require('assert');
const { EventEmitter } = require('events');
const emitter = new EventEmitter();
{
// Verify that the disposable stack removes the handlers
// when the stack is disposed.
using ds = new DisposableStack();
ds.use(emitter.addDisposableListener('foo', common.mustCall()));
ds.use(emitter.addDisposableListener('bar', common.mustCall()));
ds.use(emitter.addDisposableListener('baz', common.mustNotCall()),
{ once: true });
emitter.emit('foo');
emitter.emit('bar');
strictEqual(emitter.listenerCount('foo'), 1);
strictEqual(emitter.listenerCount('bar'), 1);
// The disposer returned by addDisposableListener can be called manually.
const disposer = emitter.addDisposableListener('foo', common.mustNotCall());
strictEqual(emitter.listenerCount('foo'), 2);
disposer.dispose();
strictEqual(emitter.listenerCount('foo'), 1);
// Disposer is callable multiple times without error.
disposer.dispose();
}
emitter.emit('foo');
emitter.emit('bar');
emitter.emit('baz');
strictEqual(emitter.listenerCount('foo'), 0);
strictEqual(emitter.listenerCount('bar'), 0);
// ============================================================================
// Type checking on inputs
throws(() => emitter.addDisposableListener('foo', 'not a function'), {
code: 'ERR_INVALID_ARG_TYPE',
});
throws(() => emitter.addDisposableListener('foo', () => {}, ''), {
code: 'ERR_INVALID_ARG_TYPE',
});
throws(() => emitter.addDisposableListener('foo', () => {}, { once: '' }), {
code: 'ERR_INVALID_ARG_TYPE',
});