Joe
Joe

Reputation: 4254

Return object from a webworker

var worker = new Worker('./src/parser.js');

  worker.onmessage = function(e) {
    console.log(e);
    console.log(e.data.getCollection()); // .. is not a function
  };

The worker:

onmessage = function(e) {
  var myModel = new Model();
  myModel.readFile(e.data.files[0], function (data) {
    console.log(myModel.getCollection()); // Returns array
    postMessage(myModel);
  });
};

The instansiated object is returned but only the values in the constructor. Not any prototype functions.

Why? Isn't it possible to return an instansiated object from a webworker?

Upvotes: 2

Views: 2302

Answers (1)

Jon
Jon

Reputation: 6275

I would guess that all data passed to and from the web worker is serialized (JSON parse / stringify). The func definitions on the object would get erased during serialization.

Instead of returning the model, could you return the queried array instead?

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Upvotes: 2

Related Questions