WeakStorage and WeakRefMap are based on WeakRef objects allowing to store values as weak references.

Usage no npm install needed!

<script type="module">
  import actualwaveWeakStorage from 'https://cdn.skypack.dev/@actualwave/weak-storage';


Weak Storage

Map implementation for key-value pairs with weak referenced values. It is based on WeakRef spec and you can try it in Chrome Canary.

let key = {type: 'key1'};
let value = {type: 'value1'};
const storage = new WeakStorage();
storage.set(key, value);
storage.get(key); // {type: "value1"}
storage.getKey(value); // {type: "key1"}
storage.forEach(console.log) // {type: "value1"} {type: "key1"} WeakStorage{}

const keys = storage.keys();
keys.next(); // { done: false, value: {type: "key1"} }
keys.next(); // {done: true, value: undefined}
keys[Symbol.iterator]().next(); // { done: false, value: {type: "key1"} }

const values = storage.values();
values.next(); // { done: false, value: {type: "value1"} }
values.next(); // {done: true, value: undefined}

const entries = storage.entries();
entries.next(); // { done: false, value: [ {type: "key1"}, {type: "value1"} ] }
entries.next(); // {done: true, value: undefined}