Question

I am working on a helper library called Ozai to make web workers easier, but am running in to a problem in firefox. I create a web worker from a URL Blob and attempt to post this payload to it:

msg = {
    "id":"0fae0ff8-bfd1-49ea-8139-3d03fb9584e4",
    "fn":"fn",
    "args":[100,200]
}

Using this code:

worker.postMessage(msg)

But it throws a DataCloneError exception. It looks like Firefox's implementation of structured cloning is failing on a very simple object. The code runs without problems on Chrome and Safari, but fails in the latest version of Firefox. Am I missing something here? How do I get around this (preferably without stringifying the payload)?

Here's a fiddle: http://jsfiddle.net/V8aCy/6/

And a pic of Firelord Ozai:

enter image description here

Était-ce utile?

La solution

You're trying to call postMessage with an object that has a property referencing arguments. That doesn't work because data has to be transferable, which means either fully JSON-serializable or implementing Transferable (e.g. ArrayBuffer), which arguments is not.

Use Array.prototype.slice.call(arguments, 0) to convert arguments into an array, which can be serialized (cloned) if the contents are OK.

Corrected fiddle.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top