Select to view content in your preferred language

Accessing the JS API via a web worker

8386
28
05-15-2017 02:51 PM
RobertHewlett
Regular Contributor

I would like to offload some of the heavy lifting in a web app to a web worker.

It is a 2-3 second jank I would like gone.

Right now the web worker would still need access to parts of the API e.g. Extent and Polygon.

I have tried loading the init.js script via importScripts() but I still get errors.

Has anyone had any success with the ArcGIS JS-API inside a web worker ... not outside but inside. 

0 Kudos
28 Replies
RobertHewlett
Regular Contributor

Hi Rene,

I am newbie on this part of the API so my conceptual thinking might be off but ....

Is proxy.connection.invoke basically the worker invoking one of its own methods/functions as this is usually done from the client side.

OR

Is the there a list of client side objects you can send into the worker and from within the worker call these methods/functions back in the client.

Rob

0 Kudos
ReneRubalcava
Esri Frequent Contributor

It's not invoking a method on itself, the proxy.connection invoking a method on the object you are passing to workers.open(), that exist on the client. It can be anything you want, it just needs to have a method that returns a promise, because it's also a way for you to request information from the main thread from within the worker.

0 Kudos
RobertHewlett
Regular Contributor

"You can then invoke any functions from the worker on this argument.

What if the function updated the client UI? BOOM?

0 Kudos
RobertHewlett
Regular Contributor

Just a heads up for others. Once I started using the proxy.connection as shown in the gist-sample the variable names on the client-side and the web-worker side had to match.

This might be a chrome-thing, transferable object-thing or a AJS-connection framework-thing. Not sure as I am a newbie to this part of the API.

If I do this on the non-web-worker side

 return connection.invoke("someFX", { argsAsJSON });

Then inside the web-worker I had to make the variable names match:

       Request.prototype.someFX = function someFX({argsAsJSON}, {proxy}) {

If I used another variable name then that variable would be undefined:

       Request.prototype.someFX = function someFX({data}, {proxy}) {

The line above (in the chrome debugger) would report data as undefined.

0 Kudos
RobertHewlett
Regular Contributor

Can a FeatureSet be used inside a worker?

I am currently getting this error.

ErrorEvent {isTrusted: true, message: "Uncaught TypeError: Cannot read property 'compatMode' of null", filename: "http://js.arcgis.com/4.3/dojo/dojo.js", lineno: 168, colno: 316}
bubbles
:
false
cancelBubble
:
false
cancelable
:
true
colno
:
316
composed
:
false
currentTarget
:
Worker
defaultPrevented
:
true
error
:
null
eventPhase
:
0
filename
:
"http://js.arcgis.com/4.3/dojo/dojo.js"
isTrusted
:
true
lineno
:
168
message
:
"Uncaught TypeError: Cannot read property 'compatMode' of null"
path
:
Array(0)
returnValue
:
false
srcElement
:
Worker
target
:
Worker
timeStamp
:
5463.285000000001
type
:
"error"
__proto__
:
ErrorEvent
0 Kudos
RobertHewlett
Regular Contributor

Below is a copy of my define()

define([
    "esri/config",
    "esri/core/promiseUtils",
    "esri/geometry/geometryEngine",
    "esri/geometry/Extent",
    "esri/geometry/Polygon",
    "esri/tasks/support/FeatureSet"
], work);
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

So far the worker works until I add:

    "esri/tasks/support/FeatureSet"
‍‍‍
0 Kudos
RobertHewlett
Regular Contributor

Enjoy!

RobertHewlett
Regular Contributor

Updated video.

MichalGasparovic
Occasional Contributor

Apologies for digging out this old thread. I'm running into the issue with the huge amount of requests and it's been recommended to do my own build. Let's assume I have it, how do I provided it to the worker?

https://community.esri.com/message/790617-re-web-worker-and-js-api-modules 

Thanks

0 Kudos