wilton.js  v202103141
 All Namespaces Functions
Functions
thread Namespace Reference

wilton/thread
Run background threads. More...

Functions

Object run (Object options, Function|Undefined callback)
 Start specified function in the background thread.
Undefined sleepMillis (Number millis, Function|Undefined callback)
 Stops the execution of the current thread for the specified time period.

Detailed Description

This module allows to run specified function in the background OS-thread.

Different threads have separate JavaScript runtime contexts and do not share JavaScript objects in memory. wilton/Channel or wilton/shared modules may be used for inter-thread communication.

Started background threads are daemon (detached) and should be stopped (e.g. using application level synchronization) before the application shutdown.

Usage example:

// runs specified function in background thread
thread.run({
callbackScript: {
module: "path/to/task/module",
func: "taskFunction",
args: ["foo", 42]
}
});
// make current thread to sleep for some time
thread.sleepMillis(1000);

Function Documentation

Object thread::run ( Object  options,
Function|Undefined  callback 
)

Starts the new OS-thread and runs specified function (found inside the specified module) in this thread.

This function returns immediately.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Object Channel object where thread will send a message on its exit

Options

  • callbackScript Object module path and function name to run from background thread
    • module String full name of JavaScript module to load
    • func String|Undefined name of the function contained in the specified module, if function is not specified, only module will be loaded (its top-level code will be executed)
    • args Array|Undefined optional list of arguments, that will be passed to specified function
  • shutdownChannelName String|Undefined the name of the Channel, that will be created for this thread; thread will send the message to this channel on exit; default value: callbackScript.module
  • capabilities Array|Undefined list of the wiltoncall calls names allowed to be used from the spawned thread; if this parameter is not specified, then capabilities checks are not performed for the spawned thread
Undefined thread::sleepMillis ( Number  millis,
Function|Undefined  callback 
)

Maked current thread to sleep for specified amount of milliseconds.

Parameters
millisNumber amount of milliseconds to sleep
callbackFunction|Undefined callback to receive result or error
Returns
Undefined