mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
perf(web): optimize AbortController (#12165)
- Use regular class constructor and symbol "private" attributes - Lazy init Set of follower signals
This commit is contained in:
parent
8375f5464b
commit
f827b93df4
1 changed files with 17 additions and 12 deletions
|
@ -11,7 +11,6 @@
|
||||||
Boolean,
|
Boolean,
|
||||||
Set,
|
Set,
|
||||||
SetPrototypeAdd,
|
SetPrototypeAdd,
|
||||||
SetPrototypeClear,
|
|
||||||
SetPrototypeDelete,
|
SetPrototypeDelete,
|
||||||
Symbol,
|
Symbol,
|
||||||
SymbolToStringTag,
|
SymbolToStringTag,
|
||||||
|
@ -21,13 +20,12 @@
|
||||||
const add = Symbol("add");
|
const add = Symbol("add");
|
||||||
const signalAbort = Symbol("signalAbort");
|
const signalAbort = Symbol("signalAbort");
|
||||||
const remove = Symbol("remove");
|
const remove = Symbol("remove");
|
||||||
|
const aborted = Symbol("aborted");
|
||||||
|
const abortAlgos = Symbol("abortAlgos");
|
||||||
|
|
||||||
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
||||||
|
|
||||||
class AbortSignal extends EventTarget {
|
class AbortSignal extends EventTarget {
|
||||||
#aborted = false;
|
|
||||||
#abortAlgorithms = new Set();
|
|
||||||
|
|
||||||
static abort() {
|
static abort() {
|
||||||
const signal = new AbortSignal(illegalConstructorKey);
|
const signal = new AbortSignal(illegalConstructorKey);
|
||||||
signal[signalAbort]();
|
signal[signalAbort]();
|
||||||
|
@ -35,25 +33,30 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
[add](algorithm) {
|
[add](algorithm) {
|
||||||
SetPrototypeAdd(this.#abortAlgorithms, algorithm);
|
if (this[abortAlgos] === null) {
|
||||||
|
this[abortAlgos] = new Set();
|
||||||
|
}
|
||||||
|
SetPrototypeAdd(this[abortAlgos], algorithm);
|
||||||
}
|
}
|
||||||
|
|
||||||
[signalAbort]() {
|
[signalAbort]() {
|
||||||
if (this.#aborted) {
|
if (this[aborted]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#aborted = true;
|
this[aborted] = true;
|
||||||
for (const algorithm of this.#abortAlgorithms) {
|
if (this[abortAlgos] !== null) {
|
||||||
algorithm();
|
for (const algorithm of this[abortAlgos]) {
|
||||||
|
algorithm();
|
||||||
|
}
|
||||||
|
this[abortAlgos] = null;
|
||||||
}
|
}
|
||||||
SetPrototypeClear(this.#abortAlgorithms);
|
|
||||||
const event = new Event("abort");
|
const event = new Event("abort");
|
||||||
setIsTrusted(event, true);
|
setIsTrusted(event, true);
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
[remove](algorithm) {
|
[remove](algorithm) {
|
||||||
SetPrototypeDelete(this.#abortAlgorithms, algorithm);
|
this[abortAlgos] && SetPrototypeDelete(this[abortAlgos], algorithm);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(key = null) {
|
constructor(key = null) {
|
||||||
|
@ -61,11 +64,13 @@
|
||||||
throw new TypeError("Illegal constructor.");
|
throw new TypeError("Illegal constructor.");
|
||||||
}
|
}
|
||||||
super();
|
super();
|
||||||
|
this[aborted] = false;
|
||||||
|
this[abortAlgos] = null;
|
||||||
this[webidl.brand] = webidl.brand;
|
this[webidl.brand] = webidl.brand;
|
||||||
}
|
}
|
||||||
|
|
||||||
get aborted() {
|
get aborted() {
|
||||||
return Boolean(this.#aborted);
|
return Boolean(this[aborted]);
|
||||||
}
|
}
|
||||||
|
|
||||||
get [SymbolToStringTag]() {
|
get [SymbolToStringTag]() {
|
||||||
|
|
Loading…
Reference in a new issue