UNPKG

parallel-es

Version:
190 lines (189 loc) 7.06 kB
import { ITaskDefinition } from "../task/task-definition"; import { IFunctionDefinition } from "../function/function-defintion"; import { IFunctionId } from "../function/function-id"; /** * Message types */ export declare const enum WorkerMessageType { /** * Sent from the worker facade to the worker slave to initialize the slave. */ InitializeWorker = 0, /** * Sent from the worker facade to the worker slave to schedule a new task on the slave. */ ScheduleTask = 1, /** * Send from the worker slave to the worker thread to request the definition of a function needed to execute a scheduled task */ FunctionRequest = 2, /** * Send from the worker thread to the worker slave as response to a {@link WorkerMessageType.FunctionRequest}. Includes * the definitions of all requested functions */ FunctionResponse = 3, /** * Sent from the worker slave to the worker thread containing the computed result */ WorkerResult = 4, /** * Sent from the worker slave to the worker thread for the case an error occurred during the evaluation of the scheduled task. */ FunctionExecutionError = 5, /** * Sent from the worker thread to the worker slave to request the slave to terminate. */ Stop = 6, } /** * Message that is exchanged between a worker slave and the worker thread. */ export interface IWorkerMessage { /** * The type of the message. */ type: WorkerMessageType; } /** * Sent to initialize the worker slave and assigns the given unique id */ export interface IInitializeWorkerMessage extends IWorkerMessage { /** * Unique id of the worker (facade / slave) */ workerId: number; } /** * Schedules the given task on the worker slave. */ export interface IScheduleTaskMessage extends IWorkerMessage { /** * Task to execute on the worker slave */ task: ITaskDefinition; } /** * Sent by the worker slave to request the function definitions with the given ids. */ export interface IFunctionRequest extends IWorkerMessage { /** * The ids of the requested functions */ functionIds: IFunctionId[]; } /** * Response to a {@link IFunctionRequest}. Contains the definitions for all requested functions. */ export interface IFunctionResponse extends IWorkerMessage { /** * The definition of the requested functions */ functions: IFunctionDefinition[]; /** * Array containing the ids of the functions that could not be resolved */ missingFunctions: IFunctionId[]; } /** * Sent from the worker slave to the worker thread to report the computed result. * Thereafter, the worker slave is ready to accept further tasks. */ export interface IWorkerResultMessage extends IWorkerMessage { /** * The computed result for the {@link IScheduleTaskMessage} */ result: any; } /** * Sent from the worker to report an error during the execution of the function. */ export interface IFunctionExecutionError extends IWorkerMessage { /** * The occurred error. Not an instance of Error. Error is not cloneable. */ error: any; } /** * Creates an initialize worker message * @param id the unique id of the worker * @returns the initialize worker message */ export declare function initializeWorkerMessage(id: number): IInitializeWorkerMessage; /** * Creates a message to schedule the given task on a worker slave * @param task the task to schedule * @returns the schedule message */ export declare function scheduleTaskMessage(task: ITaskDefinition): IScheduleTaskMessage; /** * Creates an {@link IFunctionRequest} message that requests the given function ids from the worker thread * @param functionId the id of a function to request * @param otherFunctionIds additional ids to request from the worker slave * @returns the function request message */ export declare function requestFunctionMessage(functionId: IFunctionId, ...otherFunctionIds: IFunctionId[]): IFunctionRequest; /** * Creates a function response message containing the passed function definitions * @param functions the function definitions to respond to the worker slave * @returns the function response message */ export declare function functionResponseMessage(functions: IFunctionDefinition[], ...missingFunctionIds: IFunctionId[]): IFunctionResponse; /** * Creates a worker result message for the given result * @param result the computed result for the scheduled task * @returns the message */ export declare function workerResultMessage(result: any): IWorkerResultMessage; /** * Creates a function execution error message containing the given error * @param error the error object thrown by the task computation * @returns the message */ export declare function functionExecutionError(error: Error): IFunctionExecutionError; /** * Creates a stop message * @returns the message */ export declare function stopMessage(): IWorkerMessage; /** * Tests if the given message is an {@link IScheduleTaskMessage} message * @param message the message to test * @returns {boolean} {@code true} if the message is an {@link IScheduleTaskMessage} */ export declare function isScheduleTask(message: IWorkerMessage): message is IScheduleTaskMessage; /** * Tests if the given message is an {@link IInitializeWorkerMessage} message * @param message the message to test * @returns {boolean} {@code true} if the message is an {@link IInitializeWorkerMessage} */ export declare function isInitializeMessage(message: IWorkerMessage): message is IInitializeWorkerMessage; /** * Tests if the given message is an {@link IFunctionRequest} message * @param message the message to test * @returns {boolean} {@code true} if the message is an {@link IFunctionRequest} */ export declare function isFunctionRequest(message: IWorkerMessage): message is IFunctionRequest; /** * Tests if the given message is an {@link IFunctionResponse} message * @param message the message to test * @returns {boolean} {@code true} if the message is an {@link IFunctionResponse} */ export declare function isFunctionResponse(message: IWorkerMessage): message is IFunctionResponse; /** * Tests if the given message is an {@link IWorkerResultMessage} message * @param message the message to test * @returns {boolean} {@code true} if the message is an {@link IWorkerResultMessage} */ export declare function isWorkerResult(message: IWorkerMessage): message is IWorkerResultMessage; /** * Tests if the given message is an {@link IFunctionExecutionError} message * @param message the message to test * @returns {boolean} {@code true} if the message is an {@link IFunctionExecutionError} */ export declare function isFunctionExecutionError(message: IWorkerMessage): message is IFunctionExecutionError; /** * Tests if the given message is a stop message * @param message the message to test * @returns {boolean} {@code true} if the message is a stop message */ export declare function isStopMesssage(message: IWorkerMessage): boolean;