Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Worker

deprecated

Hierarchy

  • Worker

Index

Constructors

constructor

  • new Worker(filePath: string): Worker
  • Creates a new worker.

    remarks

    This will throw an error if the resource has reached its maximum amount of workers. See maxWorkers.

    Parameters

    • filePath: string

      The path to the worker script file, can be relative or absolute path.

    Returns Worker

Properties

Readonly filePath

filePath: string

The file path that was used to create this worker.

Readonly isPaused

isPaused: boolean

Whether the worker is currently paused. See pause and resume.

Readonly valid

valid: boolean

Whether this worker is still valid. See destroy.

Static Readonly maxWorkers

maxWorkers: number

Maximum available workers per resource.

Methods

destroy

  • destroy(): void
  • Destroys the worker and stops it.

    remarks

    This permanently sets valid to false.

    Returns void

emit

  • emit(eventName: string, ...args: any[]): void
  • Emits the specified event to the worker.

    remarks

    This can be called before the worker is started.

    Parameters

    • eventName: string
    • Rest ...args: any[]

    Returns void

off

  • off(eventName: string, listener: (...args: any[]) => void): void
  • Unsubscribes from Worker event handler with specified listener.

    remarks

    Listener should be of the same reference as when event was subscribed.

    Parameters

    • eventName: string

      Name of the event.

    • listener: (...args: any[]) => void

      Listener that should be removed.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns void

on

  • on(eventName: string, callback: (...args: any[]) => void): void
  • Listens to an event emitted by the worker.

    remarks

    The event handlers should be registered before starting the worker.

    Parameters

    • eventName: string

      The event to listen for.

    • callback: (...args: any[]) => void

      The callback to call when the event is received.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns void

once

  • once(eventName: string, callback: (...args: any[]) => void): void
  • Listens to an event emitted by the worker once.

    remarks

    See on.

    Parameters

    • eventName: string

      The event to listen for.

    • callback: (...args: any[]) => void

      The callback to call when the event is received.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns void

pause

  • pause(): void
  • Pauses execution of the worker.

    remarks

    This completely stops the JavaScript event loop of the worker but events can still be received (and will be handled once the worker is resumed).

    Returns void

resume

  • resume(): void
  • Resumes execution of the worker.

    remarks

    See pause.

    Returns void

start

  • start(): void
  • Starts the worker script.

    Returns void

Static addSharedArrayBuffer

  • addSharedArrayBuffer(buffer: SharedArrayBuffer): number
  • Adds a shared array buffer to the workers array buffer pool.

    remarks

    Make sure to remove the array buffer once you are done with it by calling removeSharedArrayBuffer, otherwise you are creating a memory leak.

    Parameters

    • buffer: SharedArrayBuffer

      The shared array buffer to make available in workers.

    Returns number

    The id of the shared array buffer that can be used in workers to retrieve it.

Static removeSharedArrayBuffer

  • removeSharedArrayBuffer(id: number): void
  • Removes the shared array buffer from the workers array buffer pool.

    Parameters

    • id: number

      The id of the shared array buffer to remove.

    Returns void