From 9fb941e64443f7157dfd0de6d995a571aa358776 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Fri, 28 Jul 2017 21:45:51 +0200 Subject: [PATCH] [p-cancelable] introduce typings (#18478) --- types/p-cancelable/index.d.ts | 30 +++++++++++ types/p-cancelable/p-cancelable-tests.ts | 64 ++++++++++++++++++++++++ types/p-cancelable/tsconfig.json | 22 ++++++++ types/p-cancelable/tslint.json | 1 + 4 files changed, 117 insertions(+) create mode 100644 types/p-cancelable/index.d.ts create mode 100644 types/p-cancelable/p-cancelable-tests.ts create mode 100644 types/p-cancelable/tsconfig.json create mode 100644 types/p-cancelable/tslint.json diff --git a/types/p-cancelable/index.d.ts b/types/p-cancelable/index.d.ts new file mode 100644 index 0000000000..4f26cd17ca --- /dev/null +++ b/types/p-cancelable/index.d.ts @@ -0,0 +1,30 @@ +// Type definitions for p-cancelable 0.3 +// Project: https://github.com/sindresorhus/p-cancelable#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export = PCancelable; + +declare const PCancelable: PCancelableConstructor; + +interface PCancelableConstructor extends PromiseConstructor { + readonly prototype: PCancelable.PCancelable; + readonly CancelError: PCancelable.CancelErrorConstructor; + fn(wrapper: (onCancel: (fn?: () => void) => void, input: T) => PromiseLike): (input: T) => PCancelable.PCancelable; + new(executor: (onCancel: (fn?: () => void) => void, resolve: (value?: T | PromiseLike) => void, reject: (reason?: any) => void) => void): PCancelable.PCancelable; +} + +declare namespace PCancelable { + interface PCancelable extends Promise { + readonly canceled: boolean; + cancel(): void; + } + + interface CancelErrorConstructor extends ErrorConstructor { + new (): CancelError; + } + + interface CancelError extends Error { + readonly name: 'CancelError'; + } +} diff --git a/types/p-cancelable/p-cancelable-tests.ts b/types/p-cancelable/p-cancelable-tests.ts new file mode 100644 index 0000000000..ca5f728e18 --- /dev/null +++ b/types/p-cancelable/p-cancelable-tests.ts @@ -0,0 +1,64 @@ +/// + +import PCancelableCtor = require('p-cancelable'); +import { EventEmitter } from "events"; + +const cancelablePromise: PCancelableCtor.PCancelable<{}> = new PCancelableCtor((onCancel, resolve, reject) => { + class Worker extends EventEmitter { + close() { + } + } + + const worker = new Worker(); + + onCancel(() => { + worker.close(); + }); + + worker.on('finish', resolve); + worker.on('error', reject); +}); + +cancelablePromise + .then(value => { + console.log('Operation finished successfully:', value); + }) + .catch(reason => { + if (cancelablePromise.canceled) { + // Handle the cancelation here + console.log('Operation was canceled'); + return; + } + + throw reason; + }); + +setTimeout(() => { + cancelablePromise.cancel(); +}, 10000); + +const fn = PCancelableCtor.fn((onCancel: (fn?: () => void) => void, input: string) => { + const job = { + start() { + return Promise.resolve(10); + }, + cleanup() { + } + }; + + onCancel(() => { + job.cleanup(); + }); + + return job.start(); +}); + +const promise = fn('input'); +let num: number; +promise.then(innum => num = innum); +if (!promise.canceled) { + promise.cancel(); +} + +const err: PCancelableCtor.CancelError = new PCancelableCtor.CancelError(); +throw err; diff --git a/types/p-cancelable/tsconfig.json b/types/p-cancelable/tsconfig.json new file mode 100644 index 0000000000..2075e9d4f7 --- /dev/null +++ b/types/p-cancelable/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "p-cancelable-tests.ts" + ] +} diff --git a/types/p-cancelable/tslint.json b/types/p-cancelable/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/p-cancelable/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }