mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 13:28:42 +00:00

When add listener by once, it will be wrapped into another function. And when pass listener and there is just one event listener added by once, it will return 0 even if passed listener equal wrapped event listener. Refs: https://github.com/nodejs/node/pull/46523 PR-URL: https://github.com/nodejs/node/pull/48592 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const EventEmitter = require('events');
|
|
const assert = require('assert');
|
|
|
|
const EE = new EventEmitter();
|
|
const handler = common.mustCall(undefined, 3);
|
|
const anotherHandler = common.mustCall();
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 0);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 0);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
|
|
|
EE.once('event', handler);
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 1);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 1);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
|
|
|
EE.removeAllListeners('event');
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 0);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 0);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
|
|
|
EE.on('event', handler);
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 1);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 1);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
|
|
|
EE.once('event', anotherHandler);
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 2);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 1);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 1);
|
|
|
|
assert.strictEqual(EE.listenerCount('another-event'), 0);
|
|
assert.strictEqual(EE.listenerCount('another-event', handler), 0);
|
|
assert.strictEqual(EE.listenerCount('another-event', anotherHandler), 0);
|
|
|
|
EE.once('event', handler);
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 3);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 2);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 1);
|
|
|
|
EE.emit('event');
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 1);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 1);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
|
|
|
EE.emit('event');
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 1);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 1);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
|
|
|
EE.off('event', handler);
|
|
|
|
assert.strictEqual(EE.listenerCount('event'), 0);
|
|
assert.strictEqual(EE.listenerCount('event', handler), 0);
|
|
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|