Skip to content

Commit

Permalink
lib: propagate aborted state to dependent signals before firing events
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly committed Sep 7, 2024
1 parent dcc2ed9 commit a40318c
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// in https://github.com/mysticatea/abort-controller (MIT license)

const {
ArrayPrototypePush,
ObjectAssign,
ObjectDefineProperties,
ObjectDefineProperty,
Expand Down Expand Up @@ -373,17 +374,36 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
defineEventHandler(AbortSignal.prototype, 'abort');

function abortSignal(signal, reason) {
// 1. If signal is aborted, then return.
if (signal[kAborted]) return;
signal[kAborted] = true;
// 2. Set signal's abort reason to reason if it is given;
// otherwise to a new "AbortError" DOMException.
signal[kReason] = reason;
// 3. Let dependentSignalsToAbort be a new list.
const dependentSignalsToAbort = [];
// 4. For each dependentSignal of signal's dependent signals:
signal[kDependantSignals]?.forEach((s) => {
let dependentSignal = s.deref();

Check failure on line 387 in lib/internal/abort_controller.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'dependentSignal' is never reassigned. Use 'const' instead
// 1. If dependentSignal is not aborted, then:
if (dependentSignal && !dependentSignal[kAborted]) {
// 1. Set dependentSignal's abort reason to signal's abort reason.
dependentSignal[kReason] = reason;
// 2. Append dependentSignal to dependentSignalsToAbort.
ArrayPrototypePush(dependentSignalsToAbort, dependentSignal);
}
});
// 5. Run the abort steps for signal
const event = new Event('abort', {
[kTrustEvent]: true,
});
signal.dispatchEvent(event);
signal[kDependantSignals]?.forEach((s) => {
const signalRef = s.deref();
if (signalRef) abortSignal(signalRef, reason);
});
// 6. For each dependentSignal of dependentSignalsToAbort,
// run the abort steps for dependentSignal.
for (let i = 0; i < dependentSignalsToAbort.length; i++) {
const dependentSignal = dependentSignalsToAbort[i];
if (dependentSignal) abortSignal(dependentSignal, reason);
};
}

class AbortController {
Expand Down

0 comments on commit a40318c

Please sign in to comment.