Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b484732

Browse filesBrowse files
lpincaMylesBorins
authored andcommitted
events: assume an EventEmitter if emitter.on is a function
Assume that the `emitter` argument of `EventEmitter.once()` is an `EventEmitter` if `emitter.on` is a function. Refs: 4b3654e923e7c3c2 Refs: websockets/ws#1795 PR-URL: #35818 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c6eb0b6 commit b484732
Copy full SHA for b484732

File tree

Expand file treeCollapse file tree

2 files changed

+26
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-1
lines changed
Open diff view settings
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,10 @@ function unwrapListeners(arr) {
623623

624624
function once(emitter, name) {
625625
return new Promise((resolve, reject) => {
626-
if (typeof emitter.addEventListener === 'function') {
626+
if (
627+
typeof emitter.addEventListener === 'function' &&
628+
typeof emitter.on !== 'function'
629+
) {
627630
// EventTarget does not have `error` event semantics like Node
628631
// EventEmitters, we do not listen to `error` events here.
629632
emitter.addEventListener(
Collapse file

‎test/parallel/test-events-once.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-events-once.js
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ async function onceWithEventTargetError() {
166166
strictEqual(Reflect.has(et.events, 'error'), false);
167167
}
168168

169+
async function assumesEventEmitterIfOnIsAFunction() {
170+
const ee = new EventEmitter();
171+
ee.addEventListener = () => {};
172+
173+
const expected = new Error('kaboom');
174+
let err;
175+
process.nextTick(() => {
176+
ee.emit('error', expected);
177+
});
178+
179+
try {
180+
await once(ee, 'myevent');
181+
} catch (_e) {
182+
err = _e;
183+
}
184+
185+
strictEqual(err, expected);
186+
strictEqual(ee.listenerCount('error'), 0);
187+
strictEqual(ee.listenerCount('myevent'), 0);
188+
}
189+
169190
Promise.all([
170191
onceAnEvent(),
171192
onceAnEventWithTwoArgs(),
@@ -175,4 +196,5 @@ Promise.all([
175196
onceWithEventTarget(),
176197
onceWithEventTargetTwoArgs(),
177198
onceWithEventTargetError(),
199+
assumesEventEmitterIfOnIsAFunction(),
178200
]).then(common.mustCall());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.