2017-09-10 1 views
1

Ich versuche, eine Nachricht an eine Service-Worker-Instanz zu senden. Ich erhalte die folgende Fehlermeldung:Fehler beim Ausführen von 'postMessage' auf 'ServiceWorker': Funktion konnte nicht geklont werden

Failed to execute 'postMessage' on 'ServiceWorker': function(){ obj.removeListener(ev, fn); } could not be cloned.

Mein Code ist wie folgt:

var socket = io(); 

function onYouTubeIframeAPIReady() { 
    //Tell the service worker who I am 
    navigator.serviceWorker.ready.then(serviceWorkerRegistration => { 
     navigator.serviceWorker.controller.postMessage({ 
      name: 'socketInit', 
      value: socket 
     }); 
    }); 
} 

Jede Idee, warum dies geschieht?

Antwort

1

Eigentlich gefunden die Antwort sofort, es ist in der Dokumentation. Sie können grundsätzlich keine Function-Eigenschafts (die wahrscheinlich irgendwo in der io() Objekt gibt es) haben, weil es nicht geklont werden kann:

Parameters
message The message to send to the service worker. This can be any structured-clonable type.

A structured-clonable-type wie definiert ist:

The structured clone algorithm is an algorithm defined by the HTML5 specification for copying complex JavaScript objects. It is used internally when transferring data to and from Workers via postMessage(). It builds up a clone by recursing through the input object while maintaining a map of previously visited references in order to avoid infinitely traversing cycles.

Things that don't work with structured clone

Error and Function objects cannot be duplicated by the structured clone algorithm; attempting to do so will throw a DATA_CLONE_ERR exception. Attempting to clone DOM nodes will likewise throw a DATA_CLONE_ERR exception. Certain parameters of objects are not preserved: The lastIndex field of RegExp objects is not preserved. Property descriptors, setters, and getters (as well as similar metadata-like features) are not duplicated. For example, if an object is marked read-only using a property descriptor, it will be read-write in the duplicate, since that's the default condition. The prototype chain does not get walked and duplicated.

Verwandte Themen