Skip to content

WebReflection/symbol-structured-clone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

symbol-structured-clone

Social Media Photo by Ricardo Gomez Angel on Unsplash

A Symbol.structuredClone proposal / polyfill to hopefully help moving forward this long standing WHATWG issue, also discussed at TC39.

Live Test

// either globally, or as module, or as worker
import 'symbol-structured-clone';

class LiteralMap extends Map {
  // when cloned or via postMessage
  // it will return an object literal instead
  [Symbol.structuredClone]() {
    return Object.fromEntries([...this]);
  }
}

const lm = new LiteralMap([['a', 1], ['b', 2]]);

structuredClone(reference);
// {"a": 1, "b": 2}

postMessage(lm);
// event.data received as
// {"a": 1, "b": 2}

A note for Proxies

The has(key) trap should check if (key === Symbol.structuredClone) return true; in case there is also a get(ref, key) trap that eventually provide that callback for cloning purpose.


That's it 👋

About

A Symbol.structuredClone proposal / polyfill.

Resources

License

Stars

Watchers

Forks

Packages

No packages published