ESRI JS API 4.11: webworkers: document is not defined

1377
7
05-13-2019 05:26 PM
by Anonymous User
Not applicable

I'd like to report a broken functionality, I cannot create the ElevationLayer in webworker anymore, ending with the error of: 

 {name: "ReferenceError", message: "document is not defined", details: undefined}

Webworker file (excerpt)

Calculator.prototype.startViewshed = function(data, connection) {
  if (!elevationLayer || elevationLayer.url !== data.url) {
    // --> Error happens here
    elevationLayer = new ElevationLayer({
      url: data.url
    });
  }

..
..
..

I need to create the ElevationLayer so I can create an elevationSampler and run the analysis on it. Apparently, the 4.11 version does require a 'document' to be available, whereas it wasn't a requirement for 4.10 version of ElevationLayer class.

Please advise the relevant workaround for this issue. Thank you.

0 Kudos
7 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

I am not able to reproduce the issue using our workers framework which uses webworkers. I am able to create a new instance of ElevationLayer without errors. The following is super simplified version of the test app.

Worker script
define([
    "esri/layers/ElevationLayer",
    "esri/core/promiseUtils"
  ],
  function (ElevationLayer, promiseUtils) {
    return {
      createPoints: function (data) {
        var url = data.url;

        var elevationLayer = new ElevationLayer({
          url: data.url
        });

        return elevationLayer.load().then(function(){
          console.log("layer", elevationLayer);

          return promiseUtils.resolve({
            data: {
              sr: elevationLayer.spatialReference.toJSON()
            }
          });
        })
        .catch(function(error){
          return promiseUtils.resolve({
            data: {
              error: error
            }
          });
        });
      }
    }
  });

HTML APP  
<script src="https://js.arcgis.com/4.11/"></script>

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/ElevationLayer",
      "esri/core/workers/workers"
    ], function (
      Map, MapView, ElevationLayer, workers
    ) {

      var local = window.location.href.substring(0, window.location.href.lastIndexOf('/'));
      var workerUrl = local + "/createPointsWorker.js";

      var map = new Map({
        basemap: "oceans",
      });

      view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [-100, 35]
      });

      var url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/ScientificData/SeaTemperature/ImageServer"

      view.when(function () {
        var params = {
          url: url
        };
        executeWorker(params);
      });

      function executeWorker(params) {
        console.log(workerUrl);
        var connection;

        return workers.open(workerUrl)
          .then(function (conn) {
            console.log(params);
            connection = conn;
            return connection.invoke("createPoints", params);
          })
          .then(function (results) {
            connection.close();
            console.log("results from the workers", results);
          });
      }
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
by Anonymous User
Not applicable

Hi Undral,

do you have a sandbox somewhere? For me it's simply not working with 4.11, 4.10 works fine. What browser you are using?

M

0 Kudos
by Anonymous User
Not applicable

I've tried your sample, it didn't work. I'm 100% sure it's 4.11 that I'm pulling.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

A developer on the team was able to reproduce the issue you are describing. We created an issue for this and thank you for bringing this issue to our attention.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

This bug has been addressed at JS API version 4.12. This version will be released later this month.

by Anonymous User
Not applicable

Has this been really resolved ? I can't see it in the release ntoes
Release notes for 4.12 | ArcGIS API for JavaScript 4.12 

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

The issue is fixed at version 4.12. I apologize that this fix was not listed in the release notes.

0 Kudos