From fda2105481209f3825c552501d677c99ac7c01da Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 21 May 2022 17:51:52 +0800 Subject: [PATCH] events: use `kEmptyObject` PR-URL: https://github.com/nodejs/node/pull/43159 Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel --- lib/events.js | 5 ++++- lib/internal/event_target.js | 12 ++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index ad5ded2d0e9fc0..603f3083ed2ce0 100644 --- a/lib/events.js +++ b/lib/events.js @@ -52,6 +52,9 @@ const { SymbolAsyncIterator, } = primordials; const kRejection = SymbolFor('nodejs.rejection'); + +const { kEmptyObject } = require('internal/util'); + const { inspect } = require('internal/util/inspect'); let spliceOne; @@ -945,7 +948,7 @@ function getEventListeners(emitterOrTarget, type) { * @param {{ signal: AbortSignal; }} [options] * @returns {Promise} */ -async function once(emitter, name, options = {}) { +async function once(emitter, name, options = kEmptyObject) { const signal = options?.signal; validateAbortSignal(signal, 'options.signal'); if (signal?.aborted) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index d005e9edc08c13..651cf55d5f3f8e 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -35,7 +35,11 @@ const { } = require('internal/errors'); const { validateObject, validateString } = require('internal/validators'); -const { customInspectSymbol, kEnumerableProperty } = require('internal/util'); +const { + customInspectSymbol, + kEmptyObject, + kEnumerableProperty, +} = require('internal/util'); const { inspect } = require('util'); const kIsEventTarget = SymbolFor('nodejs.event_target'); @@ -453,7 +457,7 @@ class EventTarget { * signal?: AbortSignal * }} [options] */ - addEventListener(type, listener, options = {}) { + addEventListener(type, listener, options = kEmptyObject) { if (!isEventTarget(this)) throw new ERR_INVALID_THIS('EventTarget'); if (arguments.length < 2) @@ -540,7 +544,7 @@ class EventTarget { * capture?: boolean, * }} [options] */ - removeEventListener(type, listener, options = {}) { + removeEventListener(type, listener, options = kEmptyObject) { if (!isEventTarget(this)) throw new ERR_INVALID_THIS('EventTarget'); if (!shouldAddListener(listener)) @@ -869,7 +873,7 @@ function validateEventListenerOptions(options) { return { capture: options }; if (options === null) - return {}; + return kEmptyObject; validateObject(options, 'options', { allowArray: true, allowFunction: true, });