Skip to content

Commit 035c07f

Browse files
committed
lib: implement AbortSignal.any()
1 parent ab434d2 commit 035c07f

File tree

7 files changed

+287
-12
lines changed

7 files changed

+287
-12
lines changed

doc/api/globals.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ added:
121121

122122
Returns a new `AbortSignal` which will be aborted in `delay` milliseconds.
123123

124+
#### Static method: `AbortSignal.any(signals)`
125+
126+
<!-- YAML
127+
added: REPLACEME
128+
-->
129+
130+
* `signals` {Array} The `AbortSignal`s of which to compose a new `AbortSignal`.
131+
132+
Returns a new `AbortSignal` which will be aborted if any of the provided
133+
signals are aborted. Its [`abortSignal.reason`][] will be set to whichever
134+
one of the `signals` caused it to be aborted.
135+
124136
#### Event: `'abort'`
125137

126138
<!-- YAML
@@ -1043,3 +1055,4 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
10431055
[timers]: timers.md
10441056
[webassembly-mdn]: https://developer.mozilla.org/en-US/docs/WebAssembly
10451057
[webassembly-org]: https://webassembly.org
1058+
[`abortSignal.reason`]: #abortSignal.reason

lib/internal/abort_controller.js

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242

4343
const {
4444
validateAbortSignal,
45+
validateAbortSignalArray,
4546
validateObject,
4647
validateUint32,
4748
} = require('internal/validators');
@@ -54,6 +55,7 @@ const {
5455
clearTimeout,
5556
setTimeout,
5657
} = require('timers');
58+
const assert = require('internal/assert');
5759

5860
const {
5961
messaging_deserialize_symbol: kDeserialize,
@@ -80,13 +82,16 @@ function lazyMakeTransferable(obj) {
8082
}
8183

8284
const clearTimeoutRegistry = new SafeFinalizationRegistry(clearTimeout);
83-
const timeOutSignals = new SafeSet();
85+
const gcPersistentSignals = new SafeSet();
8486

8587
const kAborted = Symbol('kAborted');
8688
const kReason = Symbol('kReason');
8789
const kCloneData = Symbol('kCloneData');
8890
const kTimeout = Symbol('kTimeout');
8991
const kMakeTransferable = Symbol('kMakeTransferable');
92+
const kComposite = Symbol('kComposite');
93+
const kSourceSignals = Symbol('kSourceSignals');
94+
const kDependantSignals = Symbol('kDependantSignals');
9095

9196
function customInspect(self, obj, depth, options) {
9297
if (depth < 0)
@@ -116,7 +121,7 @@ function setWeakAbortSignalTimeout(weakRef, delay) {
116121
const timeout = setTimeout(() => {
117122
const signal = weakRef.deref();
118123
if (signal !== undefined) {
119-
timeOutSignals.delete(signal);
124+
gcPersistentSignals.delete(signal);
120125
abortSignal(
121126
signal,
122127
new DOMException(
@@ -185,25 +190,68 @@ class AbortSignal extends EventTarget {
185190
return signal;
186191
}
187192

193+
/**
194+
* @param {AbortSignal[]} signals
195+
* @returns {AbortSignal}
196+
*/
197+
static any(signals) {
198+
validateAbortSignalArray(signals, 'signals');
199+
const resultSignal = createAbortSignal({ composite: true });
200+
const resultSignalWeakRef = new WeakRef(resultSignal);
201+
for (const signal of signals) {
202+
if (signal.aborted) {
203+
abortSignal(resultSignal, signal.reason);
204+
return resultSignal;
205+
}
206+
resultSignal[kSourceSignals] ??= new SafeSet();
207+
signal[kDependantSignals] ??= new SafeSet();
208+
if (!signal[kComposite]) {
209+
resultSignal[kSourceSignals].add(new WeakRef(signal));
210+
signal[kDependantSignals].add(resultSignalWeakRef);
211+
} else {
212+
if (!signal[kSourceSignals]) {
213+
continue;
214+
}
215+
for (const sourceSignal of signal[kSourceSignals]) {
216+
const sourceSignalRef = sourceSignal.deref();
217+
if (!sourceSignalRef) {
218+
continue;
219+
}
220+
assert(!sourceSignalRef.aborted);
221+
assert(!sourceSignalRef[kComposite]);
222+
223+
if (resultSignal[kSourceSignals].has(sourceSignal)) {
224+
continue;
225+
}
226+
resultSignal[kSourceSignals].add(sourceSignal);
227+
sourceSignalRef[kDependantSignals].add(resultSignalWeakRef);
228+
}
229+
}
230+
}
231+
return resultSignal;
232+
}
233+
188234
[kNewListener](size, type, listener, once, capture, passive, weak) {
189235
super[kNewListener](size, type, listener, once, capture, passive, weak);
190-
if (this[kTimeout] &&
236+
const isTimeoutOrNonEmptyCompositeSignal = this[kTimeout] || (this[kComposite] && this[kSourceSignals]?.size);
237+
if (isTimeoutOrNonEmptyCompositeSignal &&
191238
type === 'abort' &&
192239
!this.aborted &&
193240
!weak &&
194241
size === 1) {
195-
// If this is a timeout signal, and we're adding a non-weak abort
242+
// If this is a timeout signal, or a non-empty composite signal, and we're adding a non-weak abort
196243
// listener, then we don't want it to be gc'd while the listener
197244
// is attached and the timer still hasn't fired. So, we retain a
198245
// strong ref that is held for as long as the listener is registered.
199-
timeOutSignals.add(this);
246+
gcPersistentSignals.add(this);
200247
}
201248
}
202249

203250
[kRemoveListener](size, type, listener, capture) {
204251
super[kRemoveListener](size, type, listener, capture);
205-
if (this[kTimeout] && type === 'abort' && size === 0) {
206-
timeOutSignals.delete(this);
252+
const isTimeoutOrNonEmptyCompositeSignal = this[kTimeout] || (this[kComposite] && this[kSourceSignals]?.size);
253+
if (isTimeoutOrNonEmptyCompositeSignal && type === 'abort' && size === 0) {
254+
gcPersistentSignals.delete(this);
207255
}
208256
}
209257

@@ -287,7 +335,8 @@ defineEventHandler(AbortSignal.prototype, 'abort');
287335
* @param {{
288336
* aborted? : boolean,
289337
* reason? : any,
290-
* transferable? : boolean
338+
* transferable? : boolean,
339+
* composite? : boolean,
291340
* }} [init]
292341
* @returns {AbortSignal}
293342
*/
@@ -296,11 +345,13 @@ function createAbortSignal(init = kEmptyObject) {
296345
aborted = false,
297346
reason = undefined,
298347
transferable = false,
348+
composite = false,
299349
} = init;
300350
const signal = new EventTarget();
301351
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
302352
signal[kAborted] = aborted;
303353
signal[kReason] = reason;
354+
signal[kComposite] = composite;
304355
return transferable ? lazyMakeTransferable(signal) : signal;
305356
}
306357

@@ -312,6 +363,11 @@ function abortSignal(signal, reason) {
312363
[kTrustEvent]: true,
313364
});
314365
signal.dispatchEvent(event);
366+
signal[kDependantSignals]?.forEach(s => {
367+
const signalRef = s.deref();
368+
if (!signalRef) return;
369+
abortSignal(signalRef, reason);
370+
});
315371
}
316372

317373
class AbortController {

lib/internal/validators.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ function validateBooleanArray(value, name) {
324324
}
325325
}
326326

327+
/**
328+
* @callback validateAbortSignalArray
329+
* @param {*} value
330+
* @param {string} name
331+
* @returns {asserts value is AbortSignal[]}
332+
*/
333+
334+
/** @type {validateAbortSignalArray} */
335+
function validateAbortSignalArray(value, name) {
336+
validateArray(value, name);
337+
for (let i = 0; i < value.length; i++) {
338+
validateAbortSignal(value[i], `${name}[${i}]`, false);
339+
}
340+
}
341+
327342
/**
328343
* @param {*} signal
329344
* @param {string} [name='signal']
@@ -395,11 +410,12 @@ function validatePort(port, name = 'Port', allowZero = true) {
395410
* @callback validateAbortSignal
396411
* @param {*} signal
397412
* @param {string} name
413+
* @param {boolean} [acceptUndefined=true]
398414
*/
399415

400416
/** @type {validateAbortSignal} */
401-
const validateAbortSignal = hideStackFrames((signal, name) => {
402-
if (signal !== undefined &&
417+
const validateAbortSignal = hideStackFrames((signal, name, acceptUndefined = true) => {
418+
if ((!acceptUndefined || signal !== undefined) &&
403419
(signal === null ||
404420
typeof signal !== 'object' ||
405421
!('aborted' in signal))) {
@@ -534,6 +550,7 @@ module.exports = {
534550
validateArray,
535551
validateStringArray,
536552
validateBooleanArray,
553+
validateAbortSignalArray,
537554
validateBoolean,
538555
validateBuffer,
539556
validateDictionary,

test/fixtures/wpt/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Last update:
1212

1313
- common: https://github.com/web-platform-tests/wpt/tree/dbd648158d/common
1414
- console: https://github.com/web-platform-tests/wpt/tree/767ae35464/console
15-
- dom/abort: https://github.com/web-platform-tests/wpt/tree/8fadb38120/dom/abort
15+
- dom/abort: https://github.com/web-platform-tests/wpt/tree/d1f1ecbd52/dom/abort
1616
- dom/events: https://github.com/web-platform-tests/wpt/tree/ab8999891c/dom/events
1717
- encoding: https://github.com/web-platform-tests/wpt/tree/0c1b9d1622/encoding
1818
- fetch/data-urls/resources: https://github.com/web-platform-tests/wpt/tree/7c79d998ff/fetch/data-urls/resources
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// META: script=./resources/abort-signal-any-tests.js
2+
3+
abortSignalAnySignalOnlyTests(AbortSignal);
4+
abortSignalAnyTests(AbortSignal, AbortController);

0 commit comments

Comments
 (0)