parallel-es
Version:
Simple parallelization for EcmaScript
71 lines (70 loc) • 3.46 kB
TypeScript
import { ITask } from "../../task/task";
import { IParallelStream } from "./parallel-stream";
/**
* Stream that has been scheduled on the thread pool and waits for completion of its tasks.
* @param TSubResult type of the sub results
* @param TEndResult type of the end result
*/
export declare class ScheduledParallelStream<TSubResult, TEndResult> implements IParallelStream<TSubResult, TEndResult> {
/**
* The tasks executed by this stream.
* Already completed tasks are replaced with undefined to free the reference to the task (gc can collect the task)
*/
private tasks;
private innerStream;
/**
* Resolves the _promise with the given end result
*/
private resolve;
/**
* Rejects the _promise with the given reason
*/
private reject;
private next;
/**
* Number of still pending tasks
*/
private pending;
/**
* Indicator if any task has failed. If this is the case, then registered next handlers are no longer called
* for outstanding results.
* @type {boolean}
* @private
*/
private failed;
/**
* Function used to join the sub results to the end result
*/
private joiner;
/**
* The accumulated end result for all yet completed tasks. Is undefined if the stream is complete.
*/
private endResult;
/**
* Not yet joined sub results
*/
private subResults;
/**
* The index of the next expected sub result that should be joined with the end result
*/
private nextSubResultIndex;
constructor(tasks: ITask<TSubResult>[], endResultDefault: TEndResult, join: (memo: TSubResult, current: TSubResult) => TEndResult);
subscribe(onNext: (subResult: TSubResult, worker: 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)): Promise<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 registerTaskHandler(task);
private _taskCompleted(subResult, taskDefinition);
/**
* Joins the currently outstanding sub results. The sub results is needed to ensure that the results are joined in
* task order. This method iterates until it either reaches the end of the sub results or a sub result for a task is
* missing (undefined, not yet computed).
* Has a better memory footprint compared to if all subresults are kept.
*/
private joinSubResults();
private _taskFailed(reason);
}