Skip to content

Commit

Permalink
Add fallback type for AbortSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Jan 5, 2025
1 parent 8927bc6 commit b6c113a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion etc/web-streams-polyfill.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export type AbortSignal = typeof globalThis extends {
AbortSignal: {
prototype: infer T;
};
} ? T : never;
} ? T : {
aborted: boolean;
reason?: any;
addEventListener(type: 'abort', listener: () => void): void;
removeEventListener(type: 'abort', listener: () => void): void;
};

// @public
export class ByteLengthQueuingStrategy implements QueuingStrategy<ArrayBufferView> {
Expand Down
12 changes: 10 additions & 2 deletions src/lib/abort-signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
*
* @public
*/
export type AbortSignal = typeof globalThis extends { AbortSignal: { prototype: infer T } } ? T : never;
export type AbortSignal = typeof globalThis extends { AbortSignal: { prototype: infer T } } ? T : {
aborted: boolean;
reason?: any;
addEventListener(type: 'abort', listener: () => void): void;
removeEventListener(type: 'abort', listener: () => void): void;
};

export function isAbortSignal(value: unknown): value is AbortSignal {
if (typeof value !== 'object' || value === null) {
Expand All @@ -31,7 +36,10 @@ export function isAbortSignal(value: unknown): value is AbortSignal {
*/
// Trick with globalThis inspired by @types/node
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0c370ead967cb97b1758d8fa15d09011fb3f58ea/types/node/globals.d.ts#L226
export type AbortController = typeof globalThis extends { AbortController: { prototype: infer T } } ? T : never;
export type AbortController = typeof globalThis extends { AbortController: { prototype: infer T } } ? T : {
signal: AbortSignal;
abort(reason?: any): void;
};

/**
* Construct a new AbortController, if supported by the platform.
Expand Down

0 comments on commit b6c113a

Please sign in to comment.