UNPKG

parallel-es

Version:
81 lines (80 loc) 4.93 kB
import { IPromise } from "../../util/promise"; import { IParallelStream } from "./parallel-stream"; import { ITask } from "../../task/task"; /** * Function that resolves the next sub result for a parallel stream * @param subResult the sub result * @param taskIndex the job relative index of the task that has computed the sub result * @param valuesPerTask the number of values each task has to process at most * @param TSubResult type of the sub result */ export declare type INextCallback<TSubResult> = (subResult: TSubResult, taskIndex: number, valuesPerTask: number) => any; /** * Function that resolves the end result of a parallel stream * @param result the end result of the stream * @param TEndResult type of the end result */ export declare type IResolveCallback<TEndResult> = (result: TEndResult) => any; /** * Function to reject a parallel stream * @param reason the rejection reason */ export declare type IRejectCallback = (reason: any) => any; /** * Callback that is invoked for a new {@link ParallelStream} * * @param next callback to invoke to trigger the next sub result * @param resolve callback to invoke to resolve the stream * @param reject callback to invoke to reject the stream */ export declare type IExecutorCallback<TSubResult, TEndResult> = (next: INextCallback<TSubResult>, resolve: IResolveCallback<TEndResult>, reject: IRejectCallback) => any; /** * Generic parallel stream that can be coordinated using the provided next, resolve and reject callbacks. * @param TSubResult type of the sub results * @param TEndResult type of the end result */ export declare class ParallelStream<TSubResult, TEndResult> implements IParallelStream<TSubResult, TEndResult> { /** * Creates a new parallel that is based on the given input stream but transforms the end result using the given transformer * @param inputStream the input stream on which the returned stream is based on * @param transformer the transformer used to transform the end result * @param TIn type of the input elements for this stream * @param TIntermediate type of the end results from the input stream * @param TResult end result after applying the transformer. * @returns parallel stream that is based on the given input stream but with the transformed end result */ static transform<TIn, TIntermediate, TResult>(inputStream: IParallelStream<TIn, TIntermediate>, transformer: (result: TIntermediate) => TResult): ParallelStream<TIn, TResult>; /** * Creates a new parallel stream for the given set of tasks. * @param tasks the set of tasks that compute the results of the stream * @param defaultResult the default result * @param joiner the joiner to use to join two computed task results * @param TTaskResult type of the task results * @param TEndResult result of the created stream. Created by applying the end results of the stream to the joiner * @returns stream for the given set of tasks */ static fromTasks<TTaskResult, TEndResult>(tasks: ITask<TTaskResult>[], defaultResult: TEndResult, joiner: (memo: TTaskResult, current: TTaskResult) => TEndResult): IParallelStream<TTaskResult, TEndResult>; private promise; /** * Registered handlers that should be called for each sub result * @type {Array} * @private */ private nextHandlers; private resolve; private reject; /** * Creates a new, generic parallel stream * @param executor the executor function that gets passed the next, resolve and reject functions */ constructor(executor: IExecutorCallback<TSubResult, TEndResult>); subscribe(onNext: (subResult: TSubResult, taskIndex: number, valuesPerWorker: number) => void, onError?: (reason: any) => void, onComplete?: (result: TEndResult) => void): IParallelStream<TSubResult, TEndResult>; then<TResult1, TResult2>(onfulfilled: (value: TEndResult) => (PromiseLike<TResult1> | TResult1), onrejected: (reason: any) => (PromiseLike<TResult2> | TResult2)): Promise<TResult2 | TResult1>; then<TResult>(onfulfilled: (value: TEndResult) => (PromiseLike<TResult> | TResult), onrejected: (reason: any) => (PromiseLike<TResult> | TResult)): Promise<TResult>; then<TResult>(onfulfilled: (value: TEndResult) => (PromiseLike<TResult> | TResult)): IPromise<TResult>; then<TResult>(onfulfilled?: (value: TEndResult) => (PromiseLike<TResult> | TResult), onrejected?: (reason: any) => (PromiseLike<TResult> | TResult)): Promise<TResult>; then<TResult>(onfulfilled?: (value: TEndResult) => (PromiseLike<TResult> | TResult), onrejected?: (reason: any) => void): Promise<TResult>; catch<TResult>(onrejected: (reason: any) => (PromiseLike<TResult> | TResult)): Promise<TResult | TEndResult>; catch(onrejected: (reason: any) => (PromiseLike<TEndResult> | TEndResult)): Promise<TEndResult>; private _next(subResult, taskIndex, valuesPerTask); }