Home > @gglib/utils > WebWorker

WebWorker class

Provides a mechanism for registering functions as background tasks which can be executed on a worker thread

Signature:

export declare class WebWorker 

Remarks

The class is a wrapper around the Web Worker Api.

To enable the functionality it is required that the workerUrl is set and points to a worker script which should run in the worker thread.

Functions that should run on the worker thread may be registered at any time, before or after the functionality is enabled.

It is important that this class and the registry of background tasks are all bundled in the application bundle as well as in the worker bundle

Example

const myTask = WebWorker.task("myBackgroundTask", () => {
  // do heavy things
})

WebWorker.enable('http://example.com/gglib.umd.js')

myTask()                           // will run on a worker thread
WebWorker.exec("myBackgroundTask") // same task as above

WebWorker.disable()                // disables webworker functionality
myTask()                           // will run on main thread
WebWorker.exec("myBackgroundTask") // same task as above

Constructors

Constructor Modifiers Description
(constructor)(script) Constructs a new instance of the WebWorker class

Properties

Property Modifiers Type Description
isSupported static boolean Determines whether web porker api is supported
isWindowContext static boolean Determines whether this context is inside a browser window
isWorkerContext static boolean Determines whether this context is inside a web worker

Methods

Method Modifiers Description
disable() static Disables a webworker script
enable(workerUrl, workerCount) static Enables a webworker and limits to the number of threads
exec(method, params) static Executes a task that has been previously registered with the task method.
task(name, action, thread) static Registers a named function that can be executed in a webworker thread