UNPKG

restricted-parallel-processes

Version:

Spawn all processes, but limit number of parallel processes

114 lines (113 loc) 4.74 kB
/// <reference types="node" /> import * as childProcess from 'child_process'; import actions from 'restricted-concurrent-actions'; /** * Spawn a certain number of parallel subprocesses after subprocesses * @param params List of parameters * @param partLength Number of parallel subprocesses to be spawn at once * @param handleRemain Handle final remaining parameters * @returns An `AsyncIterableIterator` of arrays */ export declare function spawn(params: spawn.ParamList, partLength: spawn.PartLength, handleRemain?: spawn.RemainingHandler): spawn.ReturningPromise; export declare namespace spawn { type ParamList = ReadonlyArray<ParamItem>; type ParamItem = string | ArgvParams | ObjectParams; type ArgvParams = ReadonlyArray<string>; type PartLength = actions.PartLength; type Action = actions.Action<ResultItem>; type ActionList = actions.ActionList<ResultItem>; type RemainingHandler = actions.RemainingHandler<ResultItem>; type ReturningPromise = actions.ReturningPromise<ResultItem>; type ReturningValue = actions.ReturningValue<ResultItem>; type ChildProcess = childProcess.ChildProcess; const OMIT_EMPTY_REMAINING_PART: <X>(x: readonly X[]) => (readonly X[])[], KEEP_REMAINING_PART: <X>(x: X) => X[], OMIT_REMAINING_PART: () => never[], DEFAULT_REMAINING_HANDLER: <X>(x: readonly X[]) => (readonly X[])[]; interface ObjectParams { /** * Program name to execute * @see https://nodejs.org/api/child_process.html */ readonly command: string; /** * Arguments to pass to program * @see https://nodejs.org/api/child_process.html */ readonly argv?: ReadonlyArray<string>; /** * Spawning options * @see https://nodejs.org/api/child_process.html */ readonly options?: childProcess.SpawnOptions; /** * Function that will be called upon child process creation */ readonly oncreate?: ObjectParams.CreationEventHandler; } namespace ObjectParams { type CreationEventHandler = (event: CreationEvent) => void; class CreationEvent { readonly name = "CreationEvent"; readonly process: ChildProcess; readonly params: ObjectParams; readonly promise: CreationEvent.PromiseFunc; constructor(process: ChildProcess, params: ObjectParams, promise: CreationEvent.PromiseFunc); } namespace CreationEvent { interface PromiseFunc { readonly resolve: (value?: ResultItem | PromiseLike<ResultItem>) => void; readonly reject: (error: any) => void; } } } class ResultItem { /** * Created child process * @see https://nodejs.org/api/child_process.html */ readonly process: ChildProcess; /** * Returning exit status code * @see https://nodejs.org/api/child_process.html */ readonly status: number; /** * Exit signal * @see https://nodejs.org/api/child_process.html */ readonly signal: string | null | void; /** * Received stdout chunks */ readonly stdout: ResultItem.DataChunkList; /** * Received stderr chunks */ readonly stderr: ResultItem.DataChunkList; constructor(process: ChildProcess, status: number, signal: string | null | void, stdout: ResultItem.DataChunkList, stderr: ResultItem.DataChunkList); } namespace ResultItem { type DataChunkList = ReadonlyArray<DataChunk>; type DataChunk = Buffer | string; } class DisconnectionException extends Error { readonly process: ChildProcess; readonly params: ObjectParams; constructor(process: ChildProcess, params: ObjectParams); get name(): string; } function getActionList(params: ParamList): ActionList; function getObjectParam(item: ParamItem): ObjectParams; function getActionFunction(params: ObjectParams): Action; /** * Like `spawn`, but returns a `Promise` of arrays * @param params List of parameters * @param partLength Number of parallel subprocesses to be spawn at once * @param handleRemain Handle final remaining parameters * @returns A `Promise` of arrays */ function asArray(params: ParamList, partLength: PartLength, handleRemain?: RemainingHandler): asArray.ReturningPromise; namespace asArray { type ReturningPromise = actions.asArray.ReturningPromise<ResultItem>; type ReturningValue = actions.asArray.ReturningValue<ResultItem>; } } export default spawn;