Class: WorkerHost

WorkerHost(scriptURL, onEventReady)

Manages a WebWorker instance and responds via events and/or promises.

Constructor

new WorkerHost(scriptURL, onEventReady)

Creates a new WorkerHost instance and automatically instantiates an associated Web Worker.
Parameters:
Name Type Description
scriptURL String The external Web Worker script URL / path to create this instance with.
onEventReady Boolean If true, the WorkerHost will dispatch an event (using the parent EventDispatcher), whenever the host ready state changes. It is advisable to enable this setting as the worker script may require some time to initialize and may be unable to respond to requests.
Source:

Extends

Members

(static) instances

Properties:
Name Type Description
instances Array Returns all WorkerHost instances in the current execution context.
Source:

instanceNum

Properties:
Name Type Description
instanceNum Array The instance number of the current WorkerHost instance. This value matches the index of the instance within the WorkerHost.instances property.
Source:

(readonly) ready

Properties:
Name Type Description
ready Boolean True if the host instance is ready to accept a new request for the associated Web Worker.
Source:

(readonly) scriptURL

Properties:
Name Type Description
scriptURL String The Web Worker script URL associated with this host instance.
Source:

Methods

addEventListener(type, listener, contextopt)

Registers a new event listener with the extending class instance.
Parameters:
Name Type Attributes Default Description
type String The event type to register.
listener function The listening function to invoke on the event.
context Object <optional>
null Unlike the traditional addEventListener parameter, this is the context or scope in which to invoke the listening function (since we can't use capture phases). If null, the listener is invoked in the context of the EventDispatcher or extending instance.
Inherited From:
Source:

dispatchReadyEvent(isReady)

Dispatches a ready / not ready event for the host.
Parameters:
Name Type Description
isReady Boolean Defines the ready state of the host to dispatch.
Source:
Fires:
  • event:ready
  • event:busy

getListeners(type) → {Array}

Returns all registered listeners for a specific event type.
Parameters:
Name Type Description
type String The event type to return registered listeners for.
Inherited From:
Source:
Returns:
A list of registered event listener objects for the specific event. Each object contains a listener function reference and an execution context reference.
Type
Array

handleWorkerMessage(event)

Handles a response message event for the associated Web Worker.
Parameters:
Name Type Description
event Object A standard Worker "message" event.
Source:
Listens to Events:
  • Worker#event:message

(async) invoke(method, params, requestIDopt)

Invokes an asynchronous worker method using a free workerHost instance.
Parameters:
Name Type Attributes Description
method String The method to invoke in the hosted Worker instance.
params Object The parameters to invoke the method with.
requestID * <optional>
A request ID that can be used to track the request over its lifetime (useful when re-assembling multiple discrete invokations that could otherwise result in a race condition).
Source:
Examples
//simple example invoking an "add" function with parameters "num1" and "num2"
let host = new WorkerHost("./workers/MyWorker.js");
host.invoke("add", {num1:1, num2:3}, 1).then(event => {
 //event.data.requestID will match the invoking request ID (1 in this case)
 console.log ("1 + 3 = " + event.data.result);
});
//using an async function
let host = new WorkerHost("./workers/MyWorker.js");
async function doAdd(num1, num2) {
 //request ID is not included in this case
 return (host.invoke("add", {"num1":num1, "num2":num2}));
}
doAdd(1,3).then(event => {
 console.log ("1 + 3 = "+event.data.result);
})

onWorkerReady(event)

Event handler that responds to the associated Web Worker's "ready" event.
Parameters:
Name Type Description
event Object A standard Worker "message" event object.
Source:
See:
  • dispatchReadyEvent
Listens to Events:
  • Worker#event:message

removeEventListener(type, listener, contextopt)

Removes an event listener from the extending instance.
Parameters:
Name Type Attributes Default Description
type String The event type to remove the function from.
listener function The listening function to remove.
context Object <optional>
null The context or scope in which the listener exists.
Inherited From:
Source: