Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore me #52411

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions benchmark/abort_controller/abort-signal-static-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
n: [5e6],
kind: ['default-reason', 'same-reason'],
});

function main({ n, kind }) {
switch (kind) {
case 'default-reason':
bench.start();
for (let i = 0; i < n; ++i)
AbortSignal.abort();
bench.end(n);
break;
case 'same-reason': {
const reason = new Error('same reason');

bench.start();
for (let i = 0; i < n; ++i)
AbortSignal.abort(reason);
bench.end(n);
break;
}
default:
throw new Error('Invalid kind');
}
}
75 changes: 38 additions & 37 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
const {
ObjectAssign,
ObjectDefineProperties,
ObjectSetPrototypeOf,
ObjectDefineProperty,
PromiseResolve,
SafeFinalizationRegistry,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -137,8 +138,35 @@ function setWeakAbortSignalTimeout(weakRef, delay) {
}

class AbortSignal extends EventTarget {
constructor() {
throw new ERR_ILLEGAL_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);
}
}

/**
Expand Down Expand Up @@ -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 });
}

/**
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -319,7 +347,7 @@ class AbortSignal extends EventTarget {
}

function ClonedAbortSignal() {
return createAbortSignal({ transferable: true });
return new AbortSignal(kDontThrowSymbol, { transferable: true });
}
ClonedAbortSignal.prototype[kDeserialize] = () => {};

Expand All @@ -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;
Expand All @@ -385,15 +386,15 @@ class AbortController {
* @type {AbortSignal}
*/
get signal() {
this.#signal ??= createAbortSignal();
this.#signal ??= new AbortSignal(kDontThrowSymbol);
return this.#signal;
}

/**
* @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) {
Expand All @@ -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;
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class Listener {
}
}

// Updating this function will require updating the EventTarget as well
function initEventTarget(self) {
self[kEvents] = new SafeMap();
self[kMaxEventTargetListeners] = EventEmitter.defaultMaxListeners;
Expand All @@ -554,9 +555,12 @@ class EventTarget {
// Ref: https://github.com/nodejs/node/pull/33661
static [kIsEventTarget] = true;

constructor() {
initEventTarget(this);
}
// From here: Same as initEventTarget code, added here due to performance reasons
[kEvents] = new SafeMap();
[kMaxEventTargetListeners] = EventEmitter.defaultMaxListeners;
[kMaxEventTargetListenersWarned] = false;
[kHandlers] = new SafeMap();
// Until here

[kNewListener](size, type, listener, once, capture, passive, weak) {
if (this[kMaxEventTargetListeners] > 0 &&
Expand Down Expand Up @@ -868,6 +872,7 @@ ObjectDefineProperties(EventTarget.prototype, {
},
});

// If you update this function please add to node event target as well, not used in NodeEventTarget due to performance reasons
function initNodeEventTarget(self) {
initEventTarget(self);
}
Expand All @@ -876,11 +881,6 @@ class NodeEventTarget extends EventTarget {
static [kIsNodeEventTarget] = true;
static defaultMaxListeners = 10;

constructor() {
super();
initNodeEventTarget(this);
}

/**
* @param {number} n
*/
Expand Down
Loading