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?

param [options.scripts]

Should the worker operates in script modes? Optional scripts.

Hierarchy

Index

Constructors

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

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

    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, args?: any[], transfer?: 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.

    • args: any[] = []

      An array of parameters with which to call the requested function

    • transfer: any[] = []

      An array of transferable objects which are transferred to the worker thread. See https://developer.mozilla.org/en-US/docs/Glossary/Transferable_objects

    Returns Promise<unknown>

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

  • _dispatchTask(taskData?: Object, transfer?: any[]): 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.

    • transfer: any[] = []

      An array of transferable objects which are transferred to the worker thread.

    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