Wrapper for a web worker meant to convert a pixel buffer to the specified image format and quality and return a base64 image

Param: name

The worker name to be initialized

Param: config

Worker initialization options

Param: config.debug

Should the worker run in debug mode?

Hierarchy (view full)

Properties

#controlHash: boolean

Boolean to know if the texture compressor should control the hash.

#textureHash: string = ""

Previous texture hash.

#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

  • Process the non-blocking image compression to a base64 string.

    Parameters

    • buffer: Uint8ClampedArray

      Buffer used to create the image data.

    • width: number

      Buffered image width.

    • height: number

      Buffered image height.

    • options: {
          type: string;
          quality: number;
          debug: boolean;
      } = {}
      • type: string

        The required image type.

      • quality: number

        The required image quality.

      • debug: boolean

        The debug option.

    Returns Promise<any>

  • Expand a buffer in RED format to a buffer in RGBA format.

    Parameters

    • buffer: Uint8ClampedArray

      Buffer used to create the image data.

    • width: number

      Buffered image width.

    • height: number

      Buffered image height.

    • options: {
          debug: boolean;
      } = {}
      • debug: boolean

        The debug option.

    Returns Promise<any>

  • Reduce a buffer in RGBA format to a buffer in RED format.

    Parameters

    • buffer: Uint8ClampedArray

      Buffer used to create the image data.

    • width: number

      Buffered image width.

    • height: number

      Buffered image height.

    • options: {
          debug: boolean;
      } = {}
      • debug: boolean

        The debug option.

    Returns Promise<any>

  • 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