Skip to content

Commit abd797f

Browse files
committed
fix abortSingalAll for runtimes not implementing FinalizationRegistry
1 parent 21313c3 commit abd797f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

packages/signal/src/abortSignalAll.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const allSignalRegistry = new FinalizationRegistry<() => void>((cb) => cb());
1+
const allSignalRegistry = globalThis.FinalizationRegistry
2+
? new FinalizationRegistry<() => void>((cb) => cb())
3+
: null;
24

35
const controllerInSignalSy = Symbol('CONTROLLER_IN_SIGNAL');
46

@@ -40,7 +42,7 @@ export function abortSignalAll(
4042

4143
function removeSignal(signal: AbortSignal, abortListener: () => void) {
4244
signal.removeEventListener('abort', abortListener);
43-
allSignalRegistry!.unregister(signal);
45+
allSignalRegistry?.unregister(signal);
4446
--retainedSignalsCount;
4547
}
4648

@@ -58,7 +60,7 @@ export function abortSignalAll(
5860
}
5961
signal.addEventListener('abort', onAbort);
6062
eventListenerPairs.push([signalRef, onAbort]);
61-
allSignalRegistry!.register(
63+
allSignalRegistry?.register(
6264
signal,
6365
() => {
6466
removeSignal(signal, onAbort);
@@ -74,7 +76,7 @@ export function abortSignalAll(
7476
function dispose() {
7577
const ctrl = ctrlRef.deref();
7678
if (ctrl) {
77-
allSignalRegistry!.unregister(ctrl.signal);
79+
allSignalRegistry?.unregister(ctrl.signal);
7880
// @ts-expect-error
7981
delete ctrl.signal[controllerInSignalSy];
8082
}
@@ -90,7 +92,7 @@ export function abortSignalAll(
9092
// cleanup when aborted
9193
ctrl.signal.addEventListener('abort', dispose);
9294
// cleanup when GCed
93-
allSignalRegistry!.register(ctrl.signal, dispose, ctrl.signal);
95+
allSignalRegistry?.register(ctrl.signal, dispose, ctrl.signal);
9496

9597
// keeping a strong reference of the controller binding it to the lifecycle of its signal
9698
// @ts-expect-error

0 commit comments

Comments
 (0)