@fairfox/polly 0.26.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/mesh.js CHANGED
@@ -1244,6 +1244,13 @@ class MeshSignalingClient {
1244
1244
  }
1245
1245
  }
1246
1246
 
1247
+ // src/shared/lib/mesh-state.ts
1248
+ import {
1249
+ Automerge,
1250
+ interpretAsDocumentId
1251
+ } from "@automerge/automerge-repo/slim";
1252
+ import nacl3 from "tweetnacl";
1253
+
1247
1254
  // src/shared/lib/crdt-specialised.ts
1248
1255
  import { Counter, updateText } from "@automerge/automerge-repo/slim";
1249
1256
  import { effect, signal } from "@preact/signals";
@@ -1620,11 +1627,9 @@ function applyTopLevel(doc, value) {
1620
1627
  }
1621
1628
 
1622
1629
  // src/shared/lib/mesh-state.ts
1623
- var keyMapsByRepo = new WeakMap;
1624
1630
  var defaultRepo;
1625
1631
  function configureMeshState(repo) {
1626
1632
  defaultRepo = repo;
1627
- keyMapsByRepo.set(repo, new Map);
1628
1633
  }
1629
1634
  function resetMeshState() {
1630
1635
  defaultRepo = undefined;
@@ -1636,23 +1641,30 @@ function resolveRepo(option) {
1636
1641
  }
1637
1642
  return repo;
1638
1643
  }
1639
- function getKeyMap(repo) {
1640
- let map = keyMapsByRepo.get(repo);
1641
- if (!map) {
1642
- map = new Map;
1643
- keyMapsByRepo.set(repo, map);
1644
- }
1645
- return map;
1644
+ var DOC_ID_DOMAIN = "polly/meshState/v1";
1645
+ var keyEncoder = new TextEncoder;
1646
+ function deriveDocumentId(key) {
1647
+ const digest = nacl3.hash(keyEncoder.encode(`${DOC_ID_DOMAIN}:${key}`));
1648
+ const bytes = digest.slice(0, 16);
1649
+ return interpretAsDocumentId(bytes);
1646
1650
  }
1647
1651
  function buildHandleFactory(repo, key, initialDoc) {
1652
+ const documentId = deriveDocumentId(key);
1648
1653
  return async () => {
1649
- const map = getKeyMap(repo);
1650
- const existingId = map.get(key);
1651
- if (existingId !== undefined) {
1652
- return repo.find(existingId);
1654
+ const cached = repo.handles[documentId];
1655
+ if (cached) {
1656
+ await cached.whenReady(["ready", "unavailable"]);
1657
+ if (cached.state === "ready") {
1658
+ return cached;
1659
+ }
1660
+ }
1661
+ const stored = await repo.storageSubsystem?.loadDoc(documentId);
1662
+ if (stored) {
1663
+ return repo.find(documentId, { allowableStates: ["ready"] });
1653
1664
  }
1654
- const handle = repo.create(initialDoc);
1655
- map.set(key, handle.documentId);
1665
+ const seeded = Automerge.save(Automerge.from(initialDoc));
1666
+ const handle = repo.import(seeded, { docId: documentId });
1667
+ handle.doneLoading();
1656
1668
  return handle;
1657
1669
  };
1658
1670
  }
@@ -2426,4 +2438,4 @@ export {
2426
2438
  $meshCounter
2427
2439
  };
2428
2440
 
2429
- //# debugId=6CA75FA83D35A2A964756E2164756E21
2441
+ //# debugId=C21F9D16F4FA545F64756E2164756E21