Connection

Class: esri/core/workers/Connection
Since: ArcGIS API for JavaScript 4.2

The Connection class is used to access code loaded into a separate thread via the workers framework.

The workers.open() method loads the script and returns a Promise to the main thread. This promise resolves to an instance of Connection. The main thread can then asynchronously invoke methods inside that script by calling the invoke() or broadcast() methods.

Methods in the script loaded with the workers framework must return promises. Those promises must be resolved with a data property containing the results.

Examples:
//*****************************************************************
// 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

NameReturn TypeSummary
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 String

The name of the method to invoke on all workers.

data Object
optional

The 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.

optional

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:
TypeDescription
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 String

The name of the method to be invoked in the script.

data Object
optional

The 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.

optional

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:
TypeDescription
PromiseResolves 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
    };
});

API Reference search results

NameTypeModule

There were no match results from your search criteria.