- See also
Method Overview
Name | Return Type | Summary | Object |
---|---|---|---|
Promise<Connection> | Opens a connection to workers and loads a script with the workers framework. more details | workers |
Method Details
-
-
Opens a connection to workers and loads a script with the workers framework.
ParametersSpecificationmodulePath StringA fully qualified URL to a script to execute with the workers framework.
options ObjectoptionalWorker options. See properties below for object specifications.
Specificationclient ObjectoptionalThe objects defining the API accessible from the module.
strategy StringoptionalDefault Value: distributedIndicates how to load the module. See the table below for a list of possible values.
Possible Value Description distributed The module is loaded in each available worker. Each call to Connection will be targeting an available worker. Use this strategy if the module doesn't maintain information between invocations (stateless). dedicated The module is loaded in one worker. Each call to Connection will be targeting a same worker. Use this strategy if the module maintains information from previous invocations or communication between main and worker threads needs to be stateful. local The module is loaded in the main thread. Use this strategy when using the worker framework API while disabling the use of workers. Possible Values:"distributed"|"dedicated"|"local"
signal AbortSignaloptionalAbortSignal allows for cancelable asynchronous job. If canceled, the promise will be rejected with an error named
AbortError
. See also AbortController.ReturnsType Description Promise<Connection> Resolves to an instance of Connection. - See also
Examples// Set the path for the worker's AMD loader configuration // to a folder called workersFolder. esriConfig.workers.loaderConfig = { paths: { myWorkers: new URL("./workersFolder", document.baseURI).href } }; // load myWorkers/Calculator.js in the workers framework // and invoke its "getMaxNumber" method workers.open(this, "myWorkers/Calculator") .then((connection) => { return connection.invoke("getMaxNumber", [0, 1, 2, 3, 4]); }) .then((result) => { console.log(result); }); //********************************************************* // module: workerFolder/Calculator.js //********************************************************* define([], () => { return { // this function can be invoked from the main thread getMaxNumber: function (number) { return Math.max.apply(null, numbers); } }; });
// Load workerScripts/TimeOfTheDay.js in the workers framework // We define an API accessible from the module workers.open("workerScripts/TimeOfTheDay", { client: { getCurrentTime: function() { return Date.now(); } } }) .then((connection) => { return connection.invoke("timeOfTheDay"); }) .then((result) => { console.log(result); }); //********************************************************* // module: workerScripts/TimeOfTheDay.js //********************************************************* define([], () => { return { timeOfTheDay: function(noArgs, remoteClient) { // call back the main thread to get the current time over there. return remoteClient.invoke("getCurrentTime") .then((time) => { return "The time is " + time; }); } }; });