3
3
* via its associated `AbortController` object.
4
4
*
5
5
* @remarks
6
- * This interface is compatible with the `AbortSignal` interface defined in TypeScript's DOM types.
7
- * It is redefined here, so it can be polyfilled without a DOM, for example with
8
- * {@link https://www.npmjs.com/package/abortcontroller-polyfill | abortcontroller-polyfill} in a Node environment.
6
+ * This is equivalent to the `AbortSignal` interface defined in TypeScript's DOM types or `@types/node`.
9
7
*
10
8
* @public
11
9
*/
12
- export interface AbortSignal {
13
- /**
14
- * Whether the request is aborted.
15
- */
16
- readonly aborted : boolean ;
17
-
18
- /**
19
- * If aborted, returns the reason for aborting.
20
- */
21
- readonly reason ?: any ;
22
-
23
- /**
24
- * Add an event listener to be triggered when this signal becomes aborted.
25
- */
26
- addEventListener ( type : 'abort' , listener : ( ) => void ) : void ;
27
-
28
- /**
29
- * Remove an event listener that was previously added with {@link AbortSignal.addEventListener}.
30
- */
31
- removeEventListener ( type : 'abort' , listener : ( ) => void ) : void ;
32
- }
10
+ export type AbortSignal = typeof globalThis extends { AbortSignal : { prototype : infer T } } ? T : never ;
33
11
34
12
export function isAbortSignal ( value : unknown ) : value is AbortSignal {
35
13
if ( typeof value !== 'object' || value === null ) {
@@ -47,32 +25,22 @@ export function isAbortSignal(value: unknown): value is AbortSignal {
47
25
* A controller object that allows you to abort an `AbortSignal` when desired.
48
26
*
49
27
* @remarks
50
- * This interface is compatible with the `AbortController` interface defined in TypeScript's DOM types.
51
- * It is redefined here, so it can be polyfilled without a DOM, for example with
52
- * {@link https://www.npmjs.com/package/abortcontroller-polyfill | abortcontroller-polyfill} in a Node environment.
28
+ * This is equivalent to the `AbortController` interface defined in TypeScript's DOM types or `@types/node`.
53
29
*
54
30
* @internal
55
31
*/
56
- export interface AbortController {
57
- readonly signal : AbortSignal ;
58
-
59
- abort ( reason ?: any ) : void ;
60
- }
61
-
62
- interface AbortControllerConstructor {
63
- new ( ) : AbortController ;
64
- }
65
-
66
- const supportsAbortController = typeof ( AbortController as any ) === 'function' ;
32
+ // Trick with globalThis inspired by @types /node
33
+ // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0c370ead967cb97b1758d8fa15d09011fb3f58ea/types/node/globals.d.ts#L226
34
+ export type AbortController = typeof globalThis extends { AbortController : { prototype : infer T } } ? T : never ;
67
35
68
36
/**
69
37
* Construct a new AbortController, if supported by the platform.
70
38
*
71
39
* @internal
72
40
*/
73
41
export function createAbortController ( ) : AbortController | undefined {
74
- if ( supportsAbortController ) {
75
- return new ( AbortController as AbortControllerConstructor ) ( ) ;
42
+ if ( typeof AbortController === 'function' ) {
43
+ return new AbortController ( ) ;
76
44
}
77
45
return undefined ;
78
46
}
0 commit comments