Worker

Moduleejs
Definition class Worker
InheritanceWorker inherit Object
SpecifiedWebWorker
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
nameStringWorker name. This name is initialized but workers can update as required.
oncloseFunctionCallback function invoked when the worker exits. The "this" object is set to the worker object.
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.
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 Class Methods

QualifiersMethod
static exit(): Void
 Exit the worker.
static fork(): Worker
 Create a new worker by cloning the current interpreter.
static join(workers: Object = null, timeout: Number = -1): Boolean
 Wait for Workers to exit.
static lookup(name: String): Worker
 Lookup a Worker by name.

Worker Instance Methods

QualifiersMethod
Worker(script: Path = null, options: Object = null)
 Create a new Worker instance.
clone(deep: Boolean = true): Worker
 Clone the worker and underlying interpreter.
eval(script: String, timeout: Number = -1): Object
 Load the script.
load(script: Path, timeout: Number = 0): Void
 Load and run a script in a dedicated worker thread.
postMessage(data: Object, ports: Array = null): Void
 Post a message to the Worker's parent.
preload(path: Path): Object
 Preload the specified script or module file to initialize the worker.
terminate(): Void
 Terminate the worker.
waitForMessage(timeout: Number = -1): Boolean
 Wait for receipt of a message.

Method Detail

Worker(script: Path = null, options: Object = null)
Description
Create a new Worker instance. This call returns an outside worker object for using in the calling interpreter. Inside the worker interpreter, a corresponding "inside" 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 and ejs

clone(deep: Boolean = true): Worker
Description
Clone the worker and underlying interpreter.
Parameters
deep: Boolean Ignored. [default: true]
Specified
ejscript-2.5

eval(script: String, timeout: Number = -1): Object
Description
Load the script. 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-2.5

static exit(): Void
Description
Exit the worker.
Specified
ejscript-2.5

static fork(): Worker
Description
Create a new worker by cloning the current interpreter.
Specified
ejscript-2.5

static join(workers: Object = null, timeout: Number = -1): Boolean
Description
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
ejs `

load(script: Path, timeout: Number = 0): Void
Description
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-2.5

static lookup(name: String): Worker
Description
Lookup a Worker by name.
Parameters
name: String Lookup a Worker.
Specified
ejscript-2.5

postMessage(data: Object, ports: Array = null): Void
Description
Post a message to the Worker's parent.
Parameters
data: Object Data to pass to the worker's onmessage callback.
ports: Array Not implemented P.

preload(path: Path): Object
Description
Preload the specified script or module file to initialize the worker. This will run a script using the current thread. 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. To run a worker using its own thread, use load() or Worker(script) instead.
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-2.5

terminate(): Void
Description
Terminate the worker.

waitForMessage(timeout: Number = -1): Boolean
Description
Wait for receipt of a message.
Parameters
timeout: Number Timeout to wait in milliseconds. [default: -1]
Returns
True if a message was received.
Stability
Prototype.