0.1.5 • Published 1 year ago

symbol-structured-clone v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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 👋

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.0

1 year ago