File tree Expand file tree Collapse file tree 13 files changed +211
-1
lines changed Expand file tree Collapse file tree 13 files changed +211
-1
lines changed Original file line number Diff line number Diff line change
1
+ /node_modules /
2
+ /build /
Original file line number Diff line number Diff line change
1
+ /src /
2
+ /coverage /
3
+ tsconfig.test.json
4
+
5
+ * .spec.js
6
+ * .spec.d.ts
7
+ * .spec.js.map
8
+
9
+ * .mock.js
10
+ * .mock.d.ts
11
+ * .mock.js.map
12
+
13
+ * .fixture.js
14
+ * .fixture.d.ts
15
+ * .fixture.js.map
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " @aws/abort-controller" ,
3
+ "version" : " 0.0.1" ,
4
+ "private" : true ,
5
+ "description" : " A simple abort controller library" ,
6
+ "main" : " ./build/index.js" ,
7
+ "types" : " ./build/index.d.ts" ,
8
+ "scripts" : {
9
+ "prepublishOnly" : " tsc" ,
10
+ "pretest" : " tsc -p tsconfig.test.json" ,
11
+ "test" : " jest"
12
+ },
13
+
14
+ "license" : " Apache-2.0" ,
15
+ "dependencies" : {
16
+ "@aws/types" : " ^0.0.1"
17
+ },
18
+ "devDependencies" : {
19
+ "@types/jest" : " ^20.0.2" ,
20
+ "jest" : " ^20.0.4" ,
21
+ "typescript" : " ^2.3"
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ import { AbortController } from "./AbortController" ;
2
+ import { AbortSignal } from "./AbortSignal" ;
3
+
4
+ jest . useFakeTimers ( ) ;
5
+
6
+ describe ( 'AbortController' , ( ) => {
7
+ it ( 'should communicate cancellation via its signal' , ( ) => {
8
+ const source = new AbortController ( ) ;
9
+ const { signal} = source ;
10
+ expect ( signal ) . toBeInstanceOf ( AbortSignal ) ;
11
+ expect ( signal . aborted ) . toBe ( false ) ;
12
+
13
+ source . abort ( ) ;
14
+ expect ( signal . aborted ) . toBe ( true ) ;
15
+ } ) ;
16
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { AbortController as IAbortController } from "@aws/types" ;
2
+ import { AbortSignal } from "./AbortSignal" ;
3
+
4
+ export class AbortController implements IAbortController {
5
+ public readonly signal : AbortSignal = new AbortSignal ( ) ;
6
+
7
+ abort ( ) : void {
8
+ this . signal . abort ( ) ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ import { AbortController } from "./AbortController" ;
2
+ import { AbortSignal } from "./AbortSignal" ;
3
+
4
+ describe ( 'AbortSignal' , ( ) => {
5
+ it ( 'should report aborted to be false until the signal is aborted' , ( ) => {
6
+ const controller = new AbortController ( ) ;
7
+ const { signal} = controller ;
8
+ expect ( signal . aborted ) . toBe ( false ) ;
9
+
10
+ controller . abort ( ) ;
11
+ expect ( signal . aborted ) . toBe ( true ) ;
12
+ } ) ;
13
+
14
+ it ( 'should invoke the onabort handler when the signal is aborted' , ( ) => {
15
+ const controller = new AbortController ( ) ;
16
+ const { signal} = controller ;
17
+ const abortHandler = jest . fn ( ) ;
18
+ signal . onabort = abortHandler ;
19
+ expect ( abortHandler . mock . calls . length ) . toBe ( 0 ) ;
20
+ controller . abort ( ) ;
21
+ expect ( abortHandler . mock . calls . length ) . toBe ( 1 ) ;
22
+ } ) ;
23
+
24
+ it ( 'should not invoke the onabort handler multiple time' , ( ) => {
25
+ const controller = new AbortController ( ) ;
26
+ const { signal} = controller ;
27
+ const abortHandler = jest . fn ( ) ;
28
+ signal . onabort = abortHandler ;
29
+ expect ( abortHandler . mock . calls . length ) . toBe ( 0 ) ;
30
+ controller . abort ( ) ;
31
+ expect ( abortHandler . mock . calls . length ) . toBe ( 1 ) ;
32
+ controller . abort ( ) ;
33
+ expect ( abortHandler . mock . calls . length ) . toBe ( 1 ) ;
34
+ } ) ;
35
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import {
2
+ AbortSignal as IAbortSignal ,
3
+ AbortHandler ,
4
+ } from '@aws/types' ;
5
+
6
+ export class AbortSignal implements IAbortSignal {
7
+ public onabort ?: AbortHandler ;
8
+ private _aborted : boolean ;
9
+
10
+ constructor ( ) {
11
+ Object . defineProperty ( this , '_aborted' , {
12
+ value : false ,
13
+ writable : true ,
14
+ } ) ;
15
+ }
16
+
17
+ /**
18
+ * Whether the associated operation has already been cancelled.
19
+ */
20
+ get aborted ( ) : boolean {
21
+ return this . _aborted ;
22
+ }
23
+
24
+ /**
25
+ * @internal
26
+ */
27
+ abort ( ) : void {
28
+ this . _aborted = true ;
29
+ if ( this . onabort ) {
30
+ this . onabort ( ) ;
31
+ this . onabort = undefined ;
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ export * from "./AbortController" ;
2
+ export * from "./AbortSignal" ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "compilerOptions" : {
3
+ "module" : " commonjs" ,
4
+ "target" : " es5" ,
5
+ "lib" : [
6
+ " es5" ,
7
+ " es2015.collection"
8
+ ],
9
+ "strict" : true ,
10
+ "sourceMap" : true ,
11
+ "declaration" : true ,
12
+ "stripInternal" : true ,
13
+ "rootDir" : " ./src" ,
14
+ "outDir" : " ./build"
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "extends" : " ./tsconfig.json" ,
3
+ "compilerOptions" : {
4
+ "sourceMap" : false ,
5
+ "inlineSourceMap" : true ,
6
+ "inlineSources" : true ,
7
+ "rootDir" : " ./src" ,
8
+ "outDir" : " ./build"
9
+ }
10
+ }
Original file line number Diff line number Diff line change 11
11
"scripts" : {
12
12
"prepublishOnly" : " tsc" ,
13
13
"pretest" : " tsc" ,
14
- "test" : " "
14
+ "test" : " exit 0 "
15
15
},
16
16
17
17
"license" : " UNLICENSED"
Original file line number Diff line number Diff line change
1
+ export interface AbortHandler {
2
+ ( ) : void ;
3
+ }
4
+
5
+ /**
6
+ * Holders of an AbortSignal object may query if the associated operation has
7
+ * been aborted and register an onabort handler.
8
+ *
9
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
10
+ */
11
+ export interface AbortSignal {
12
+ /**
13
+ * Whether the action represented by this signal has been cancelled.
14
+ */
15
+ readonly aborted : boolean ;
16
+
17
+ /**
18
+ * A function to be invoked when the action represented by this signal has
19
+ * been cancelled.
20
+ */
21
+ onabort ?: AbortHandler ;
22
+ }
23
+
24
+ /**
25
+ * The AWS SDK uses a Controller/Signal model to allow for cooperative
26
+ * cancellation of asynchronous operations. When initiating such an operation,
27
+ * the caller can create an AbortController and then provide linked signal to
28
+ * subtasks. This allows a single source to communicate to multiple consumers
29
+ * that an action has been aborted without dictating how that cancellation
30
+ * should be handled.
31
+ *
32
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController
33
+ */
34
+ export interface AbortController {
35
+ /**
36
+ * An object that reports whether the action associated with this
37
+ * {AbortController} has been cancelled.
38
+ */
39
+ readonly signal : AbortSignal ;
40
+
41
+ /**
42
+ * Declares the operation associated with this AbortController to have been
43
+ * cancelled.
44
+ */
45
+ abort ( ) : void ;
46
+ }
Original file line number Diff line number Diff line change
1
+ export * from './abort' ;
1
2
export * from './credentials' ;
2
3
export * from './crypto' ;
3
4
export * from './http' ;
You can’t perform that action at this time.
0 commit comments