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: /
And a pic of Firelord Ozai:
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/V8aCy/6/
And a pic of Firelord Ozai:
Share Improve this question edited Oct 29, 2013 at 15:31 mako-taco asked Oct 29, 2013 at 15:02 mako-tacomako-taco 7325 silver badges10 bronze badges 1- See this answer: stackoverflow./a/42376465/1034782 Simple. Worked for me. – Donatello Commented Aug 24, 2018 at 15:39
1 Answer
Reset to default 6You'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.