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

Should the worker run in debug mode?

Param: options.loadPrimitives

Should the worker automatically load the primitives library?

Param: options.scripts

Should the worker operates in script modes? Optional scripts.

Hierarchy (view full)

Properties

#tasks: Map<number, {
    resolve: ((result) => void);
    reject: ((error) => void);
}> = ...

A queue of active tasks that this Worker is executing.

Type declaration

  • resolve: ((result) => void)
      • (result): void
      • Parameters

        • result: any

        Returns void

  • reject: ((error) => void)
      • (error): void
      • Parameters

        • error: Error

        Returns void

#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.

Accessors

  • get ready(): Promise<any>
  • A Promise which resolves once the Worker is ready to accept tasks

    Returns Promise<any>

Methods

  • 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.

  • 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.

    • Optional args: any[] = []

      An array of parameters with which to call the requested function

    • Optional 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.

  • Dispatch a task to a named Worker, awaiting confirmation of the result.

    Parameters

    • taskData: WorkerTask = {}

      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.

  • Handle messages emitted by the Worker thread.

    Parameters

    • event: MessageEvent<any>

      The dispatched message event

    Returns void

  • Handle errors emitted by the Worker thread.

    Parameters

    • error: ErrorEvent

      The dispatched error event

    Returns void