//*****************************************************************
// This snippet shows how to invoke a method in the workers thread
// from the main thread
//*****************************************************************
// Pass the fully qualified URL to a script to execute in the worker thread.
var local = window.location.href.substring(0, window.location.href.lastIndexOf('/'));
var workerUrl = local + "/Calculator.js";
workers.open(this, workerUrl)
.then(function(connection) {
return connection.invoke("getMaxNumber", {
numbers: [0, 1, 2, 3, 4]
});
})
.then(function(result) {
console.log(result);
});
//******************************************************************
// In this example, `Calculator` is the name of the script that is
// loaded with the workers framework.
//******************************************************************
//*********************************************************
// module: workerScripts/Calculator.js
//*********************************************************
define([
"esri/core/promiseUtils"
],
function (promiseUtils) {
return {
// this function can be invoked from the main thread
getMaxNumber: function (data) {
return promiseUtils.resolve({
data: Math.max.apply(null, data.numbers)
});
}
}
});
Method Overview
Name | Return Type | Summary | |
---|---|---|---|
Promise[] | A convenient method that broadcasts a message to all workers. more details | more details | |
Closes the existing connection instance to workers. more details | more details | ||
Promise | Invokes a method on the script loaded with the workers framework. more details | more details |
Method Details
broadcast(methodName, data, buffers){Promise[]}
A convenient method that broadcasts a message to all workers. Use this method if it is mandatory that all workers need to have a piece of information which is critical for them to process additional requests.
Parameters:methodName StringThe name of the method to invoke on all workers.
optionaldata ObjectThe object which contains all required parameters to be passed to the method. The data passed between the main thread and the workers framework are copied, not shared. Objects are serialized and de-serialized on both ends.
optionalbuffers ArrayBuffer[]An array of transferable buffers. Each buffer in the array should have a corresponding entry in the data object. Transferable objects are transferred from one context to another with a zero-copy operation, which results in a big performance improvement when sending large data sets.
Returns:Type Description Promise[] Resolves to an array objects containing results returned the method invoked on all workers. close()
Closes the existing connection instance to workers. Notify all workers to destroy the connection instance and unload the connection module.
invoke(methodName, data, buffers){Promise}
Invokes a method on the script loaded with the workers framework.
Parameters:methodName StringThe name of the method to be invoked in the script.
optionaldata ObjectThe object which contains all required parameters to be passed to the method. The data passed between the main thread and the workers framework are copied, not shared. Objects are serialized and de-serialized on both ends.
optionalbuffers ArrayBuffer[]An array of transferable buffers. Each buffer in the array should have a corresponding entry in the data object. Transferable objects are transferred from one context to another with a zero-copy operation, which results in a big performance improvement when sending large data sets.
Returns:Type Description Promise Resolves to an object containing results returned from the invoked method. Example:// Invoke a method called 'createImage' in the // script loaded with the workers framework // and handle transferables: connection.invoke("createImage", { canvasWidth: width, canvasHeight: height, extent: { xmin: imageExtent.xmin, ymin: imageExtent.ymin, xmax: imageExtent.xmax, ymax: imageExtent.ymax } ) .then(function(params) { var canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; var ctx = canvas.getContext('2d'); // create the view for the data, then create a new image and add it to the context var imageData = new ImageData(new Uint8ClampedArray(params.data.mandelbrotData), width, height); ctx.putImageData(imageData, 0, 0); return { options: options, img: canvas }; });