UNPKG

node-worker-threads-pool-ts

Version:

Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Typescript, Async/Await.

46 lines (45 loc) 1.5 kB
/** * @typedef {import("./pool-worker").TaskConfig} TaskConfig */ export interface DynamicPoolWorkerParam { code: string; param?: any; } import { Pool } from "./pool"; import { DynamicTaskExecutor } from "./task-executor"; import { TaskFunc } from "./static-pool"; import { CommonWorkerSettings } from "./common"; export interface DynamicPoolOptions<ParamType, ResultType, WorkerData = any> { /** Function to be executed. */ task: TaskFunc<ParamType, ResultType>; /** Parameter for task function. */ param?: ParamType; /** * Data to pass into workers. * @deprecated since version 1.4.0. Please use parameter instead. */ workerData?: WorkerData; timeout?: number; } /** * Threads pool that can run different function * each call. */ export declare class DynamicPool extends Pool { /** * @param size Number of workers. * @param opt Some advanced settings. * */ constructor(size: number, opt?: CommonWorkerSettings); /** * Choose a idle worker to execute the function * with context provided. */ exec<ParamType = any, ResultType = any, WorkerData = any>(options: DynamicPoolOptions<ParamType, ResultType, WorkerData>): Promise<ResultType>; /** * Create a task executor of this pool. * This is used to apply some advanced settings to a task. */ createExecutor<ParamType, ResultType>(task: TaskFunc<ParamType, ResultType>): DynamicTaskExecutor<ParamType, ResultType>; }