mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2025-08-26 20:49:22 +00:00
Add AbortSignal polyfills
This commit is contained in:
parent
cd5376ff49
commit
1e179b4396
78
src/lib/polyfills.ts
Normal file
78
src/lib/polyfills.ts
Normal file
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Polyfill for AbortSignal.any()
|
||||
*
|
||||
* AbortSignal.any() creates an AbortSignal that will be aborted when any of the
|
||||
* provided signals are aborted. This is useful for combining multiple abort signals.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
|
||||
*/
|
||||
|
||||
// Check if AbortSignal.any is already available
|
||||
if (!AbortSignal.any) {
|
||||
AbortSignal.any = function(signals: AbortSignal[]): AbortSignal {
|
||||
// If no signals provided, return a signal that never aborts
|
||||
if (signals.length === 0) {
|
||||
return new AbortController().signal;
|
||||
}
|
||||
|
||||
// If only one signal, return it directly for efficiency
|
||||
if (signals.length === 1) {
|
||||
return signals[0];
|
||||
}
|
||||
|
||||
// Check if any signal is already aborted
|
||||
for (const signal of signals) {
|
||||
if (signal.aborted) {
|
||||
// Create an already-aborted signal with the same reason
|
||||
const controller = new AbortController();
|
||||
controller.abort(signal.reason);
|
||||
return controller.signal;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new controller for the combined signal
|
||||
const controller = new AbortController();
|
||||
|
||||
// Function to abort the combined signal
|
||||
const onAbort = (event: Event) => {
|
||||
const target = event.target as AbortSignal;
|
||||
controller.abort(target.reason);
|
||||
};
|
||||
|
||||
// Listen for abort events on all input signals
|
||||
for (const signal of signals) {
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
}
|
||||
|
||||
// Clean up listeners when the combined signal is aborted
|
||||
controller.signal.addEventListener('abort', () => {
|
||||
for (const signal of signals) {
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
}
|
||||
}, { once: true });
|
||||
|
||||
return controller.signal;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Polyfill for AbortSignal.timeout()
|
||||
*
|
||||
* AbortSignal.timeout() creates an AbortSignal that will be aborted after a
|
||||
* specified number of milliseconds.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static
|
||||
*/
|
||||
|
||||
// Check if AbortSignal.timeout is already available
|
||||
if (!AbortSignal.timeout) {
|
||||
AbortSignal.timeout = function(milliseconds: number): AbortSignal {
|
||||
const controller = new AbortController();
|
||||
|
||||
setTimeout(() => {
|
||||
controller.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError'));
|
||||
}, milliseconds);
|
||||
|
||||
return controller.signal;
|
||||
};
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import { createRoot } from 'react-dom/client';
|
||||
|
||||
import App from './App.tsx'
|
||||
import './index.css'
|
||||
// Import polyfills first
|
||||
import './lib/polyfills.ts';
|
||||
|
||||
import App from './App.tsx';
|
||||
import './index.css';
|
||||
|
||||
// FIXME: a custom font should be used. Eg:
|
||||
// import '@fontsource-variable/<font-name>';
|
||||
|
Loading…
x
Reference in New Issue
Block a user