Skip to content

Commit 65dd60f

Browse files
committed
lib: improve perf of AbortListener creation
1 parent 74343a7 commit 65dd60f

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

lib/internal/abort_controller.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
const {
77
ObjectAssign,
88
ObjectDefineProperties,
9-
ObjectSetPrototypeOf,
109
ObjectDefineProperty,
1110
PromiseResolve,
1211
SafeFinalizationRegistry,
@@ -69,6 +68,8 @@ const {
6968
let _MessageChannel;
7069
let markTransferMode;
7170

71+
const kDontThrowSymbol = Symbol('kDontThrowSymbol');
72+
7273
// Loading the MessageChannel and markTransferable have to be done lazily
7374
// because otherwise we'll end up with a require cycle that ends up with
7475
// an incomplete initialization of abort_controller.
@@ -137,8 +138,35 @@ function setWeakAbortSignalTimeout(weakRef, delay) {
137138
}
138139

139140
class AbortSignal extends EventTarget {
140-
constructor() {
141-
throw new ERR_ILLEGAL_CONSTRUCTOR();
141+
142+
/**
143+
* @param {symbol | undefined} dontThrowSymbol
144+
* @param {{
145+
* aborted? : boolean,
146+
* reason? : any,
147+
* transferable? : boolean,
148+
* composite? : boolean,
149+
* }} [init]
150+
* @private
151+
*/
152+
constructor(dontThrowSymbol = undefined, init = kEmptyObject) {
153+
if (dontThrowSymbol !== kDontThrowSymbol) {
154+
throw new ERR_ILLEGAL_CONSTRUCTOR();
155+
}
156+
super();
157+
158+
const {
159+
aborted = false,
160+
reason = undefined,
161+
transferable = false,
162+
composite = false,
163+
} = init;
164+
this[kAborted] = aborted;
165+
this[kReason] = reason;
166+
this[kComposite] = composite;
167+
if (transferable) {
168+
lazyMarkTransferMode(this, false, true);
169+
}
142170
}
143171

144172
/**
@@ -176,7 +204,7 @@ class AbortSignal extends EventTarget {
176204
*/
177205
static abort(
178206
reason = new DOMException('This operation was aborted', 'AbortError')) {
179-
return createAbortSignal({ aborted: true, reason });
207+
return new AbortSignal(kDontThrowSymbol, { aborted: true, reason });
180208
}
181209

182210
/**
@@ -185,7 +213,7 @@ class AbortSignal extends EventTarget {
185213
*/
186214
static timeout(delay) {
187215
validateUint32(delay, 'delay', false);
188-
const signal = createAbortSignal();
216+
const signal = new AbortSignal(kDontThrowSymbol);
189217
signal[kTimeout] = true;
190218
clearTimeoutRegistry.register(
191219
signal,
@@ -199,7 +227,7 @@ class AbortSignal extends EventTarget {
199227
*/
200228
static any(signals) {
201229
validateAbortSignalArray(signals, 'signals');
202-
const resultSignal = createAbortSignal({ composite: true });
230+
const resultSignal = new AbortSignal(kDontThrowSymbol, { composite: true });
203231
if (!signals.length) {
204232
return resultSignal;
205233
}
@@ -319,7 +347,7 @@ class AbortSignal extends EventTarget {
319347
}
320348

321349
function ClonedAbortSignal() {
322-
return createAbortSignal({ transferable: true });
350+
return new AbortSignal(kDontThrowSymbol, { transferable: true });
323351
}
324352
ClonedAbortSignal.prototype[kDeserialize] = () => {};
325353

@@ -337,33 +365,6 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
337365

338366
defineEventHandler(AbortSignal.prototype, 'abort');
339367

340-
/**
341-
* @param {{
342-
* aborted? : boolean,
343-
* reason? : any,
344-
* transferable? : boolean,
345-
* composite? : boolean,
346-
* }} [init]
347-
* @returns {AbortSignal}
348-
*/
349-
function createAbortSignal(init = kEmptyObject) {
350-
const {
351-
aborted = false,
352-
reason = undefined,
353-
transferable = false,
354-
composite = false,
355-
} = init;
356-
const signal = new EventTarget();
357-
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
358-
signal[kAborted] = aborted;
359-
signal[kReason] = reason;
360-
signal[kComposite] = composite;
361-
if (transferable) {
362-
lazyMarkTransferMode(signal, false, true);
363-
}
364-
return signal;
365-
}
366-
367368
function abortSignal(signal, reason) {
368369
if (signal[kAborted]) return;
369370
signal[kAborted] = true;
@@ -385,15 +386,15 @@ class AbortController {
385386
* @type {AbortSignal}
386387
*/
387388
get signal() {
388-
this.#signal ??= createAbortSignal();
389+
this.#signal ??= new AbortSignal(kDontThrowSymbol);
389390
return this.#signal;
390391
}
391392

392393
/**
393394
* @param {any} [reason]
394395
*/
395396
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
396-
abortSignal(this.#signal ??= createAbortSignal(), reason);
397+
abortSignal(this.#signal ??= new AbortSignal(kDontThrowSymbol), reason);
397398
}
398399

399400
[customInspectSymbol](depth, options) {
@@ -404,7 +405,7 @@ class AbortController {
404405

405406
static [kMakeTransferable]() {
406407
const controller = new AbortController();
407-
controller.#signal = createAbortSignal({ transferable: true });
408+
controller.#signal = new AbortSignal(kDontThrowSymbol, { transferable: true });
408409
return controller;
409410
}
410411
}

0 commit comments

Comments
 (0)