mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
lib: improve perf of AbortSignal creation
PR-URL: https://github.com/nodejs/node/pull/52408 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
This commit is contained in:
parent
64b67779f7
commit
818c10e86d
|
|
@ -6,7 +6,6 @@
|
|||
const {
|
||||
ObjectAssign,
|
||||
ObjectDefineProperties,
|
||||
ObjectSetPrototypeOf,
|
||||
ObjectDefineProperty,
|
||||
PromiseResolve,
|
||||
SafeFinalizationRegistry,
|
||||
|
|
@ -69,6 +68,8 @@ const {
|
|||
let _MessageChannel;
|
||||
let markTransferMode;
|
||||
|
||||
const kDontThrowSymbol = Symbol('kDontThrowSymbol');
|
||||
|
||||
// Loading the MessageChannel and markTransferable have to be done lazily
|
||||
// because otherwise we'll end up with a require cycle that ends up with
|
||||
// an incomplete initialization of abort_controller.
|
||||
|
|
@ -137,9 +138,36 @@ function setWeakAbortSignalTimeout(weakRef, delay) {
|
|||
}
|
||||
|
||||
class AbortSignal extends EventTarget {
|
||||
constructor() {
|
||||
|
||||
/**
|
||||
* @param {symbol | undefined} dontThrowSymbol
|
||||
* @param {{
|
||||
* aborted? : boolean,
|
||||
* reason? : any,
|
||||
* transferable? : boolean,
|
||||
* composite? : boolean,
|
||||
* }} [init]
|
||||
* @private
|
||||
*/
|
||||
constructor(dontThrowSymbol = undefined, init = kEmptyObject) {
|
||||
if (dontThrowSymbol !== kDontThrowSymbol) {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
}
|
||||
super();
|
||||
|
||||
const {
|
||||
aborted = false,
|
||||
reason = undefined,
|
||||
transferable = false,
|
||||
composite = false,
|
||||
} = init;
|
||||
this[kAborted] = aborted;
|
||||
this[kReason] = reason;
|
||||
this[kComposite] = composite;
|
||||
if (transferable) {
|
||||
lazyMarkTransferMode(this, false, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
|
|
@ -176,7 +204,7 @@ class AbortSignal extends EventTarget {
|
|||
*/
|
||||
static abort(
|
||||
reason = new DOMException('This operation was aborted', 'AbortError')) {
|
||||
return createAbortSignal({ aborted: true, reason });
|
||||
return new AbortSignal(kDontThrowSymbol, { aborted: true, reason });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -185,7 +213,7 @@ class AbortSignal extends EventTarget {
|
|||
*/
|
||||
static timeout(delay) {
|
||||
validateUint32(delay, 'delay', false);
|
||||
const signal = createAbortSignal();
|
||||
const signal = new AbortSignal(kDontThrowSymbol);
|
||||
signal[kTimeout] = true;
|
||||
clearTimeoutRegistry.register(
|
||||
signal,
|
||||
|
|
@ -199,7 +227,7 @@ class AbortSignal extends EventTarget {
|
|||
*/
|
||||
static any(signals) {
|
||||
validateAbortSignalArray(signals, 'signals');
|
||||
const resultSignal = createAbortSignal({ composite: true });
|
||||
const resultSignal = new AbortSignal(kDontThrowSymbol, { composite: true });
|
||||
if (!signals.length) {
|
||||
return resultSignal;
|
||||
}
|
||||
|
|
@ -319,7 +347,7 @@ class AbortSignal extends EventTarget {
|
|||
}
|
||||
|
||||
function ClonedAbortSignal() {
|
||||
return createAbortSignal({ transferable: true });
|
||||
return new AbortSignal(kDontThrowSymbol, { transferable: true });
|
||||
}
|
||||
ClonedAbortSignal.prototype[kDeserialize] = () => {};
|
||||
|
||||
|
|
@ -337,33 +365,6 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
|
|||
|
||||
defineEventHandler(AbortSignal.prototype, 'abort');
|
||||
|
||||
/**
|
||||
* @param {{
|
||||
* aborted? : boolean,
|
||||
* reason? : any,
|
||||
* transferable? : boolean,
|
||||
* composite? : boolean,
|
||||
* }} [init]
|
||||
* @returns {AbortSignal}
|
||||
*/
|
||||
function createAbortSignal(init = kEmptyObject) {
|
||||
const {
|
||||
aborted = false,
|
||||
reason = undefined,
|
||||
transferable = false,
|
||||
composite = false,
|
||||
} = init;
|
||||
const signal = new EventTarget();
|
||||
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
|
||||
signal[kAborted] = aborted;
|
||||
signal[kReason] = reason;
|
||||
signal[kComposite] = composite;
|
||||
if (transferable) {
|
||||
lazyMarkTransferMode(signal, false, true);
|
||||
}
|
||||
return signal;
|
||||
}
|
||||
|
||||
function abortSignal(signal, reason) {
|
||||
if (signal[kAborted]) return;
|
||||
signal[kAborted] = true;
|
||||
|
|
@ -385,7 +386,7 @@ class AbortController {
|
|||
* @type {AbortSignal}
|
||||
*/
|
||||
get signal() {
|
||||
this.#signal ??= createAbortSignal();
|
||||
this.#signal ??= new AbortSignal(kDontThrowSymbol);
|
||||
return this.#signal;
|
||||
}
|
||||
|
||||
|
|
@ -393,7 +394,7 @@ class AbortController {
|
|||
* @param {any} [reason]
|
||||
*/
|
||||
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
|
||||
abortSignal(this.#signal ??= createAbortSignal(), reason);
|
||||
abortSignal(this.#signal ??= new AbortSignal(kDontThrowSymbol), reason);
|
||||
}
|
||||
|
||||
[customInspectSymbol](depth, options) {
|
||||
|
|
@ -404,7 +405,7 @@ class AbortController {
|
|||
|
||||
static [kMakeTransferable]() {
|
||||
const controller = new AbortController();
|
||||
controller.#signal = createAbortSignal({ transferable: true });
|
||||
controller.#signal = new AbortSignal(kDontThrowSymbol, { transferable: true });
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user