Merge pull request #31344 from BendingBender/worker-threads-pool

Add types for worker-threads-pool
This commit is contained in:
Nathan Shively-Sanders
2018-12-17 10:17:45 -08:00
committed by GitHub
4 changed files with 100 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
// Type definitions for worker-threads-pool 2.0
// Project: https://github.com/watson/worker-threads-pool#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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;
}
}
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
@@ -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(() => {});