Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a Javascript-Object to Web Worker

I have an Object like that:

function A(id) {
    this.id = id;
}

A.prototype.getId = function() {
    return this.id;
}

It is included in a html-page as a file ("objects.js") as well as in the web worker with importScript("objects.js"). Now I create an instance of A in the html-page with "var a = new A()" and post it with "postMessage()" to a Web Worker.

The funny thing is that in the worker it still has the property "id" with its value but the prototype function is lost. I guess the reason may be that the prototype functions are "bound" to the html-page context and not to the web worker context.

So what I'm doing in the worker is that:

event.data.a.__proto__ = A.prototype;

It's working and I see it as some kind of cast...

Now my question is if that is the only and a valid way or if there's a better way of posting an object with prototype functions to a web worker. The object definition is available in both contexts...

like image 778
user2160787 Avatar asked Mar 12 '13 12:03

user2160787


People also ask

How do I send data to Webworker?

postMessage() The postMessage() method of the Worker interface sends a message to the worker's inner scope. This accepts a single parameter, which is the data to send to the worker. The data may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.

How can you send data using a worker object?

8. How can you send data using a Worker object? Explanation: Once you have a Worker object, you can send data to it with postMessage(). The value you pass to postMessage() will be cloned, and the resulting copy will be delivered to the worker via a message event.

How do I import a module into Webworker?

You should make this worker file available as a dynamic import. To do so, add import('your-npm-module/workerfile. js') somewhere in your code where it doesn't get executed, for example if (false) {…} . Meteor build system will detect that you may want to use it dynamically and will package it separately.

What are the limitations of web workers?

Limitations Of Web Workers The Web Workers API is a very powerful tool, but it has a few limitations: A worker can't directly manipulate the DOM and has limited access to methods and properties of the window object. A worker can not be run directly from the filesystem. It can only be run via a server.


1 Answers

The structure clone algorithm that is used for serializing data before sending it to the web worker does not walk the prototype chain (for details, see § 2.7.5 Safe passing of structured data). That explains why the derived functions are not preserved.

Beside manually restoring the object as you did, you could also creating a new object, which has the prototype functions, and use Object.assign to copy the properties from the received object.

Note that both workarounds assume that the prototype object and their functions are known to the web worker. In general, there is no automated way to transfer arbitrary objects while preserving functions (see my answer to this related question about sending objects with functions).

like image 98
Philipp Claßen Avatar answered Sep 29 '22 07:09

Philipp Claßen