Skip to content

Commit fc3ffe2

Browse files
committed
events: set EventEmitterAsyncResource fields private
1 parent 1d2603b commit fc3ffe2

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

lib/events.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const {
6767
AbortError,
6868
codes: {
6969
ERR_INVALID_ARG_TYPE,
70-
ERR_INVALID_THIS,
7170
ERR_UNHANDLED_ERROR,
7271
},
7372
genericNodeError,
@@ -106,9 +105,9 @@ function lazyEventEmitterAsyncResource() {
106105
AsyncResource,
107106
} = require('async_hooks');
108107

109-
const kEventEmitter = Symbol('kEventEmitter');
110-
const kAsyncResource = Symbol('kAsyncResource');
111108
class EventEmitterReferencingAsyncResource extends AsyncResource {
109+
#eventEmitter;
110+
112111
/**
113112
* @param {EventEmitter} ee
114113
* @param {string} [type]
@@ -119,21 +118,21 @@ function lazyEventEmitterAsyncResource() {
119118
*/
120119
constructor(ee, type, options) {
121120
super(type, options);
122-
this[kEventEmitter] = ee;
121+
this.#eventEmitter = ee;
123122
}
124123

125124
/**
126125
* @type {EventEmitter}
127126
*/
128127
get eventEmitter() {
129-
if (this[kEventEmitter] === undefined)
130-
throw new ERR_INVALID_THIS('EventEmitterReferencingAsyncResource');
131-
return this[kEventEmitter];
128+
return this.#eventEmitter;
132129
}
133130
}
134131

135132
EventEmitterAsyncResource =
136133
class EventEmitterAsyncResource extends EventEmitter {
134+
#asyncResource;
135+
137136
/**
138137
* @param {{
139138
* name?: string,
@@ -154,8 +153,7 @@ function lazyEventEmitterAsyncResource() {
154153
}
155154
super(options);
156155

157-
this[kAsyncResource] =
158-
new EventEmitterReferencingAsyncResource(this, name, options);
156+
this.#asyncResource = new EventEmitterReferencingAsyncResource(this, name, options);
159157
}
160158

161159
/**
@@ -164,9 +162,7 @@ function lazyEventEmitterAsyncResource() {
164162
* @returns {boolean}
165163
*/
166164
emit(event, ...args) {
167-
if (this[kAsyncResource] === undefined)
168-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
169-
const { asyncResource } = this;
165+
const asyncResource = this.#asyncResource;
170166
ArrayPrototypeUnshift(args, super.emit, this, event);
171167
return ReflectApply(asyncResource.runInAsyncScope, asyncResource,
172168
args);
@@ -176,36 +172,28 @@ function lazyEventEmitterAsyncResource() {
176172
* @returns {void}
177173
*/
178174
emitDestroy() {
179-
if (this[kAsyncResource] === undefined)
180-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
181-
this.asyncResource.emitDestroy();
175+
this.#asyncResource.emitDestroy();
182176
}
183177

184178
/**
185179
* @type {number}
186180
*/
187181
get asyncId() {
188-
if (this[kAsyncResource] === undefined)
189-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
190-
return this.asyncResource.asyncId();
182+
return this.#asyncResource.asyncId();
191183
}
192184

193185
/**
194186
* @type {number}
195187
*/
196188
get triggerAsyncId() {
197-
if (this[kAsyncResource] === undefined)
198-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
199-
return this.asyncResource.triggerAsyncId();
189+
return this.#asyncResource.triggerAsyncId();
200190
}
201191

202192
/**
203193
* @type {EventEmitterReferencingAsyncResource}
204194
*/
205195
get asyncResource() {
206-
if (this[kAsyncResource] === undefined)
207-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
208-
return this[kAsyncResource];
196+
return this.#asyncResource;
209197
}
210198
};
211199
}

0 commit comments

Comments
 (0)