Options
All
  • Public
  • Public/Protected
  • All
Menu

An asynchronous web Worker which can load user-defined functions and await execution using Promises.

param name

The worker name to be initialized

param [options={}]

Worker initialization options

param [options.debug=false]

Should the worker run in debug mode?

param [options.loadPrimitives=false]

Should the worker automatically load the primitives library?

Hierarchy

  • Worker
    • AsyncWorker

Index

Constructors

  • new AsyncWorker(name: any, __namedParameters?: { debug: boolean; loadPrimitives: boolean }): AsyncWorker
  • Parameters

    • name: any
    • __namedParameters: { debug: boolean; loadPrimitives: boolean } = {}
      • debug: boolean
      • loadPrimitives: boolean

    Returns AsyncWorker

Properties

name: any
ready: Promise<any>

A Promise which resolves once the Worker is ready to accept tasks

tasks: Map<number, Object> = ...

A queue of active tasks that this Worker is executing.

_taskIndex: number = 0

An auto-incrementing task index.

WORKER_HARNESS_JS: string = "scripts/worker.js"

A path reference to the JavaScript file which provides companion worker-side functionality.

Methods

  • loadFunction(functionName: string, functionRef: Function): Promise<unknown>
  • Load a function onto a given Worker. The function must be a pure function with no external dependencies or requirements on global scope.

    Parameters

    • functionName: string

      The name of the function to load

    • functionRef: Function

      A reference to the function that should be loaded

    Returns Promise<unknown>

    A Promise which resolves once the Worker has loaded the function.

  • executeFunction(functionName: string, ...params: any[]): Promise<unknown>
  • Execute a task on a specific Worker.

    Parameters

    • functionName: string

      The named function to execute on the worker. This function must first have been loaded.

    • Rest ...params: any[]

      An array of parameters with which to call the requested function

    Returns Promise<unknown>

    A Promise which resolves with the returned result of the function once complete.

  • _dispatchTask(taskData?: Object): Promise<any>
  • Dispatch a task to a named Worker, awaiting confirmation of the result.

    Parameters

    • taskData: Object = {}

      Data to dispatch to the Worker as part of the task.

    Returns Promise<any>

    A Promise which wraps the task transaction.

  • _onMessage(event: MessageEvent<any>): any
  • Handle messages emitted by the Worker thread.

    Parameters

    • event: MessageEvent<any>

      The dispatched message event

    Returns any

  • _onError(error: ErrorEvent): void
  • Handle errors emitted by the Worker thread.

    Parameters

    • error: ErrorEvent

      The dispatched error event

    Returns void