diff --git a/lib/events.js b/lib/events.js index 36217a8313c134a..cb20ddd6b854760 100644 --- a/lib/events.js +++ b/lib/events.js @@ -67,7 +67,6 @@ const { AbortError, codes: { ERR_INVALID_ARG_TYPE, - ERR_INVALID_THIS, ERR_UNHANDLED_ERROR, }, genericNodeError, @@ -106,9 +105,9 @@ function lazyEventEmitterAsyncResource() { AsyncResource, } = require('async_hooks'); - const kEventEmitter = Symbol('kEventEmitter'); - const kAsyncResource = Symbol('kAsyncResource'); class EventEmitterReferencingAsyncResource extends AsyncResource { + #eventEmitter; + /** * @param {EventEmitter} ee * @param {string} [type] @@ -119,21 +118,21 @@ function lazyEventEmitterAsyncResource() { */ constructor(ee, type, options) { super(type, options); - this[kEventEmitter] = ee; + this.#eventEmitter = ee; } /** * @type {EventEmitter} */ get eventEmitter() { - if (this[kEventEmitter] === undefined) - throw new ERR_INVALID_THIS('EventEmitterReferencingAsyncResource'); - return this[kEventEmitter]; + return this.#eventEmitter; } } EventEmitterAsyncResource = class EventEmitterAsyncResource extends EventEmitter { + #asyncResource; + /** * @param {{ * name?: string, @@ -154,8 +153,7 @@ function lazyEventEmitterAsyncResource() { } super(options); - this[kAsyncResource] = - new EventEmitterReferencingAsyncResource(this, name, options); + this.#asyncResource = new EventEmitterReferencingAsyncResource(this, name, options); } /** @@ -164,9 +162,7 @@ function lazyEventEmitterAsyncResource() { * @returns {boolean} */ emit(event, ...args) { - if (this[kAsyncResource] === undefined) - throw new ERR_INVALID_THIS('EventEmitterAsyncResource'); - const { asyncResource } = this; + const asyncResource = this.#asyncResource; ArrayPrototypeUnshift(args, super.emit, this, event); return ReflectApply(asyncResource.runInAsyncScope, asyncResource, args); @@ -176,36 +172,28 @@ function lazyEventEmitterAsyncResource() { * @returns {void} */ emitDestroy() { - if (this[kAsyncResource] === undefined) - throw new ERR_INVALID_THIS('EventEmitterAsyncResource'); - this.asyncResource.emitDestroy(); + this.#asyncResource.emitDestroy(); } /** * @type {number} */ get asyncId() { - if (this[kAsyncResource] === undefined) - throw new ERR_INVALID_THIS('EventEmitterAsyncResource'); - return this.asyncResource.asyncId(); + return this.#asyncResource.asyncId(); } /** * @type {number} */ get triggerAsyncId() { - if (this[kAsyncResource] === undefined) - throw new ERR_INVALID_THIS('EventEmitterAsyncResource'); - return this.asyncResource.triggerAsyncId(); + return this.#asyncResource.triggerAsyncId(); } /** * @type {EventEmitterReferencingAsyncResource} */ get asyncResource() { - if (this[kAsyncResource] === undefined) - throw new ERR_INVALID_THIS('EventEmitterAsyncResource'); - return this[kAsyncResource]; + return this.#asyncResource; } }; }