@azure/abort-controller 1.0.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ import { AbortSignal, abortSignal, AbortSignalLike } from "./AbortSignal";
2
+
3
+ /**
4
+ * This error is thrown when an asynchronous operation has been aborted.
5
+ * Check for this error by testing the `name` that the name property of the
6
+ * error matches `"AbortError"`.
7
+ *
8
+ * @example
9
+ * const controller = new AbortController();
10
+ * controller.abort();
11
+ * try {
12
+ * doAsyncWork(controller.signal)
13
+ * } catch (e) {
14
+ * if (e.name === 'AbortError') {
15
+ * // handle abort error here.
16
+ * }
17
+ * }
18
+ */
19
+ export class AbortError extends Error {
20
+ constructor(message?: string) {
21
+ super(message);
22
+ this.name = "AbortError";
23
+ }
24
+ }
25
+
26
+ /**
27
+ * An AbortController provides an AbortSignal and the associated controls to signal
28
+ * that an asynchronous operation should be aborted.
29
+ *
30
+ * @example
31
+ * // Abort an operation when another event fires
32
+ * const controller = new AbortController();
33
+ * const signal = controller.signal;
34
+ * doAsyncWork(signal);
35
+ * button.addEventListener('click', () => controller.abort());
36
+ *
37
+ * @example
38
+ * // Share aborter cross multiple operations in 30s
39
+ * // Upload the same data to 2 different data centers at the same time,
40
+ * // abort another when any of them is finished
41
+ * const controller = AbortController.withTimeout(30 * 1000);
42
+ * doAsyncWork(controller.signal).then(controller.abort);
43
+ * doAsyncWork(controller.signal).then(controller.abort);
44
+ *
45
+ * @example
46
+ * // Cascaded aborting
47
+ * // All operations can't take more than 30 seconds
48
+ * const aborter = Aborter.timeout(30 * 1000);
49
+ *
50
+ * // Following 2 operations can't take more than 25 seconds
51
+ * await doAsyncWork(aborter.withTimeout(25 * 1000));
52
+ * await doAsyncWork(aborter.withTimeout(25 * 1000));
53
+ *
54
+ * @export
55
+ * @class AbortController
56
+ * @implements {AbortSignalLike}
57
+ */
58
+ export class AbortController {
59
+ private _signal: AbortSignal;
60
+
61
+ /**
62
+ * @param {AbortSignalLike[]} [parentSignals] The AbortSignals that will signal aborted on the AbortSignal associated with this controller.
63
+ * @constructor
64
+ */
65
+ constructor(parentSignals?: AbortSignalLike[]);
66
+ /**
67
+ * @param {...AbortSignalLike} parentSignals The AbortSignals that will signal aborted on the AbortSignal associated with this controller.
68
+ * @constructor
69
+ */
70
+ constructor(...parentSignals: AbortSignalLike[]);
71
+ constructor(parentSignals?: any) {
72
+ this._signal = new AbortSignal();
73
+
74
+ if (!parentSignals) {
75
+ return;
76
+ }
77
+ // coerce parentSignals into an array
78
+ if (!Array.isArray(parentSignals)) {
79
+ parentSignals = arguments;
80
+ }
81
+ for (const parentSignal of parentSignals) {
82
+ // if the parent signal has already had abort() called,
83
+ // then call abort on this signal as well.
84
+ if (parentSignal.aborted) {
85
+ this.abort();
86
+ } else {
87
+ // when the parent signal aborts, this signal should as well.
88
+ parentSignal.addEventListener("abort", () => {
89
+ this.abort();
90
+ });
91
+ }
92
+ }
93
+ }
94
+
95
+ /**
96
+ * The AbortSignal associated with this controller that will signal aborted
97
+ * when the abort method is called on this controller.
98
+ *
99
+ * @readonly
100
+ * @type {AbortSignal}
101
+ * @memberof AbortController
102
+ */
103
+ public get signal() {
104
+ return this._signal;
105
+ }
106
+
107
+ /**
108
+ * Signal that any operations passed this controller's associated abort signal
109
+ * to cancel any remaining work and throw an `AbortError`.
110
+ *
111
+ * @memberof AbortController
112
+ */
113
+ abort() {
114
+ abortSignal(this._signal);
115
+ }
116
+
117
+ /**
118
+ * Creates a new AbortSignal instance that will abort after the provided ms.
119
+ *
120
+ * @static
121
+ * @params {number} ms Elapsed time in milliseconds to trigger an abort.
122
+ * @returns {AbortSignal}
123
+ */
124
+ public static timeout(ms: number): AbortSignal {
125
+ const signal = new AbortSignal();
126
+ const timer = setTimeout(abortSignal, ms, signal);
127
+ // Prevent the active Timer from keeping the Node.js event loop active.
128
+ if (typeof timer.unref === "function") {
129
+ timer.unref();
130
+ }
131
+ return signal;
132
+ }
133
+ }
@@ -0,0 +1,159 @@
1
+ /// <reference path="./shims-public.d.ts" />
2
+ type AbortEventListener = (this: AbortSignalLike, ev?: any) => any;
3
+
4
+ const listenersMap = new WeakMap<AbortSignal, AbortEventListener[]>();
5
+ const abortedMap = new WeakMap<AbortSignal, boolean>();
6
+
7
+ /**
8
+ * Allows the request to be aborted upon firing of the "abort" event.
9
+ * Compatible with the browser built-in AbortSignal and common polyfills.
10
+ */
11
+ export interface AbortSignalLike {
12
+ /**
13
+ * Indicates if the signal has already been aborted.
14
+ */
15
+ readonly aborted: boolean;
16
+ /**
17
+ * Add new "abort" event listener, only support "abort" event.
18
+ */
19
+ addEventListener(
20
+ type: "abort",
21
+ listener: (this: AbortSignalLike, ev: any) => any,
22
+ options?: any
23
+ ): void;
24
+ /**
25
+ * Remove "abort" event listener, only support "abort" event.
26
+ */
27
+ removeEventListener(
28
+ type: "abort",
29
+ listener: (this: AbortSignalLike, ev: any) => any,
30
+ options?: any
31
+ ): void;
32
+ }
33
+
34
+ /**
35
+ * An aborter instance implements AbortSignal interface, can abort HTTP requests.
36
+ *
37
+ * - Call AbortSignal.none to create a new AbortSignal instance that cannot be cancelled.
38
+ * Use `AbortSignal.none` when you are required to pass a cancellation token but the operation
39
+ * cannot or will not ever be cancelled.
40
+ *
41
+ * @example
42
+ * // Abort without timeout
43
+ * await doAsyncWork(AbortSignal.none);
44
+ *
45
+ * @export
46
+ * @class AbortSignal
47
+ * @implements {AbortSignalLike}
48
+ */
49
+ export class AbortSignal implements AbortSignalLike {
50
+ constructor() {
51
+ listenersMap.set(this, []);
52
+ abortedMap.set(this, false);
53
+ }
54
+
55
+ /**
56
+ * Status of whether aborted or not.
57
+ *
58
+ * @readonly
59
+ * @type {boolean}
60
+ * @memberof AbortSignal
61
+ */
62
+ public get aborted(): boolean {
63
+ if (!abortedMap.has(this)) {
64
+ throw new TypeError("Expected `this` to be an instance of AbortSignal.");
65
+ }
66
+
67
+ return abortedMap.get(this)!;
68
+ }
69
+
70
+ /**
71
+ * Creates a new AbortSignal instance that will never be aborted.
72
+ *
73
+ * @readonly
74
+ * @static
75
+ * @type {AbortSignal}
76
+ * @memberof AbortSignal
77
+ */
78
+ public static get none(): AbortSignal {
79
+ return new AbortSignal();
80
+ }
81
+
82
+ /**
83
+ * onabort event listener.
84
+ *
85
+ * @memberof AbortSignal
86
+ */
87
+ public onabort?: (ev?: Event) => any;
88
+
89
+ /**
90
+ * Added new "abort" event listener, only support "abort" event.
91
+ *
92
+ * @param {"abort"} _type Only support "abort" event
93
+ * @param {(this: AbortSignalLike, ev: any) => any} listener
94
+ * @memberof AbortSignal
95
+ */
96
+ public addEventListener(
97
+ // tslint:disable-next-line:variable-name
98
+ _type: "abort",
99
+ listener: (this: AbortSignalLike, ev: any) => any
100
+ ): void {
101
+ if (!listenersMap.has(this)) {
102
+ throw new TypeError("Expected `this` to be an instance of AbortSignal.");
103
+ }
104
+
105
+ const listeners = listenersMap.get(this)!;
106
+ listeners.push(listener);
107
+ }
108
+
109
+ /**
110
+ * Remove "abort" event listener, only support "abort" event.
111
+ *
112
+ * @param {"abort"} _type Only support "abort" event
113
+ * @param {(this: AbortSignalLike, ev: any) => any} listener
114
+ * @memberof AbortSignal
115
+ */
116
+ public removeEventListener(
117
+ // tslint:disable-next-line:variable-name
118
+ _type: "abort",
119
+ listener: (this: AbortSignalLike, ev: any) => any
120
+ ): void {
121
+ if (!listenersMap.has(this)) {
122
+ throw new TypeError("Expected `this` to be an instance of AbortSignal.");
123
+ }
124
+
125
+ const listeners = listenersMap.get(this)!;
126
+
127
+ const index = listeners.indexOf(listener);
128
+ if (index > -1) {
129
+ listeners.splice(index, 1);
130
+ }
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Helper to trigger an abort event immediately, the onabort and all abort event listeners will be triggered.
136
+ * Will try to trigger abort event for all linked AbortSignal nodes.
137
+ *
138
+ * - If there is a timeout, the timer will be cancelled.
139
+ * - If aborted is true, nothing will happen.
140
+ *
141
+ * @returns
142
+ * @internal
143
+ */
144
+ export function abortSignal(signal: AbortSignal) {
145
+ if (signal.aborted) {
146
+ return;
147
+ }
148
+
149
+ if (signal.onabort) {
150
+ signal.onabort.call(signal);
151
+ }
152
+
153
+ const listeners = listenersMap.get(signal)!;
154
+ listeners.forEach((listener) => {
155
+ listener.call(signal);
156
+ });
157
+
158
+ abortedMap.set(signal, true);
159
+ }
package/src/aborter.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference lib="es5" />
2
+
3
+ // Changes to Aborter
4
+ // * Rename Aborter to AbortSignal
5
+ // * Remove withValue and getValue - async context should be solved differently/wholistically, not tied to cancellation
6
+ // * Remove withTimeout, it's moved to the controller
7
+ // * AbortSignal constructor no longer takes a parent. Cancellation graphs are created from the controller.
8
+
9
+ // Potential changes to align with DOM Spec
10
+ // * dispatchEvent on Signal
11
+
12
+ export { AbortController, AbortError } from "./AbortController";
13
+ export { AbortSignal, AbortSignalLike } from "./AbortSignal";
@@ -0,0 +1,2 @@
1
+ // forward declaration of Event in case DOM libs are not present.
2
+ interface Event {}
package/tsconfig.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Basic Options */
4
+ "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
5
+ "module": "es6" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
6
+ "lib": [] /* lib dependencies are triple-slash directives in lib/index.ts */,
7
+ "declaration": true /* Generates corresponding '.d.ts' file. */,
8
+ "declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
9
+ "sourceMap": true /* Generates corresponding '.map' file. */,
10
+ "outDir": "./dist-esm" /* Redirect output structure to the directory. */,
11
+ "stripInternal": true /* Do not emit declarations for code with @internal annotation*/,
12
+ "declarationDir": "./types" /* Output directory for generated declaration files.*/,
13
+ "importHelpers": true /* Import emit helpers from 'tslib'. */,
14
+ /* Strict Type-Checking Options */
15
+ "strict": true /* Enable all strict type-checking options. */,
16
+ "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
17
+ /* Additional Checks */
18
+ "noUnusedLocals": true /* Report errors on unused locals. */,
19
+ /* Module Resolution Options */
20
+ "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
21
+ "allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
22
+ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
23
+ /* Experimental Options */
24
+ "forceConsistentCasingInFileNames": true,
25
+ /* Other options */
26
+ "newLine": "LF" /* Use the specified end of line sequence to be used when emitting files: "crlf" (windows) or "lf" (unix).”*/,
27
+ "allowJs": false /* Don't allow JavaScript files to be compiled.*/
28
+ },
29
+ "compileOnSave": true,
30
+ "exclude": ["node_modules", "./types/**/*.d.ts", "./samples/**/*.ts"],
31
+ "include": ["./src/**/*.ts", "./test/**/*.ts"]
32
+ }
@@ -0,0 +1,90 @@
1
+ import { AbortSignal, AbortSignalLike } from "./AbortSignal";
2
+ /**
3
+ * This error is thrown when an asynchronous operation has been aborted.
4
+ * Check for this error by testing the `name` that the name property of the
5
+ * error matches `"AbortError"`.
6
+ *
7
+ * @example
8
+ * const controller = new AbortController();
9
+ * controller.abort();
10
+ * try {
11
+ * doAsyncWork(controller.signal)
12
+ * } catch (e) {
13
+ * if (e.name === 'AbortError') {
14
+ * // handle abort error here.
15
+ * }
16
+ * }
17
+ */
18
+ export declare class AbortError extends Error {
19
+ constructor(message?: string);
20
+ }
21
+ /**
22
+ * An AbortController provides an AbortSignal and the associated controls to signal
23
+ * that an asynchronous operation should be aborted.
24
+ *
25
+ * @example
26
+ * // Abort an operation when another event fires
27
+ * const controller = new AbortController();
28
+ * const signal = controller.signal;
29
+ * doAsyncWork(signal);
30
+ * button.addEventListener('click', () => controller.abort());
31
+ *
32
+ * @example
33
+ * // Share aborter cross multiple operations in 30s
34
+ * // Upload the same data to 2 different data centers at the same time,
35
+ * // abort another when any of them is finished
36
+ * const controller = AbortController.withTimeout(30 * 1000);
37
+ * doAsyncWork(controller.signal).then(controller.abort);
38
+ * doAsyncWork(controller.signal).then(controller.abort);
39
+ *
40
+ * @example
41
+ * // Cascaded aborting
42
+ * // All operations can't take more than 30 seconds
43
+ * const aborter = Aborter.timeout(30 * 1000);
44
+ *
45
+ * // Following 2 operations can't take more than 25 seconds
46
+ * await doAsyncWork(aborter.withTimeout(25 * 1000));
47
+ * await doAsyncWork(aborter.withTimeout(25 * 1000));
48
+ *
49
+ * @export
50
+ * @class AbortController
51
+ * @implements {AbortSignalLike}
52
+ */
53
+ export declare class AbortController {
54
+ private _signal;
55
+ /**
56
+ * @param {AbortSignalLike[]} [parentSignals] The AbortSignals that will signal aborted on the AbortSignal associated with this controller.
57
+ * @constructor
58
+ */
59
+ constructor(parentSignals?: AbortSignalLike[]);
60
+ /**
61
+ * @param {...AbortSignalLike} parentSignals The AbortSignals that will signal aborted on the AbortSignal associated with this controller.
62
+ * @constructor
63
+ */
64
+ constructor(...parentSignals: AbortSignalLike[]);
65
+ /**
66
+ * The AbortSignal associated with this controller that will signal aborted
67
+ * when the abort method is called on this controller.
68
+ *
69
+ * @readonly
70
+ * @type {AbortSignal}
71
+ * @memberof AbortController
72
+ */
73
+ readonly signal: AbortSignal;
74
+ /**
75
+ * Signal that any operations passed this controller's associated abort signal
76
+ * to cancel any remaining work and throw an `AbortError`.
77
+ *
78
+ * @memberof AbortController
79
+ */
80
+ abort(): void;
81
+ /**
82
+ * Creates a new AbortSignal instance that will abort after the provided ms.
83
+ *
84
+ * @static
85
+ * @params {number} ms Elapsed time in milliseconds to trigger an abort.
86
+ * @returns {AbortSignal}
87
+ */
88
+ static timeout(ms: number): AbortSignal;
89
+ }
90
+ //# sourceMappingURL=AbortController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbortController.d.ts","sourceRoot":"","sources":["../../src/AbortController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAe,eAAe,EAAE,MAAM,eAAe,CAAC;AAE1E;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,CAAC,EAAE,MAAM;CAI7B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,OAAO,CAAc;IAE7B;;;OAGG;gBACS,aAAa,CAAC,EAAE,eAAe,EAAE;IAC7C;;;OAGG;gBACS,GAAG,aAAa,EAAE,eAAe,EAAE;IAyB/C;;;;;;;OAOG;aACQ,MAAM;IAIjB;;;;;OAKG;IACH,KAAK;IAIL;;;;;;OAMG;WACW,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW;CAS/C"}
@@ -0,0 +1,77 @@
1
+ /// <reference path="../../src/shims-public.d.ts" />
2
+ /**
3
+ * Allows the request to be aborted upon firing of the "abort" event.
4
+ * Compatible with the browser built-in AbortSignal and common polyfills.
5
+ */
6
+ export interface AbortSignalLike {
7
+ /**
8
+ * Indicates if the signal has already been aborted.
9
+ */
10
+ readonly aborted: boolean;
11
+ /**
12
+ * Add new "abort" event listener, only support "abort" event.
13
+ */
14
+ addEventListener(type: "abort", listener: (this: AbortSignalLike, ev: any) => any, options?: any): void;
15
+ /**
16
+ * Remove "abort" event listener, only support "abort" event.
17
+ */
18
+ removeEventListener(type: "abort", listener: (this: AbortSignalLike, ev: any) => any, options?: any): void;
19
+ }
20
+ /**
21
+ * An aborter instance implements AbortSignal interface, can abort HTTP requests.
22
+ *
23
+ * - Call AbortSignal.none to create a new AbortSignal instance that cannot be cancelled.
24
+ * Use `AbortSignal.none` when you are required to pass a cancellation token but the operation
25
+ * cannot or will not ever be cancelled.
26
+ *
27
+ * @example
28
+ * // Abort without timeout
29
+ * await doAsyncWork(AbortSignal.none);
30
+ *
31
+ * @export
32
+ * @class AbortSignal
33
+ * @implements {AbortSignalLike}
34
+ */
35
+ export declare class AbortSignal implements AbortSignalLike {
36
+ constructor();
37
+ /**
38
+ * Status of whether aborted or not.
39
+ *
40
+ * @readonly
41
+ * @type {boolean}
42
+ * @memberof AbortSignal
43
+ */
44
+ readonly aborted: boolean;
45
+ /**
46
+ * Creates a new AbortSignal instance that will never be aborted.
47
+ *
48
+ * @readonly
49
+ * @static
50
+ * @type {AbortSignal}
51
+ * @memberof AbortSignal
52
+ */
53
+ static readonly none: AbortSignal;
54
+ /**
55
+ * onabort event listener.
56
+ *
57
+ * @memberof AbortSignal
58
+ */
59
+ onabort?: (ev?: Event) => any;
60
+ /**
61
+ * Added new "abort" event listener, only support "abort" event.
62
+ *
63
+ * @param {"abort"} _type Only support "abort" event
64
+ * @param {(this: AbortSignalLike, ev: any) => any} listener
65
+ * @memberof AbortSignal
66
+ */
67
+ addEventListener(_type: "abort", listener: (this: AbortSignalLike, ev: any) => any): void;
68
+ /**
69
+ * Remove "abort" event listener, only support "abort" event.
70
+ *
71
+ * @param {"abort"} _type Only support "abort" event
72
+ * @param {(this: AbortSignalLike, ev: any) => any} listener
73
+ * @memberof AbortSignal
74
+ */
75
+ removeEventListener(_type: "abort", listener: (this: AbortSignalLike, ev: any) => any): void;
76
+ }
77
+ //# sourceMappingURL=AbortSignal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbortSignal.d.ts","sourceRoot":"","sources":["../../src/AbortSignal.ts"],"names":[],"mappings":";AAMA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B;;OAEG;IACH,gBAAgB,CACd,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,GAAG,KAAK,GAAG,EACjD,OAAO,CAAC,EAAE,GAAG,GACZ,IAAI,CAAC;IACR;;OAEG;IACH,mBAAmB,CACjB,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,GAAG,KAAK,GAAG,EACjD,OAAO,CAAC,EAAE,GAAG,GACZ,IAAI,CAAC;CACT;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,WAAY,YAAW,eAAe;;IAMjD;;;;;;OAMG;aACQ,OAAO,EAAI,OAAO;IAQ7B;;;;;;;OAOG;oBACe,IAAI,EAAI,WAAW;IAIrC;;;;OAIG;IACI,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,KAAK,GAAG,CAAC;IAErC;;;;;;OAMG;IACI,gBAAgB,CAErB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,GAAG,KAAK,GAAG,GAChD,IAAI;IASP;;;;;;OAMG;IACI,mBAAmB,CAExB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,GAAG,KAAK,GAAG,GAChD,IAAI;CAYR"}
@@ -0,0 +1,4 @@
1
+ /// <reference lib="es5" />
2
+ export { AbortController, AbortError } from "./AbortController";
3
+ export { AbortSignal, AbortSignalLike } from "./AbortSignal";
4
+ //# sourceMappingURL=aborter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aborter.d.ts","sourceRoot":"","sources":["../../src/aborter.ts"],"names":[],"mappings":";AAWA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}