File size: 817 Bytes
1944112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * In-memory StorageAdapter.
 *
 * For tests, and for a host that genuinely wants the registry to die with the
 * page. Note that the *weights* still live in Cache Storage and survive — only
 * the registry entry pointing at them is lost, which `ModelStore.verify()` will
 * then never be asked about. Do not use this in production for that reason.
 */

/** @returns {import("../engine/model-store.js").StorageAdapter} */
export function memoryStorage(initial = {}) {
  const store = new Map(Object.entries(initial));
  return {
    async get(key) {
      const keys = Array.isArray(key) ? key : [key];
      return Object.fromEntries(keys.filter((k) => store.has(k)).map((k) => [k, store.get(k)]));
    },
    async set(items) {
      for (const [k, v] of Object.entries(items)) store.set(k, v);
    },
  };
}