Skip to content

Commit

Permalink
馃挜 add TypeScript definitions (fixes #6)(#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog authored and mysticatea committed Dec 10, 2018
1 parent c909fcd commit dcc2506
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
40 changes: 40 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Event,
EventTarget,
AddEventListenerOptions,
EventListenerOptions,
EventTargetListener
} from "event-target-shim";

export as namespace AbortControllerShim;

export type AbortSignalListener = (this: AbortSignal, ev: any) => any;

export class AbortSignal extends EventTarget {
private constructor();

/**
* Returns true if this AbortSignal's AbortController has signaled to abort, and false
* otherwise.
*/
readonly aborted: boolean;
onabort: AbortSignalListener | null;
addEventListener(type: "abort", listener: AbortSignalListener, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventTargetListener | null, options?: boolean | AddEventListenerOptions): void;
removeEventListener(type: "abort", listener: AbortSignalListener, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventTargetListener | null, options?: boolean | EventListenerOptions): void;
}

export class AbortController {
/**
* Returns the AbortSignal object associated with this object.
*/
readonly signal: AbortSignal;

/**
* Invoking this method will set this object's AbortSignal's aborted flag and
* signal to any observers that the associated activity is to be aborted.
*/
abort(): void;
}
export default AbortController;
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"version": "1.1.0",
"description": "An implementation of WHATWG AbortController interface.",
"main": "dist/abort-controller",
"types": "index.d.ts",
"files": [
"dist",
"polyfill.*"
"polyfill.*",
"index.d.ts"
],
"engines": {
"node": ">=6.5"
Expand Down Expand Up @@ -40,7 +42,8 @@
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-watch": "^4.3.1"
"rollup-watch": "^4.3.1",
"typescript": "^3.2.1"
},
"scripts": {
"preversion": "npm test",
Expand All @@ -53,6 +56,7 @@
"test": "run-s -s lint test:*",
"test:mocha": "nyc --require ./scripts/babel-register.js mocha test/*.mjs",
"test:karma": "karma start scripts/karma.conf.js --single-run",
"test:types": "tsc",
"watch": "run-p -s watch:*",
"watch:mocha": "mocha test/*.mjs --require ./scripts/babel-register.js --watch --watch-extensions js,mjs --growl",
"watch:karma": "karma start scripts/karma.conf.js --watch",
Expand Down
30 changes: 30 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AbortController as AbortControllerShim, AbortSignal as AbortSignalShim } from "..";

function signalCallback(as: AbortSignal) { }
function signalShimCallback(as: AbortSignalShim) { }
function controllerCallback(ac: AbortController) { }
function controllerShimCallback(ac: AbortControllerShim) { }
function abortCallback(this: AbortSignal, ev: Event) { }

const controller = new AbortController();
const controllerShim = new AbortControllerShim();
const signal = controller.signal;
const signalShim = controllerShim.signal;

signalCallback(signal);
signalCallback(signalShim);

signalShimCallback(signalShim);

controllerCallback(controller);
controllerCallback(controllerShim);

controllerShimCallback(controllerShim);

if (signalShim.aborted === false) {
signalShim.addEventListener("abort", abortCallback);
signalShim.removeEventListener("abort", abortCallback);
signalShim.onabort = null;
signalShim.onabort = abortCallback;
}
controllerShim.abort();
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"lib": ["es2015", "dom"],
"noEmit": true,
"strict": true
},
"files": [
"test/types.ts"
],
"exclude": [
"node_modules"
]
}

0 comments on commit dcc2506

Please sign in to comment.