Search code examples
javascriptangularjsweb-worker

Error cloning element from scope while using Angular and Web Workers


I have an Angular app and I'm trying to implement some functionality into a Web Worker. To achieve this I need to pass an object from the angular scope to the worker so it's processed and a result is generated.

I run into the following error:

Error: Failed to execute 'postMessage' on 'Worker': An object could not be cloned.

When calling worker.postMessage($scope.scopeObj);

Is there something special I have to do in order to send that object to the worker? It currently works with mock objects created manually.


Solution

  • Turns out the object I was trying to send had a call to a function inside, and as this document (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#Things_that_don%27t_work_with_structured_clones) indicates, that's one of the things that are not supported on the message.

    Removing the function from the object send fixed the error.