Class Parallel

Index

Constructor methods

Properties

Methods

Constructor methods

constructor(data: T in Parallel<T>, opts?: ParallelOptions): Parallel

This is the constructor. Use it to new up any parallel jobs. The constructor takes an array of data you want to operate on. This data will be held in memory until you finish your job, and can be accessed via the .data attribute of your job. The object returned by the Parallel constructor is meant to be chained, so you can produce a chain of operations on the provided data.

Parameters

  • data: T in Parallel<T>

    This is the data you wish to operate on. Will often be an array, but the only restrictions are that your values are serializable as JSON.

  • opts?: ParallelOptions optional

    Some options for your job.

Returns

Parallel

Properties

public data: T in Parallel<T>

Data

Methods

public map(fn: (data: N) => N): Parallel

Map will apply the supplied function to every element in the wrapped data. Parallel will spawn one worker for each array element in the data, or the supplied maxWorkers argument. The values returned will be stored for further processing.

Parameters

  • fn: (data: N) => N

    A function to apply. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data.

Returns

Parallel

Parallel instance.

public reduce(fn: (data: N[]) => N): Parallel

Reduce applies an operation to every member of the wrapped data, and returns a scalar value produced by the operation. Use it for combining the results of a map operation, by summing numbers for example. This takes a reducing function, which gets an argument, data, an array of the stored value, and the current element.

Parameters

  • fn: (data: N[]) => N

    A function to apply. Receives the stored value and current element as argument. The value returned will be stored as the current value for the next iteration. Finally, the current value will be assigned to current data.

Returns

Parallel

Parallel instance.

public require(file: string): Parallel

If you have state that you want to share between your main thread and worker threads, this is how. Require takes either a string or a function. A string should point to a file name. NOte that in order to use require with a file name as an argument, you have to provide the evalPath property in the options object.

Parameters

  • file: string

Returns

Parallel

Parallel instance.

public require(fn: Function): Parallel

see

require

Parameters

  • fn: Function

Returns

Parallel

public spawn(fn: (data: T) => T): Parallel

This function will spawn a new process on a worker thread. Pass it the function you want to call. Your function will receive one argument, which is the current data. The value returned from your spawned function will update the current data.

Parameters

  • fn: (data: T) => T

    A function to execute on a worker thread. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data.

Returns

Parallel

Parallel instance.

public then(success: (data: T) => void, fail?: (e: Error) => void): Parallel

The functions given to then are called after the last requested operation has finished. success receives the resulting data object, while fail will receive an error object.

Parameters

  • success: (data: T) => void

    A function that gets called upon successful completion. Receives the wrapped data as an argument.

  • fail?: (e: Error) => void optional

    A function that gets called if the job fails. The function is passed an error object.

Returns

Parallel

Parallel instance.