Skip to content

Commit 9e37ee7

Browse files
jeskewsrchase
authored andcommitted
Rename CancellationToken to AbortController (and bring into line with WHATWG spec) (#54)
* Rename CancellationToken to AbortController (and bring into line with WHATWG spec) * Rename abortcontroller types file to abort.ts
1 parent be5bcfd commit 9e37ee7

File tree

10 files changed

+163
-0
lines changed

10 files changed

+163
-0
lines changed

packages/abort-controller/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/build/

packages/abort-controller/.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
"author": "[email protected]",
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./AbortController";
2+
export * from "./AbortSignal";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"sourceMap": false,
5+
"inlineSourceMap": true,
6+
"inlineSources": true,
7+
"rootDir": "./src",
8+
"outDir": "./build"
9+
}
10+
}

0 commit comments

Comments
 (0)