Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ConcurrentWorkerSet

A set of concurrent Web Workers. Acts as a Communication Peer for WorkerService instances running in Web Workers.

Starts and manages a certain number of web workers and provides a means to communicate with them using various communication schemes, such as:

  • addEventListener : receive a unidirectional messages
  • broadcastMessage : send unidirectional broadcast message
  • invokeRequest : send a request that waits for a response, with load balancing
  • [[postMessage]] : send a unidirectional message, with load balancing

The request queue holds all requests before they are stuffed into the event queue, allows for easy (and early) cancelling of requests. The workers now only get a single new RequestMessage when they return their previous result, or if they are idle. When they are idle, they are stored in m_availableWorkers.

Hierarchy

  • ConcurrentWorkerSet

Index

Constructors

constructor

Accessors

numIdleWorkers

  • get numIdleWorkers(): number

numWorkers

  • get numWorkers(): number

requestQueueSize

  • get requestQueueSize(): number

terminated

  • get terminated(): boolean

workerCount

  • get workerCount(): number | undefined

Methods

addEventListener

  • addEventListener(serviceId: string, callback: (message: any) => void): void
  • Registers an event listener for events that originated in a web worker, for a given serviceId. You can only set one event listener per serviceId.

    Parameters

    • serviceId: string

      The service to listen to.

    • callback: (message: any) => void

      The callback to invoke for matching events.

        • (message: any): void
        • Parameters

          • message: any

          Returns void

    Returns void

addReference

  • addReference(): void

broadcastMessage

  • broadcastMessage(message: any, buffers?: ArrayBuffer[] | undefined): void

broadcastRequest

  • Invokes a request that expects responses from all workers.

    Send RequestMessage to all workers and resolves when all workers have sent a matching ResponseMessage. Use this function to wait on request that need to happen on all workers before proceeding (like synchronous worker service creation).

    Type parameters

    • Res

    Parameters

    • serviceId: string

      The name of service, as registered with the [[WorkerClient]] instance.

    • request: WorkerServiceManagerRequest | ServiceRequest

      The request to process.

    • Optional transferList: ArrayBuffer[]

      An optional array of ArrayBuffers to transfer to the worker context.

    Returns Promise<Res[]>

    Array of Promises that resolves with a response from each worker (unspecified order).

connect

  • connect(serviceId: string): Promise<void>
  • Waits for service to be initialized in all workers.

    Each service that starts in a worker sends an isInitializedMessage to confirm that it has started successfully. This method resolves when all workers in a set have service initialized.

    Promise is rejected if any of worker fails to start.

    Parameters

    • serviceId: string

      The service identifier.

    Returns Promise<void>

destroy

  • destroy(): void

Protected eventHandler

  • eventHandler(event: any): void

invokeRequest

  • Invokes a request that expects a response from a random worker.

    Sends RequestMessage and resolves when a matching ResponseMessage is received from workers. Use this function when interfacing with "RPC-like" calls to services.

    Type parameters

    • Res

    Parameters

    • serviceId: string

      The name of service, as registered with the [[WorkerClient]] instance.

    • request: ServiceRequest

      The request to process.

    • Optional transferList: ArrayBuffer[]

      An optional array of ArrayBuffers to transfer to the worker context.

    • Optional requestController: RequestController

      An optional RequestController to store state of cancelling.

    Returns Promise<Res>

    A Promise that resolves with a response from the service.

removeEventListener

  • removeEventListener(serviceId: string): void

removeReference

  • removeReference(): void

start

stop

  • stop(): Promise<void>

Generated using TypeDoc