Skip to content

Commit

Permalink
events: set EventEmitterAsyncResource fields private
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 11, 2024
1 parent 1d2603b commit 5f8d924
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ function lazyEventEmitterAsyncResource() {
AsyncResource,
} = require('async_hooks');

const kEventEmitter = Symbol('kEventEmitter');
const kAsyncResource = Symbol('kAsyncResource');
class EventEmitterReferencingAsyncResource extends AsyncResource {
/**
* @param {EventEmitter} ee
Expand All @@ -119,16 +117,14 @@ 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;
}
}

Expand All @@ -154,8 +150,7 @@ function lazyEventEmitterAsyncResource() {
}
super(options);

this[kAsyncResource] =
new EventEmitterReferencingAsyncResource(this, name, options);
this.#asyncResource = new EventEmitterReferencingAsyncResource(this, name, options);
}

/**
Expand All @@ -164,9 +159,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);
Expand All @@ -176,36 +169,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;
}
};
}
Expand Down

0 comments on commit 5f8d924

Please sign in to comment.