Skip to main content

Class: WorkerPool

A pool of web workers that can be used to execute jobs. The pool will create workers up to the maximum number of workers specified in the options. When a job is queued, the pool will find the first available worker and execute the job. If no workers are available, the job will be queued until a worker becomes available.

Constructors​

constructor​

• new WorkerPool(Proto, options?): WorkerPool

Create a new worker pool.

Parameters​

NameTypeDefault valueDescription
Proto(options?: WorkerOptions) => WorkerundefinedThe worker class to create.
optionsWorkerPoolOptionsdefaultOptionsThe options to create the worker pool.

Returns​

WorkerPool

Properties​

Proto​

• Proto: (options?: WorkerOptions) => Worker

The worker class to create.

Type declaration​

• new Proto(options?): Worker

Parameters​
NameType
options?WorkerOptions
Returns​

Worker


WORKING_COUNT​

▪ Static WORKING_COUNT: number = 0

A static count of working web workers across all worker pools.


options​

• options: WorkerPoolOptions = defaultOptions

The options to create the worker pool.


queue​

• queue: WorkerPoolJob[] = []

The queue of jobs that are waiting to be executed.

Accessors​

availableCount​

• get availableCount(): number

The number of workers that are available to take jobs.

Returns​

number


isBusy​

• get isBusy(): boolean

Whether or not are there no available workers.

Returns​

boolean


queuedBytes​

• get queuedBytes(): number

Total bytes of transferable payloads sitting in the queue, waiting for a free worker. A sustained climb here means jobs are being enqueued (with their serialized copies) faster than workers drain them.

Returns​

number


workingCount​

• get workingCount(): number

The number of workers that are simultaneously working.

Returns​

number

Methods​

addJob​

▸ addJob(job): void

Append a new job to be executed by a worker.

Parameters​

NameTypeDescription
jobWorkerPoolJobThe job to queue.

Returns​

void


drainQueue​

▸ drainQueue(): number

Drop every job still waiting for a worker, resolving each null. Used to release the serialized payloads parked in the queue when the renderer is under memory pressure; in-flight jobs are left alone.

Returns​

number

The number of jobs dropped.


postMessage​

▸ postMessage(message, buffers?): void

Parameters​

NameType
messageany
buffers?Transferable[]

Returns​

void


terminate​

▸ terminate(): void

Returns​

void