Options
All
  • Public
  • Public/Protected
  • All
Menu

Class WorkerPool

Class to manage multiple workers, send tasks to them and receive results.

Hierarchy

  • WorkerPool

Index

Constructors

constructor

  • Parameters

    • Default value workers: Worker[] = []

      Workers to be added to this pool immediately.

    Returns WorkerPool

Properties

Private idleWorkers

idleWorkers: WorkerWrapper[]

List of the idle workers in this pool.

Private tasks

tasks: WorkerTask[]

List of queued tasks in this pool.

Private workers

workers: WorkerWrapper[]

A list of all the workers in this pool.

Accessors

running

  • get running(): boolean
  • Returns whether or not there are tasks running in this pool.

    Returns boolean

workerCount

  • get workerCount(): number
  • Returns the total number of workers held by this pool.

    Returns number

Static sharedInstance

Methods

addWorker

  • Adds the specified worker to this pool and, optionally, runs an init task.

    Parameters

    • worker: AnyWorker

      The worker to add

    • Optional initTask: WorkerTask

      Init task to be executed immediately on the worker

    Returns Promise<any>

addWorkers

  • Adds an array of workers to this pool and, optionally, runs an init task on them.

    Parameters

    • workers: AnyWorker[]

      A list of workers to add

    • Optional initTask: WorkerTask

      Init task to be executed immediately on each of the workers added

    Returns Promise<any[]>

cancelAllPending

  • cancelAllPending(): void
  • Cancels all pending tasks in this pool.

    WARNING: Tasks that have already been sent to workers are NOT cancelled.

    Returns void

cancelPending

  • Cancels pending tasks of the specified type.

    WARNING: Tasks that have already been sent to workers are NOT cancelled.

    Parameters

    • task: string | WorkerTask

      The task id (name) or a task instance to be cancelled.

    Returns void

Private executeTask

  • Executes the specified task in the worker provided and listens for a response. If a success message is received from the worker, the task promise is resolved with the results, the promise is rejected with an explanation otherwise.

    Parameters

    Returns void

Private executeTaskFromQueue

  • Checks if there are pending tasks in the queue, if so, the next task is executed in the worker provided. If no tasks are pending, the worker is flagged as idle.

    Parameters

    • worker: WorkerWrapper

      Worker that will execute a task or be marked as idle.

    Returns void

killWorkers

  • killWorkers(): void
  • Immediately kills all the workers in this pool.

    WARNING: Pending and running tasks will not resolve their promises when this method is called.

    Returns void

makeTask

  • makeTask(id: string, args?: any[], transferable?: ArrayBuffer[]): WorkerTask
  • Makes a WorkerTask to be executed by any of the workers in this pool.

    Parameters

    • id: string

      The task id (name) to execute

    • Default value args: any[] = []

      Any arguments that need to be passed to the task as described in the TaskExecutor

    • Default value transferable: ArrayBuffer[] = []

      List of objects that will be transferred to the worker. NOTE: Transferred objects are invalidated after the task is sent and cannot be used anymore.

    Returns WorkerTask

removeWorker

scheduleTask

  • Schedules a task to be executed in a worker as soon as one is available. There are no guarantees as of which worker will perform the task.

    Parameters

    Returns Promise<any>

scheduleTasks

  • scheduleTasks(tasks: WorkerTask[]): Promise<any[]>
  • Schedules an array of tasks to be executed in workers as soon as they become available. There are no guarantees as of which workers will perform the tasks.

    Parameters

    Returns Promise<any[]>

Private wrapWorker

Private wrapWorkers

Generated using TypeDoc