@azure/abort-controller 1.0.1 → 1.0.2
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.
- package/{changelog.md → CHANGELOG.md} +9 -3
- package/LICENSE +17 -17
- package/README.md +3 -16
- package/dist/index.js +28 -37
- package/dist/index.js.map +1 -1
- package/dist-esm/src/AbortController.js +17 -16
- package/dist-esm/src/AbortController.js.map +1 -1
- package/dist-esm/src/AbortSignal.js +12 -21
- package/dist-esm/src/AbortSignal.js.map +1 -1
- package/dist-esm/src/{aborter.js → index.js} +3 -1
- package/dist-esm/src/index.js.map +1 -0
- package/package.json +43 -44
- package/{src/shims-public.d.ts → shims-public.d.ts} +3 -0
- package/types/src/AbortController.d.ts +15 -20
- package/types/src/AbortController.d.ts.map +1 -1
- package/types/src/AbortSignal.d.ts +10 -22
- package/types/src/AbortSignal.d.ts.map +1 -1
- package/types/src/{aborter.d.ts → index.d.ts} +1 -1
- package/types/src/index.d.ts.map +1 -0
- package/dist-esm/src/aborter.js.map +0 -1
- package/src/AbortController.ts +0 -133
- package/src/AbortSignal.ts +0 -170
- package/src/aborter.ts +0 -11
- package/tsconfig.json +0 -11
- package/types/src/aborter.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure/abort-controller",
|
|
3
3
|
"sdk-type": "client",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"description": "Microsoft Azure SDK for JavaScript - Aborter",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
|
-
"module": "
|
|
7
|
+
"module": "dist-esm/src/index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
|
|
10
10
|
"build:es6": "tsc -p tsconfig.json",
|
|
11
11
|
"build:nodebrowser": "rollup -c 2>&1",
|
|
12
12
|
"build:test": "rollup -c rollup.test.config.js 2>&1",
|
|
13
13
|
"build": "npm run build:es6 && npm run build:nodebrowser",
|
|
14
|
-
"check-format": "prettier --list-different --config
|
|
15
|
-
"clean": "rimraf dist dist-esm dist-test types temp browser/*.js* browser/*.zip statistics.html coverage coverage-browser .nyc_output *.tgz *.log test*.xml TEST*.xml",
|
|
14
|
+
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
|
|
15
|
+
"clean": "rimraf dist dist-esm dist-test types temp dist-browser/*.js* dist-browser/*.zip statistics.html coverage coverage-browser .nyc_output *.tgz *.log test*.xml TEST*.xml",
|
|
16
16
|
"execute:samples": "echo skipped",
|
|
17
17
|
"extract-api": "tsc -p . && api-extractor run --local",
|
|
18
|
-
"format": "prettier --write --config
|
|
18
|
+
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
|
|
19
19
|
"integration-test:browser": "echo skipped",
|
|
20
20
|
"integration-test:node": "echo skipped",
|
|
21
21
|
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
|
|
22
|
-
"lint:fix": "eslint
|
|
23
|
-
"lint": "eslint -
|
|
22
|
+
"lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
|
|
23
|
+
"lint": "eslint package.json api-extractor.json src test --ext .ts",
|
|
24
24
|
"pack": "npm pack 2>&1",
|
|
25
25
|
"prebuild": "npm run clean",
|
|
26
26
|
"pretest": "npm run build:test",
|
|
@@ -28,60 +28,62 @@
|
|
|
28
28
|
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
|
|
29
29
|
"test": "npm run build:test && npm run unit-test && npm run integration-test",
|
|
30
30
|
"unit-test:browser": "karma start --single-run",
|
|
31
|
-
"unit-test:node": "cross-env TS_NODE_FILES=true TS_NODE_COMPILER_OPTIONS=\"{\\\"module\\\": \\\"commonjs\\\"}\" mocha --require ts-node/register --require source-map-support/register --reporter mocha-multi
|
|
32
|
-
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
|
|
31
|
+
"unit-test:node": "cross-env TS_NODE_FILES=true TS_NODE_COMPILER_OPTIONS=\"{\\\"module\\\": \\\"commonjs\\\"}\" mocha --require ts-node/register --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace --no-timeouts test/*.spec.ts",
|
|
32
|
+
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
|
|
33
|
+
"build:samples": "echo Skipped.",
|
|
34
|
+
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --mode file --out ./dist/docs ./src"
|
|
33
35
|
},
|
|
34
|
-
"types": "./types/src/
|
|
36
|
+
"types": "./types/src/index.d.ts",
|
|
35
37
|
"engine": {
|
|
36
38
|
"node": ">=8.0.0"
|
|
37
39
|
},
|
|
38
40
|
"files": [
|
|
39
41
|
"dist/",
|
|
40
42
|
"dist-esm/src/",
|
|
41
|
-
"
|
|
43
|
+
"shims-public.d.ts",
|
|
42
44
|
"types/src",
|
|
43
|
-
"
|
|
45
|
+
"README.md",
|
|
46
|
+
"LICENSE"
|
|
44
47
|
],
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"url": "git+https://github.com/Azure/azure-sdk-for-js.git"
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=8.0.0"
|
|
48
50
|
},
|
|
51
|
+
"repository": "github:Azure/azure-sdk-for-js",
|
|
49
52
|
"keywords": [
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
53
|
+
"azure",
|
|
54
|
+
"aborter",
|
|
55
|
+
"abortsignal",
|
|
56
|
+
"cancellation",
|
|
57
|
+
"node.js",
|
|
58
|
+
"typescript",
|
|
59
|
+
"javascript",
|
|
60
|
+
"browser",
|
|
61
|
+
"cloud"
|
|
58
62
|
],
|
|
59
63
|
"author": "Microsoft Corporation",
|
|
60
64
|
"license": "MIT",
|
|
61
65
|
"bugs": {
|
|
62
66
|
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
|
|
63
67
|
},
|
|
64
|
-
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/core/abort-controller",
|
|
68
|
+
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/core/abort-controller/README.md",
|
|
65
69
|
"sideEffects": false,
|
|
66
70
|
"dependencies": {
|
|
67
|
-
"tslib": "^
|
|
71
|
+
"tslib": "^2.0.0"
|
|
68
72
|
},
|
|
69
73
|
"devDependencies": {
|
|
70
|
-
"@
|
|
74
|
+
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
|
|
75
|
+
"@microsoft/api-extractor": "7.7.11",
|
|
76
|
+
"@rollup/plugin-commonjs": "11.0.2",
|
|
77
|
+
"@rollup/plugin-multi-entry": "^3.0.0",
|
|
78
|
+
"@rollup/plugin-node-resolve": "^8.0.0",
|
|
71
79
|
"@rollup/plugin-replace": "^2.2.0",
|
|
72
|
-
"@types/mocha": "^
|
|
80
|
+
"@types/mocha": "^7.0.2",
|
|
73
81
|
"@types/node": "^8.0.0",
|
|
74
|
-
"@typescript-eslint/eslint-plugin": "^2.0.0",
|
|
75
|
-
"@typescript-eslint/parser": "^2.0.0",
|
|
76
82
|
"assert": "^1.4.1",
|
|
77
|
-
"cross-env": "^
|
|
83
|
+
"cross-env": "^7.0.2",
|
|
78
84
|
"delay": "^4.2.0",
|
|
79
|
-
"eslint": "^
|
|
80
|
-
"
|
|
81
|
-
"eslint-plugin-no-null": "^1.0.2",
|
|
82
|
-
"eslint-plugin-no-only-tests": "^2.3.0",
|
|
83
|
-
"eslint-plugin-promise": "^4.1.1",
|
|
84
|
-
"karma": "^4.0.1",
|
|
85
|
+
"eslint": "^7.15.0",
|
|
86
|
+
"karma": "^5.1.0",
|
|
85
87
|
"karma-chrome-launcher": "^3.0.0",
|
|
86
88
|
"karma-coverage": "^2.0.0",
|
|
87
89
|
"karma-edge-launcher": "^0.4.2",
|
|
@@ -89,22 +91,19 @@
|
|
|
89
91
|
"karma-firefox-launcher": "^1.1.0",
|
|
90
92
|
"karma-ie-launcher": "^1.0.0",
|
|
91
93
|
"karma-junit-reporter": "^2.0.1",
|
|
92
|
-
"karma-mocha": "^
|
|
94
|
+
"karma-mocha": "^2.0.1",
|
|
93
95
|
"karma-mocha-reporter": "^2.2.5",
|
|
94
|
-
"karma-remap-
|
|
95
|
-
"mocha": "^
|
|
96
|
+
"karma-remap-istanbul": "^0.6.0",
|
|
97
|
+
"mocha": "^7.1.1",
|
|
96
98
|
"mocha-junit-reporter": "^1.18.0",
|
|
97
|
-
"mocha-multi": "^1.1.3",
|
|
98
99
|
"nyc": "^14.0.0",
|
|
99
100
|
"prettier": "^1.16.4",
|
|
100
101
|
"rimraf": "^3.0.0",
|
|
101
102
|
"rollup": "^1.16.3",
|
|
102
|
-
"rollup-plugin-commonjs": "^10.0.0",
|
|
103
|
-
"rollup-plugin-multi-entry": "^2.1.0",
|
|
104
|
-
"rollup-plugin-node-resolve": "^5.0.2",
|
|
105
103
|
"rollup-plugin-sourcemaps": "^0.4.2",
|
|
106
104
|
"rollup-plugin-terser": "^5.1.1",
|
|
107
105
|
"ts-node": "^8.3.0",
|
|
108
|
-
"typescript": "
|
|
106
|
+
"typescript": "4.1.2",
|
|
107
|
+
"typedoc": "0.15.0"
|
|
109
108
|
}
|
|
110
109
|
}
|
|
@@ -5,6 +5,7 @@ import { AbortSignal, AbortSignalLike } from "./AbortSignal";
|
|
|
5
5
|
* error matches `"AbortError"`.
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
|
+
* ```ts
|
|
8
9
|
* const controller = new AbortController();
|
|
9
10
|
* controller.abort();
|
|
10
11
|
* try {
|
|
@@ -14,6 +15,7 @@ import { AbortSignal, AbortSignalLike } from "./AbortSignal";
|
|
|
14
15
|
* // handle abort error here.
|
|
15
16
|
* }
|
|
16
17
|
* }
|
|
18
|
+
* ```
|
|
17
19
|
*/
|
|
18
20
|
export declare class AbortError extends Error {
|
|
19
21
|
constructor(message?: string);
|
|
@@ -23,43 +25,43 @@ export declare class AbortError extends Error {
|
|
|
23
25
|
* that an asynchronous operation should be aborted.
|
|
24
26
|
*
|
|
25
27
|
* @example
|
|
26
|
-
*
|
|
28
|
+
* Abort an operation when another event fires
|
|
29
|
+
* ```ts
|
|
27
30
|
* const controller = new AbortController();
|
|
28
31
|
* const signal = controller.signal;
|
|
29
32
|
* doAsyncWork(signal);
|
|
30
33
|
* button.addEventListener('click', () => controller.abort());
|
|
34
|
+
* ```
|
|
31
35
|
*
|
|
32
36
|
* @example
|
|
33
|
-
*
|
|
37
|
+
* Share aborter cross multiple operations in 30s
|
|
38
|
+
* ```ts
|
|
34
39
|
* // Upload the same data to 2 different data centers at the same time,
|
|
35
40
|
* // abort another when any of them is finished
|
|
36
41
|
* const controller = AbortController.withTimeout(30 * 1000);
|
|
37
42
|
* doAsyncWork(controller.signal).then(controller.abort);
|
|
38
43
|
* doAsyncWork(controller.signal).then(controller.abort);
|
|
44
|
+
*```
|
|
39
45
|
*
|
|
40
46
|
* @example
|
|
41
|
-
*
|
|
47
|
+
* Cascaded aborting
|
|
48
|
+
* ```ts
|
|
42
49
|
* // All operations can't take more than 30 seconds
|
|
43
50
|
* const aborter = Aborter.timeout(30 * 1000);
|
|
44
51
|
*
|
|
45
52
|
* // Following 2 operations can't take more than 25 seconds
|
|
46
53
|
* await doAsyncWork(aborter.withTimeout(25 * 1000));
|
|
47
54
|
* await doAsyncWork(aborter.withTimeout(25 * 1000));
|
|
48
|
-
*
|
|
49
|
-
* @export
|
|
50
|
-
* @class AbortController
|
|
51
|
-
* @implements {AbortSignalLike}
|
|
55
|
+
* ```
|
|
52
56
|
*/
|
|
53
57
|
export declare class AbortController {
|
|
54
58
|
private _signal;
|
|
55
59
|
/**
|
|
56
|
-
* @param
|
|
57
|
-
* @constructor
|
|
60
|
+
* @param parentSignals - The AbortSignals that will signal aborted on the AbortSignal associated with this controller.
|
|
58
61
|
*/
|
|
59
62
|
constructor(parentSignals?: AbortSignalLike[]);
|
|
60
63
|
/**
|
|
61
|
-
* @param
|
|
62
|
-
* @constructor
|
|
64
|
+
* @param parentSignals - The AbortSignals that will signal aborted on the AbortSignal associated with this controller.
|
|
63
65
|
*/
|
|
64
66
|
constructor(...parentSignals: AbortSignalLike[]);
|
|
65
67
|
/**
|
|
@@ -67,23 +69,16 @@ export declare class AbortController {
|
|
|
67
69
|
* when the abort method is called on this controller.
|
|
68
70
|
*
|
|
69
71
|
* @readonly
|
|
70
|
-
* @type {AbortSignal}
|
|
71
|
-
* @memberof AbortController
|
|
72
72
|
*/
|
|
73
|
-
|
|
73
|
+
get signal(): AbortSignal;
|
|
74
74
|
/**
|
|
75
75
|
* Signal that any operations passed this controller's associated abort signal
|
|
76
76
|
* to cancel any remaining work and throw an `AbortError`.
|
|
77
|
-
*
|
|
78
|
-
* @memberof AbortController
|
|
79
77
|
*/
|
|
80
78
|
abort(): void;
|
|
81
79
|
/**
|
|
82
80
|
* 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}
|
|
81
|
+
* @param ms - Elapsed time in milliseconds to trigger an abort.
|
|
87
82
|
*/
|
|
88
83
|
static timeout(ms: number): AbortSignal;
|
|
89
84
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbortController.d.ts","sourceRoot":"","sources":["../../src/AbortController.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AbortController.d.ts","sourceRoot":"","sources":["../../src/AbortController.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAe,eAAe,EAAE,MAAM,eAAe,CAAC;AAE1E;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,CAAC,EAAE,MAAM;CAI7B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,OAAO,CAAc;IAE7B;;OAEG;gBACS,aAAa,CAAC,EAAE,eAAe,EAAE;IAC7C;;OAEG;gBACS,GAAG,aAAa,EAAE,eAAe,EAAE;IA2B/C;;;;;OAKG;IACH,IAAW,MAAM,IAAI,WAAW,CAE/B;IAED;;;OAGG;IACH,KAAK,IAAI,IAAI;IAIb;;;OAGG;WACW,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW;CAS/C"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/// <reference path="../../
|
|
1
|
+
/// <reference path="../../shims-public.d.ts" />
|
|
2
2
|
/**
|
|
3
3
|
* Allows the request to be aborted upon firing of the "abort" event.
|
|
4
4
|
* Compatible with the browser built-in AbortSignal and common polyfills.
|
|
@@ -25,12 +25,10 @@ export interface AbortSignalLike {
|
|
|
25
25
|
* cannot or will not ever be cancelled.
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
|
-
*
|
|
28
|
+
* Abort without timeout
|
|
29
|
+
* ```ts
|
|
29
30
|
* await doAsyncWork(AbortSignal.none);
|
|
30
|
-
*
|
|
31
|
-
* @export
|
|
32
|
-
* @class AbortSignal
|
|
33
|
-
* @implements {AbortSignalLike}
|
|
31
|
+
* ```
|
|
34
32
|
*/
|
|
35
33
|
export declare class AbortSignal implements AbortSignalLike {
|
|
36
34
|
constructor();
|
|
@@ -38,39 +36,30 @@ export declare class AbortSignal implements AbortSignalLike {
|
|
|
38
36
|
* Status of whether aborted or not.
|
|
39
37
|
*
|
|
40
38
|
* @readonly
|
|
41
|
-
* @type {boolean}
|
|
42
|
-
* @memberof AbortSignal
|
|
43
39
|
*/
|
|
44
|
-
|
|
40
|
+
get aborted(): boolean;
|
|
45
41
|
/**
|
|
46
42
|
* Creates a new AbortSignal instance that will never be aborted.
|
|
47
43
|
*
|
|
48
44
|
* @readonly
|
|
49
|
-
* @static
|
|
50
|
-
* @type {AbortSignal}
|
|
51
|
-
* @memberof AbortSignal
|
|
52
45
|
*/
|
|
53
|
-
static
|
|
46
|
+
static get none(): AbortSignal;
|
|
54
47
|
/**
|
|
55
48
|
* onabort event listener.
|
|
56
|
-
*
|
|
57
|
-
* @memberof AbortSignal
|
|
58
49
|
*/
|
|
59
50
|
onabort: ((ev?: Event) => any) | null;
|
|
60
51
|
/**
|
|
61
52
|
* Added new "abort" event listener, only support "abort" event.
|
|
62
53
|
*
|
|
63
|
-
* @param
|
|
64
|
-
* @param
|
|
65
|
-
* @memberof AbortSignal
|
|
54
|
+
* @param _type - Only support "abort" event
|
|
55
|
+
* @param listener - The listener to be added
|
|
66
56
|
*/
|
|
67
57
|
addEventListener(_type: "abort", listener: (this: AbortSignalLike, ev: any) => any): void;
|
|
68
58
|
/**
|
|
69
59
|
* Remove "abort" event listener, only support "abort" event.
|
|
70
60
|
*
|
|
71
|
-
* @param
|
|
72
|
-
* @param
|
|
73
|
-
* @memberof AbortSignal
|
|
61
|
+
* @param _type - Only support "abort" event
|
|
62
|
+
* @param listener - The listener to be removed
|
|
74
63
|
*/
|
|
75
64
|
removeEventListener(_type: "abort", listener: (this: AbortSignalLike, ev: any) => any): void;
|
|
76
65
|
/**
|
|
@@ -85,7 +74,6 @@ export declare class AbortSignal implements AbortSignalLike {
|
|
|
85
74
|
* - If there is a timeout, the timer will be cancelled.
|
|
86
75
|
* - If aborted is true, nothing will happen.
|
|
87
76
|
*
|
|
88
|
-
* @returns
|
|
89
77
|
* @internal
|
|
90
78
|
*/
|
|
91
79
|
export declare function abortSignal(signal: AbortSignal): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbortSignal.d.ts","sourceRoot":"","sources":["../../src/AbortSignal.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"AbortSignal.d.ts","sourceRoot":"","sources":["../../src/AbortSignal.ts"],"names":[],"mappings":";AAWA;;;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;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAY,YAAW,eAAe;;IAMjD;;;;OAIG;IACH,IAAW,OAAO,IAAI,OAAO,CAM5B;IAED;;;;OAIG;IACH,WAAkB,IAAI,IAAI,WAAW,CAEpC;IAED;;OAEG;IACI,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,KAAK,GAAG,CAAC,GAAG,IAAI,CAAQ;IAEpD;;;;;OAKG;IACI,gBAAgB,CAErB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,GAAG,KAAK,GAAG,GAChD,IAAI;IASP;;;;;OAKG;IACI,mBAAmB,CAExB,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,GAAG,KAAK,GAAG,GAChD,IAAI;IAaP;;OAEG;IACH,aAAa,CAAC,MAAM,EAAE,KAAK,GAAG,OAAO;CAKtC;AAED;;;;;;;;GAQG;AAEH,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAiBrD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"aborter.js","sourceRoot":"","sources":["../../src/aborter.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,kCAAkC;AAClC,uHAAuH;AACvH,qDAAqD;AACrD,2GAA2G;AAE3G,2CAA2C;AAC3C,4BAA4B;AAE5B,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,WAAW,EAAmB,MAAM,eAAe,CAAC"}
|
package/src/AbortController.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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
|
-
}
|
package/src/AbortSignal.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
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) | null = null;
|
|
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
|
-
* Dispatches a synthetic event to the AbortSignal.
|
|
135
|
-
*/
|
|
136
|
-
dispatchEvent(_event: Event): boolean {
|
|
137
|
-
throw new Error(
|
|
138
|
-
"This is a stub dispatchEvent implementation that should not be used. It only exists for type-checking purposes."
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Helper to trigger an abort event immediately, the onabort and all abort event listeners will be triggered.
|
|
145
|
-
* Will try to trigger abort event for all linked AbortSignal nodes.
|
|
146
|
-
*
|
|
147
|
-
* - If there is a timeout, the timer will be cancelled.
|
|
148
|
-
* - If aborted is true, nothing will happen.
|
|
149
|
-
*
|
|
150
|
-
* @returns
|
|
151
|
-
* @internal
|
|
152
|
-
*/
|
|
153
|
-
export function abortSignal(signal: AbortSignal) {
|
|
154
|
-
if (signal.aborted) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (signal.onabort) {
|
|
159
|
-
signal.onabort.call(signal);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const listeners = listenersMap.get(signal)!;
|
|
163
|
-
if (listeners) {
|
|
164
|
-
listeners.forEach((listener) => {
|
|
165
|
-
listener.call(signal, { type: "abort" });
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
abortedMap.set(signal, true);
|
|
170
|
-
}
|