symbol-structured-clone

0.1.5 • Public • Published

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 👋

Dependents (0)

Package Sidebar

Install

npm i symbol-structured-clone

Weekly Downloads

7

Version

0.1.5

License

MIT

Unpacked Size

5.42 kB

Total Files

4

Last publish

Collaborators

  • webreflection