Worker

Moduleejs.sys
Definition class Worker
InheritanceWorker inherit Object
Specifiedwebworker-w3c
StabilityPrototype.

Worker Class.

Worker threads are medium-weight thread-based virtual machine instances. They run separate interpreters with tightly controlled data interchange. This class is currently being specified by the WebWorker task group (See http://www.whatwg.org/specs/web-workers/current-work/#introduction). This class is prototype and highly likely to change.


Properties

QualifiersPropertyTypeDescription
public nameStringWorker name. This name is initialized but workers can update as required.
public oncloseFunctionCallback function invoked when the worker exits. The "this" object is set to the worker object.
public onerrorFunctionCallback function to receive incoming error events. This is invoked when the Worker thows an exception. The "this" object is set to the worker object.
public onmessageFunctionCallback function to receive incoming messages. This is invoked when postMessage is called in another Worker. The "this" object is set to the worker object. This is invoked as: function (event) { }.

Worker Methods

QualifiersMethod
public eval(script: String, timeout: Number = -1): String
 Load the script.
public static exit(): Void
 Exit the worker.
public static join(workers: Object = null , timeout: Number = -1): Boolean
 Wait for Workers to exit.
public load(script: Path, timeout: Number = 0): Void
 Load and run a script in a dedicated worker thread.
public static lookup(name: String): Worker
 Lookup a Worker by name.
public postMessage(data: Object, ports: Array = null): Void
 Post a message to the Worker's parent.
public preload(path: Path): String
 Preload the specified script or module file to initialize the worker.
public terminate(): Void
 Terminate the worker.
public waitForMessage(timeout: Number = -1): Boolean
 Wait for receipt of a message.
Worker(script: Path = null , options: Object = null)
 Create a new Worker instance.

Method Detail

public eval(script: String, timeout: Number = -1): String

Load the script.

Description
The literal script is compiled as a JavaScript program and loaded and run. This is similar to the global eval() command but the script is run in its own interpreter and does not share any data the the invoking interpreter. The result is serialized in the worker and then deserialized (using JSON) in the current interpreter. The call returns undefined if the timeout expires.
Parameters
script: String Literal JavaScript program string.
timeout: Number If the timeout is non-zero, this call blocks and will return the value of the last expression in the script. Otherwise, this call will not block and join() can be used to wait for completion. Set the timeout to -1 to block until the script completes. The default is -1. [default: -1]
Returns
The value of the last expression evaluated in the script. Returns undefined if the timeout expires before the script completes.
Throws
Specified
ejscript-1.1

static public exit(): Void

Exit the worker.

Specified
ejscript-1.1

static public join(workers: Object = null , timeout: Number = -1): Boolean

Wait for Workers to exit.

Parameters
workers: Object Set of Workers to wait for. Can be a single Worker object or an array of Workers. If null or if the array is empty, then all workers are waited for. [default: null ]
timeout: Number Timeout to wait in milliseconds. The value -1 disables the timeout. [default: -1]
Specified
ejscript-1.1

public load(script: Path, timeout: Number = 0): Void

Load and run a script in a dedicated worker thread.

Parameters
script: Path Filename of a script or module to load and run.
timeout: Number If the timeout is non-zero, this call blocks and will return the value of the last expression in the script. Otherwise, this call will not block and join() can be used to wait for completion. Set the timeout to -1 to block until the script completes. The default is to not block. [default: 0]
Specified
ejscript-1.1

static public lookup(name: String): Worker

Lookup a Worker by name.

Parameters
name: String Lookup a Worker.
Specified
ejscript-1.1

public postMessage(data: Object, ports: Array = null): Void

Post a message to the Worker's parent.

Parameters
data: Object Data to pass to the worker's onmessage callback.
ports: Array Not implemented.

public preload(path: Path): String

Preload the specified script or module file to initialize the worker.

Description
This will run a script using the current thread and will block. To run a worker using its own thread, use load() or Worker(script). This call will load the script/module and initialize and run global code. The call will block until all global code has completed and the script/module is initialized.
Parameters
path: Path Filename path for the module or script to load. This should include the file extension.
Returns
The value of the last expression in the script or module.
Throws
Specified
ejscript-1.1

public terminate(): Void

Terminate the worker.


public waitForMessage(timeout: Number = -1): Boolean

Wait for receipt of a message.

Parameters
timeout: Number Timeout to wait in milliseconds. [default: -1]
Stability
Prototype.

public Worker(script: Path = null , options: Object = null)

Create a new Worker instance.

Description
This call returns an outside worker object for using in the calling interpreter. Inside the worker interpreter, a corresponding "insdie" worker object is created that is paired to the outside worker.
Parameters
script: Path Optional path to a script or module to execute. If supplied, then a new Worker instance will invoke load() to execute the script. [default: null ]
options: Object Options hash. [default: null]
Options
searchSearch path.
nameName of the Worker instance.
Specified
webworker