From 10721d9717d5d2bfbf7356a27182fa791eb7d0c1 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 13 Dec 2018 00:41:41 +0100 Subject: [PATCH] Add types for worker-threads-pool --- types/worker-threads-pool/index.d.ts | 53 +++++++++++++++++++ types/worker-threads-pool/tsconfig.json | 23 ++++++++ types/worker-threads-pool/tslint.json | 1 + .../worker-threads-pool-tests.ts | 23 ++++++++ 4 files changed, 100 insertions(+) create mode 100644 types/worker-threads-pool/index.d.ts create mode 100644 types/worker-threads-pool/tsconfig.json create mode 100644 types/worker-threads-pool/tslint.json create mode 100644 types/worker-threads-pool/worker-threads-pool-tests.ts diff --git a/types/worker-threads-pool/index.d.ts b/types/worker-threads-pool/index.d.ts new file mode 100644 index 0000000000..0a7e0ee0b0 --- /dev/null +++ b/types/worker-threads-pool/index.d.ts @@ -0,0 +1,53 @@ +// Type definitions for worker-threads-pool 2.0 +// Project: https://github.com/watson/worker-threads-pool#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { Worker, WorkerOptions } from 'worker_threads'; + +export = Pool; + +declare class Pool { + constructor(options?: Pool.Options); + + /** + * Number of active workers in the pool. + */ + size: number; + + /** + * @param filename argument passed directly to `new Worker(filename, options)` + * @param options argument passed directly to `new Worker(filename, options)` + * @param callback will be called once the worker is created + */ + acquire(filename: string, callback: (error: Error | null, worker: Worker) => void): void; + acquire( + filename: string, + options: WorkerOptions, + callback: (error: Error | null, worker: Worker) => void + ): void; + + /** + * Calls `worker.terminate()` on all workers in the pool. + * @param callback will be called once all workers have terminated + */ + destroy(callback?: () => void): void; +} + +declare namespace Pool { + interface Options { + /** + * Maximum number of workers allowed in the pool. Other workers will be queued + * and started once there's room in the pool. + * @default 1 + */ + max?: number; + /** + * Maximum number of workers waiting to be started when the pool is full. + * The callback to `pool.acquire` will be called with an error in case this limit is reached. + */ + maxWaiting?: number; + } +} diff --git a/types/worker-threads-pool/tsconfig.json b/types/worker-threads-pool/tsconfig.json new file mode 100644 index 0000000000..4f29ff4e7e --- /dev/null +++ b/types/worker-threads-pool/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "worker-threads-pool-tests.ts" + ] +} diff --git a/types/worker-threads-pool/tslint.json b/types/worker-threads-pool/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/worker-threads-pool/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/worker-threads-pool/worker-threads-pool-tests.ts b/types/worker-threads-pool/worker-threads-pool-tests.ts new file mode 100644 index 0000000000..f5cee7ea58 --- /dev/null +++ b/types/worker-threads-pool/worker-threads-pool-tests.ts @@ -0,0 +1,23 @@ +import Pool = require('worker-threads-pool'); + +const pool = new Pool({ max: 5 }); +new Pool({ maxWaiting: 5 }); + +// $ExpectType number +pool.size; + +pool.acquire('/my/worker.js', (err, worker) => { + // $ExpectType Error | null + err; + // $ExpectType Worker + worker; +}); +pool.acquire('/my/worker.js', { workerData: null }, (err, worker) => { + // $ExpectType Error | null + err; + // $ExpectType Worker + worker; +}); + +pool.destroy(); +pool.destroy(() => {});