Skip to content

Instantly share code, notes, and snippets.

@Vanilagy
Last active January 24, 2019 22:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vanilagy/d73e3f466d50d238b769cd9b391dd273 to your computer and use it in GitHub Desktop.
Save Vanilagy/d73e3f466d50d238b769cd9b391dd273 to your computer and use it in GitHub Desktop.
HTML Structured Clone Algorithm directly exposed in JavaScript using BroadcastChannels
// @Vanilagy 2019
//
// Example:
// structuredClone([5, {hello: 'world'}, new Set()]).then((data) => console.log(data));
// > [5, {hello: "world"}, Set(0)]
var structuredClone = (function() {
var sender = new BroadcastChannel('structuredClone'),
receiver = new BroadcastChannel('structuredClone');
return function(data) {
return new Promise((resolve) => {
receiver.onmessage = (e) => resolve(e.data);
sender.postMessage(data);
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment