@dxos/echo-pipeline 0.8.2 → 0.8.3-main.672df60

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.
@@ -118,7 +118,7 @@ var import_invariant5 = require("@dxos/invariant");
118
118
  var import_keys3 = require("@dxos/keys");
119
119
  var import_log6 = require("@dxos/log");
120
120
  var import_util4 = require("@dxos/util");
121
- var A2 = __toESM(require("@automerge/automerge"));
121
+ var A = __toESM(require("@automerge/automerge"));
122
122
  var import_automerge_repo3 = require("@automerge/automerge-repo");
123
123
  var import_context6 = require("@dxos/context");
124
124
  var import_invariant6 = require("@dxos/invariant");
@@ -186,169 +186,13 @@ var import_async9 = require("@dxos/async");
186
186
  var import_context13 = require("@dxos/context");
187
187
  var import_log13 = require("@dxos/log");
188
188
  var import_echo_protocol7 = require("@dxos/echo-protocol");
189
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts";
190
- var MAX_UPDATE_FREQ = 10;
191
- var DocumentsSynchronizer = class extends import_context.Resource {
192
- constructor(_params) {
193
- super(), this._params = _params, this._syncStates = /* @__PURE__ */ new Map(), this._pendingUpdates = /* @__PURE__ */ new Set(), this._sendUpdatesJob = void 0;
194
- }
195
- addDocuments(documentIds, retryCounter = 0) {
196
- if (retryCounter > 3) {
197
- import_log2.log.warn("Failed to load document, retry limit reached", {
198
- documentIds
199
- }, {
200
- F: __dxlog_file,
201
- L: 50,
202
- S: this,
203
- C: (f, a) => f(...a)
204
- });
205
- return;
206
- }
207
- for (const documentId of documentIds) {
208
- this._params.repo.find(documentId).then(async (doc) => {
209
- await doc.whenReady();
210
- this._startSync(doc);
211
- this._pendingUpdates.add(doc.documentId);
212
- this._sendUpdatesJob.trigger();
213
- }).catch((error) => {
214
- import_log2.log.warn("Failed to load document, wraparound", {
215
- documentId,
216
- error
217
- }, {
218
- F: __dxlog_file,
219
- L: 64,
220
- S: this,
221
- C: (f, a) => f(...a)
222
- });
223
- this.addDocuments([
224
- documentId
225
- ], retryCounter + 1);
226
- });
227
- }
228
- }
229
- removeDocuments(documentIds) {
230
- for (const documentId of documentIds) {
231
- this._syncStates.get(documentId)?.clearSubscriptions?.();
232
- this._syncStates.delete(documentId);
233
- this._pendingUpdates.delete(documentId);
234
- }
235
- }
236
- async _open() {
237
- this._sendUpdatesJob = new import_async2.UpdateScheduler(this._ctx, this._checkAndSendUpdates.bind(this), {
238
- maxFrequency: MAX_UPDATE_FREQ
239
- });
240
- }
241
- async _close() {
242
- await this._sendUpdatesJob.join();
243
- this._syncStates.clear();
244
- }
245
- update(updates) {
246
- for (const { documentId, mutation, isNew } of updates) {
247
- if (isNew) {
248
- const { handle: doc } = this._params.repo.findWithProgress(documentId);
249
- doc.update((doc2) => import_automerge.next.loadIncremental(doc2, mutation));
250
- this._startSync(doc);
251
- } else {
252
- this._writeMutation(documentId, mutation);
253
- }
254
- }
255
- }
256
- _startSync(doc) {
257
- if (this._syncStates.has(doc.documentId)) {
258
- import_log2.log.info("Document already being synced", {
259
- documentId: doc.documentId
260
- }, {
261
- F: __dxlog_file,
262
- L: 103,
263
- S: this,
264
- C: (f, a) => f(...a)
265
- });
266
- return;
267
- }
268
- const syncState = {
269
- handle: doc
270
- };
271
- this._subscribeForChanges(syncState);
272
- this._syncStates.set(doc.documentId, syncState);
273
- }
274
- _subscribeForChanges(syncState) {
275
- const handler = () => {
276
- this._pendingUpdates.add(syncState.handle.documentId);
277
- this._sendUpdatesJob.trigger();
278
- };
279
- syncState.handle.on("heads-changed", handler);
280
- syncState.clearSubscriptions = () => syncState.handle.off("heads-changed", handler);
281
- }
282
- async _checkAndSendUpdates() {
283
- const updates = [];
284
- const docsWithPendingUpdates = Array.from(this._pendingUpdates);
285
- this._pendingUpdates.clear();
286
- for (const documentId of docsWithPendingUpdates) {
287
- const update = this._getPendingChanges(documentId);
288
- if (update) {
289
- updates.push({
290
- documentId,
291
- mutation: update
292
- });
293
- }
294
- }
295
- if (updates.length > 0) {
296
- this._params.sendUpdates({
297
- updates
298
- });
299
- }
300
- }
301
- _getPendingChanges(documentId) {
302
- const syncState = this._syncStates.get(documentId);
303
- (0, import_invariant2.invariant)(syncState, "Sync state for document not found", {
304
- F: __dxlog_file,
305
- L: 144,
306
- S: this,
307
- A: [
308
- "syncState",
309
- "'Sync state for document not found'"
310
- ]
311
- });
312
- const handle = syncState.handle;
313
- if (!handle || !handle.isReady() || !handle.doc()) {
314
- return;
315
- }
316
- const doc = handle.doc();
317
- const mutation = syncState.lastSentHead ? import_automerge.next.saveSince(doc, syncState.lastSentHead) : import_automerge.next.save(doc);
318
- if (mutation.length === 0) {
319
- return;
320
- }
321
- syncState.lastSentHead = import_automerge.next.getHeads(doc);
322
- return mutation;
323
- }
324
- _writeMutation(documentId, mutation) {
325
- const syncState = this._syncStates.get(documentId);
326
- (0, import_invariant2.invariant)(syncState, "Sync state for document not found", {
327
- F: __dxlog_file,
328
- L: 160,
329
- S: this,
330
- A: [
331
- "syncState",
332
- "'Sync state for document not found'"
333
- ]
334
- });
335
- syncState.handle.update((doc) => {
336
- const headsBefore = import_automerge.next.getHeads(doc);
337
- const newDoc = import_automerge.next.loadIncremental(doc, mutation);
338
- if (import_automerge.next.equals(headsBefore, syncState.lastSentHead)) {
339
- syncState.lastSentHead = import_automerge.next.getHeads(newDoc);
340
- }
341
- return newDoc;
342
- });
343
- }
344
- };
345
189
  function _ts_decorate(decorators, target, key, desc) {
346
190
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
347
191
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
348
192
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
349
193
  return c > 3 && r && Object.defineProperty(target, key, r), r;
350
194
  }
351
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts";
195
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts";
352
196
  var MIN_QUERY_INTERVAL = 5e3;
353
197
  var POLL_INTERVAL = 3e4;
354
198
  var CollectionSynchronizer = class extends import_context3.Resource {
@@ -386,7 +230,7 @@ var CollectionSynchronizer = class extends import_context3.Resource {
386
230
  collectionId,
387
231
  state
388
232
  }, {
389
- F: __dxlog_file2,
233
+ F: __dxlog_file,
390
234
  L: 76,
391
235
  S: this,
392
236
  C: (f, a) => f(...a)
@@ -405,7 +249,7 @@ var CollectionSynchronizer = class extends import_context3.Resource {
405
249
  (0, import_log4.log)("clearLocalCollectionState", {
406
250
  collectionId
407
251
  }, {
408
- F: __dxlog_file2,
252
+ F: __dxlog_file,
409
253
  L: 90,
410
254
  S: this,
411
255
  C: (f, a) => f(...a)
@@ -488,7 +332,7 @@ var CollectionSynchronizer = class extends import_context3.Resource {
488
332
  peerId,
489
333
  state
490
334
  }, {
491
- F: __dxlog_file2,
335
+ F: __dxlog_file,
492
336
  L: 171,
493
337
  S: this,
494
338
  C: (f, a) => f(...a)
@@ -591,7 +435,7 @@ function _ts_decorate2(decorators, target, key, desc) {
591
435
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
592
436
  return c > 3 && r && Object.defineProperty(target, key, r), r;
593
437
  }
594
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
438
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
595
439
  var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
596
440
  constructor(_params) {
597
441
  super(), this._params = _params, this._replicators = /* @__PURE__ */ new Set(), this._connections = /* @__PURE__ */ new Map(), this._lifecycleState = import_context4.LifecycleState.CLOSED, this._connected = new import_async5.Trigger(), this._ready = new import_async5.Trigger();
@@ -637,7 +481,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
637
481
  }
638
482
  async addReplicator(replicator) {
639
483
  (0, import_invariant4.invariant)(this._lifecycleState === import_context4.LifecycleState.OPEN, void 0, {
640
- F: __dxlog_file3,
484
+ F: __dxlog_file2,
641
485
  L: 115,
642
486
  S: this,
643
487
  A: [
@@ -646,7 +490,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
646
490
  ]
647
491
  });
648
492
  (0, import_invariant4.invariant)(this.peerId, void 0, {
649
- F: __dxlog_file3,
493
+ F: __dxlog_file2,
650
494
  L: 116,
651
495
  S: this,
652
496
  A: [
@@ -655,7 +499,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
655
499
  ]
656
500
  });
657
501
  (0, import_invariant4.invariant)(!this._replicators.has(replicator), void 0, {
658
- F: __dxlog_file3,
502
+ F: __dxlog_file2,
659
503
  L: 117,
660
504
  S: this,
661
505
  A: [
@@ -679,7 +523,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
679
523
  }
680
524
  async removeReplicator(replicator) {
681
525
  (0, import_invariant4.invariant)(this._lifecycleState === import_context4.LifecycleState.OPEN, void 0, {
682
- F: __dxlog_file3,
526
+ F: __dxlog_file2,
683
527
  L: 136,
684
528
  S: this,
685
529
  A: [
@@ -688,7 +532,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
688
532
  ]
689
533
  });
690
534
  (0, import_invariant4.invariant)(this._replicators.has(replicator), void 0, {
691
- F: __dxlog_file3,
535
+ F: __dxlog_file2,
692
536
  L: 137,
693
537
  S: this,
694
538
  A: [
@@ -744,7 +588,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
744
588
  }).catch((err) => {
745
589
  if (connectionEntry.isOpen) {
746
590
  import_log5.log.catch(err, void 0, {
747
- F: __dxlog_file3,
591
+ F: __dxlog_file2,
748
592
  L: 197,
749
593
  S: this,
750
594
  C: (f, a) => f(...a)
@@ -765,13 +609,13 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
765
609
  (0, import_log5.log)("Connection opened", {
766
610
  peerId: connection.peerId
767
611
  }, {
768
- F: __dxlog_file3,
612
+ F: __dxlog_file2,
769
613
  L: 215,
770
614
  S: this,
771
615
  C: (f, a) => f(...a)
772
616
  });
773
617
  (0, import_invariant4.invariant)(!this._connections.has(connection.peerId), void 0, {
774
- F: __dxlog_file3,
618
+ F: __dxlog_file2,
775
619
  L: 216,
776
620
  S: this,
777
621
  A: [
@@ -800,7 +644,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
800
644
  } catch (err) {
801
645
  if (connectionEntry.isOpen) {
802
646
  import_log5.log.catch(err, void 0, {
803
- F: __dxlog_file3,
647
+ F: __dxlog_file2,
804
648
  L: 235,
805
649
  S: this,
806
650
  C: (f, a) => f(...a)
@@ -811,7 +655,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
811
655
  (0, import_log5.log)("emit peer-candidate", {
812
656
  peerId: connection.peerId
813
657
  }, {
814
- F: __dxlog_file3,
658
+ F: __dxlog_file2,
815
659
  L: 240,
816
660
  S: this,
817
661
  C: (f, a) => f(...a)
@@ -843,14 +687,14 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
843
687
  (0, import_log5.log)("Connection auth scope changed", {
844
688
  peerId: connection.peerId
845
689
  }, {
846
- F: __dxlog_file3,
690
+ F: __dxlog_file2,
847
691
  L: 268,
848
692
  S: this,
849
693
  C: (f, a) => f(...a)
850
694
  });
851
695
  const entry = this._connections.get(connection.peerId);
852
696
  (0, import_invariant4.invariant)(entry, void 0, {
853
- F: __dxlog_file3,
697
+ F: __dxlog_file2,
854
698
  L: 270,
855
699
  S: this,
856
700
  A: [
@@ -867,14 +711,14 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
867
711
  (0, import_log5.log)("Connection closed", {
868
712
  peerId: connection.peerId
869
713
  }, {
870
- F: __dxlog_file3,
714
+ F: __dxlog_file2,
871
715
  L: 276,
872
716
  S: this,
873
717
  C: (f, a) => f(...a)
874
718
  });
875
719
  const entry = this._connections.get(connection.peerId);
876
720
  (0, import_invariant4.invariant)(entry, void 0, {
877
- F: __dxlog_file3,
721
+ F: __dxlog_file2,
878
722
  L: 278,
879
723
  S: this,
880
724
  A: [
@@ -888,13 +732,13 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
888
732
  });
889
733
  this._params.monitor?.recordPeerDisconnected(connection.peerId);
890
734
  void entry.reader.cancel().catch((err) => import_log5.log.catch(err, void 0, {
891
- F: __dxlog_file3,
735
+ F: __dxlog_file2,
892
736
  L: 284,
893
737
  S: this,
894
738
  C: (f, a) => f(...a)
895
739
  }));
896
740
  void entry.writer.abort().catch((err) => import_log5.log.catch(err, void 0, {
897
- F: __dxlog_file3,
741
+ F: __dxlog_file2,
898
742
  L: 285,
899
743
  S: this,
900
744
  C: (f, a) => f(...a)
@@ -1052,7 +896,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1052
896
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1053
897
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1054
898
  }
1055
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
899
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
1056
900
  var FIND_PARAMS = {
1057
901
  allowableStates: [
1058
902
  "ready",
@@ -1213,7 +1057,7 @@ var AutomergeHost = class extends import_context2.Resource {
1213
1057
  if (headsToWait.length > 0) {
1214
1058
  await Promise.all(headsToWait.map(async (entry, index) => {
1215
1059
  const handle = await this.loadDoc(import_context2.Context.default(void 0, {
1216
- F: __dxlog_file4,
1060
+ F: __dxlog_file3,
1217
1061
  L: 288
1218
1062
  }), entry.documentId);
1219
1063
  await waitForHeads(handle, entry.heads);
@@ -1226,7 +1070,7 @@ var AutomergeHost = class extends import_context2.Resource {
1226
1070
  import_log3.log.info("re-indexing heads for document", {
1227
1071
  documentId
1228
1072
  }, {
1229
- F: __dxlog_file4,
1073
+ F: __dxlog_file3,
1230
1074
  L: 302,
1231
1075
  S: this,
1232
1076
  C: (f, a) => f(...a)
@@ -1236,7 +1080,7 @@ var AutomergeHost = class extends import_context2.Resource {
1236
1080
  import_log3.log.warn("document is not available locally, skipping", {
1237
1081
  documentId
1238
1082
  }, {
1239
- F: __dxlog_file4,
1083
+ F: __dxlog_file3,
1240
1084
  L: 305,
1241
1085
  S: this,
1242
1086
  C: (f, a) => f(...a)
@@ -1249,7 +1093,7 @@ var AutomergeHost = class extends import_context2.Resource {
1249
1093
  await batch.write();
1250
1094
  }
1251
1095
  import_log3.log.info("done re-indexing heads", void 0, {
1252
- F: __dxlog_file4,
1096
+ F: __dxlog_file3,
1253
1097
  L: 314,
1254
1098
  S: this,
1255
1099
  C: (f, a) => f(...a)
@@ -1470,7 +1314,7 @@ var AutomergeHost = class extends import_context2.Resource {
1470
1314
  toReplicate,
1471
1315
  count: toReplicate.length
1472
1316
  }, {
1473
- F: __dxlog_file4,
1317
+ F: __dxlog_file3,
1474
1318
  L: 557,
1475
1319
  S: this,
1476
1320
  C: (f, a) => f(...a)
@@ -1530,7 +1374,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1530
1374
  };
1531
1375
  var decodeCollectionState = (state) => {
1532
1376
  (0, import_invariant3.invariant)(typeof state === "object" && state !== null, "Invalid state", {
1533
- F: __dxlog_file4,
1377
+ F: __dxlog_file3,
1534
1378
  L: 608,
1535
1379
  S: void 0,
1536
1380
  A: [
@@ -1543,7 +1387,7 @@ var decodeCollectionState = (state) => {
1543
1387
  var encodeCollectionState = (state) => {
1544
1388
  return state;
1545
1389
  };
1546
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1390
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1547
1391
  var DEFAULT_FACTORY = (params) => new import_teleport_extension_automerge_replicator.AutomergeReplicator(...params);
1548
1392
  var MeshReplicatorConnection = class extends import_context6.Resource {
1549
1393
  constructor(_params) {
@@ -1558,7 +1402,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1558
1402
  this.writable = new WritableStream({
1559
1403
  write: async (message, controller) => {
1560
1404
  (0, import_invariant6.invariant)(this._isEnabled, "Writing to a disabled connection", {
1561
- F: __dxlog_file5,
1405
+ F: __dxlog_file4,
1562
1406
  L: 51,
1563
1407
  S: this,
1564
1408
  A: [
@@ -1591,7 +1435,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1591
1435
  thisPeerId: this.peerId,
1592
1436
  remotePeerId: remotePeerId.toHex()
1593
1437
  }, {
1594
- F: __dxlog_file5,
1438
+ F: __dxlog_file4,
1595
1439
  L: 80,
1596
1440
  S: this,
1597
1441
  C: (f, a) => f(...a)
@@ -1618,7 +1462,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1618
1462
  }
1619
1463
  get peerId() {
1620
1464
  (0, import_invariant6.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1621
- F: __dxlog_file5,
1465
+ F: __dxlog_file4,
1622
1466
  L: 106,
1623
1467
  S: this,
1624
1468
  A: [
@@ -1643,7 +1487,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1643
1487
  */
1644
1488
  enable() {
1645
1489
  (0, import_invariant6.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1646
- F: __dxlog_file5,
1490
+ F: __dxlog_file4,
1647
1491
  L: 127,
1648
1492
  S: this,
1649
1493
  A: [
@@ -1662,7 +1506,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1662
1506
  };
1663
1507
  var logSendSync = (message) => {
1664
1508
  (0, import_log7.log)("sendSyncMessage", () => {
1665
- const decodedSyncMessage = message.type === "sync" && message.data ? A2.decodeSyncMessage(message.data) : void 0;
1509
+ const decodedSyncMessage = message.type === "sync" && message.data ? A.decodeSyncMessage(message.data) : void 0;
1666
1510
  return {
1667
1511
  sync: decodedSyncMessage && {
1668
1512
  headsLength: decodedSyncMessage.heads.length,
@@ -1674,18 +1518,18 @@ var logSendSync = (message) => {
1674
1518
  to: message.targetId
1675
1519
  };
1676
1520
  }, {
1677
- F: __dxlog_file5,
1521
+ F: __dxlog_file4,
1678
1522
  L: 140,
1679
1523
  S: void 0,
1680
1524
  C: (f, a) => f(...a)
1681
1525
  });
1682
1526
  };
1683
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1527
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1684
1528
  var deriveCollectionIdFromSpaceId = (spaceId, rootDocumentId) => rootDocumentId ? `space:${spaceId}:${rootDocumentId}` : `space:${spaceId}`;
1685
1529
  var getSpaceIdFromCollectionId = (collectionId) => {
1686
1530
  const spaceId = collectionId.split(":")[1];
1687
1531
  (0, import_invariant7.invariant)(import_keys4.SpaceId.isValid(spaceId), void 0, {
1688
- F: __dxlog_file6,
1532
+ F: __dxlog_file5,
1689
1533
  L: 16,
1690
1534
  S: void 0,
1691
1535
  A: [
@@ -1695,7 +1539,7 @@ var getSpaceIdFromCollectionId = (collectionId) => {
1695
1539
  });
1696
1540
  return spaceId;
1697
1541
  };
1698
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1542
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1699
1543
  var MeshEchoReplicator = class {
1700
1544
  constructor() {
1701
1545
  this._connectionsPerPeer = /* @__PURE__ */ new Map();
@@ -1721,7 +1565,7 @@ var MeshEchoReplicator = class {
1721
1565
  }
1722
1566
  createExtension(extensionFactory) {
1723
1567
  (0, import_invariant5.invariant)(this._context, void 0, {
1724
- F: __dxlog_file7,
1568
+ F: __dxlog_file6,
1725
1569
  L: 67,
1726
1570
  S: this,
1727
1571
  A: [
@@ -1736,13 +1580,13 @@ var MeshEchoReplicator = class {
1736
1580
  (0, import_log6.log)("onRemoteConnected", {
1737
1581
  peerId: connection.peerId
1738
1582
  }, {
1739
- F: __dxlog_file7,
1583
+ F: __dxlog_file6,
1740
1584
  L: 73,
1741
1585
  S: this,
1742
1586
  C: (f, a) => f(...a)
1743
1587
  });
1744
1588
  (0, import_invariant5.invariant)(this._context, void 0, {
1745
- F: __dxlog_file7,
1589
+ F: __dxlog_file6,
1746
1590
  L: 74,
1747
1591
  S: this,
1748
1592
  A: [
@@ -1767,7 +1611,7 @@ var MeshEchoReplicator = class {
1767
1611
  (0, import_log6.log)("onRemoteDisconnected", {
1768
1612
  peerId: connection.peerId
1769
1613
  }, {
1770
- F: __dxlog_file7,
1614
+ F: __dxlog_file6,
1771
1615
  L: 88,
1772
1616
  S: this,
1773
1617
  C: (f, a) => f(...a)
@@ -1779,7 +1623,7 @@ var MeshEchoReplicator = class {
1779
1623
  import_log6.log.warn("disconnected connection not found", {
1780
1624
  peerId: connection.peerId
1781
1625
  }, {
1782
- F: __dxlog_file7,
1626
+ F: __dxlog_file6,
1783
1627
  L: 96,
1784
1628
  S: this,
1785
1629
  C: (f, a) => f(...a)
@@ -1801,13 +1645,13 @@ var MeshEchoReplicator = class {
1801
1645
  peerId: connection.peerId,
1802
1646
  documentId: params.documentId
1803
1647
  }, {
1804
- F: __dxlog_file7,
1648
+ F: __dxlog_file6,
1805
1649
  L: 114,
1806
1650
  S: this,
1807
1651
  C: (f, a) => f(...a)
1808
1652
  });
1809
1653
  (0, import_invariant5.invariant)(this._context, void 0, {
1810
- F: __dxlog_file7,
1654
+ F: __dxlog_file6,
1811
1655
  L: 115,
1812
1656
  S: this,
1813
1657
  A: [
@@ -1827,7 +1671,7 @@ var MeshEchoReplicator = class {
1827
1671
  documentId: params.documentId,
1828
1672
  acceptDocument: remoteDocumentExists
1829
1673
  }, {
1830
- F: __dxlog_file7,
1674
+ F: __dxlog_file6,
1831
1675
  L: 123,
1832
1676
  S: this,
1833
1677
  C: (f, a) => f(...a)
@@ -1841,7 +1685,7 @@ var MeshEchoReplicator = class {
1841
1685
  peerId: connection.peerId,
1842
1686
  documentId: params.documentId
1843
1687
  }, {
1844
- F: __dxlog_file7,
1688
+ F: __dxlog_file6,
1845
1689
  L: 139,
1846
1690
  S: this,
1847
1691
  C: (f, a) => f(...a)
@@ -1857,7 +1701,7 @@ var MeshEchoReplicator = class {
1857
1701
  spaceKey,
1858
1702
  isAuthorized
1859
1703
  }, {
1860
- F: __dxlog_file7,
1704
+ F: __dxlog_file6,
1861
1705
  L: 147,
1862
1706
  S: this,
1863
1707
  C: (f, a) => f(...a)
@@ -1865,7 +1709,7 @@ var MeshEchoReplicator = class {
1865
1709
  return isAuthorized;
1866
1710
  } catch (err) {
1867
1711
  import_log6.log.catch(err, void 0, {
1868
- F: __dxlog_file7,
1712
+ F: __dxlog_file6,
1869
1713
  L: 157,
1870
1714
  S: this,
1871
1715
  C: (f, a) => f(...a)
@@ -1881,7 +1725,7 @@ var MeshEchoReplicator = class {
1881
1725
  peerId: connection.peerId,
1882
1726
  collectionId
1883
1727
  }, {
1884
- F: __dxlog_file7,
1728
+ F: __dxlog_file6,
1885
1729
  L: 167,
1886
1730
  S: this,
1887
1731
  C: (f, a) => f(...a)
@@ -1900,7 +1744,7 @@ var MeshEchoReplicator = class {
1900
1744
  spaceKey,
1901
1745
  deviceKey
1902
1746
  }, {
1903
- F: __dxlog_file7,
1747
+ F: __dxlog_file6,
1904
1748
  L: 184,
1905
1749
  S: this,
1906
1750
  C: (f, a) => f(...a)
@@ -2273,6 +2117,162 @@ var createStorageAverages = () => ({
2273
2117
  var getByteCount = (message) => {
2274
2118
  return message.type.length + message.senderId.length + message.targetId.length + (message.data?.byteLength ?? 0) + (message.documentId?.length ?? 0);
2275
2119
  };
2120
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts";
2121
+ var MAX_UPDATE_FREQ = 10;
2122
+ var DocumentsSynchronizer = class extends import_context.Resource {
2123
+ constructor(_params) {
2124
+ super(), this._params = _params, this._syncStates = /* @__PURE__ */ new Map(), this._pendingUpdates = /* @__PURE__ */ new Set(), this._sendUpdatesJob = void 0;
2125
+ }
2126
+ addDocuments(documentIds, retryCounter = 0) {
2127
+ if (retryCounter > 3) {
2128
+ import_log2.log.warn("Failed to load document, retry limit reached", {
2129
+ documentIds
2130
+ }, {
2131
+ F: __dxlog_file7,
2132
+ L: 52,
2133
+ S: this,
2134
+ C: (f, a) => f(...a)
2135
+ });
2136
+ return;
2137
+ }
2138
+ for (const documentId of documentIds) {
2139
+ this._params.repo.find(documentId).then(async (doc) => {
2140
+ await doc.whenReady();
2141
+ this._startSync(doc);
2142
+ this._pendingUpdates.add(doc.documentId);
2143
+ this._sendUpdatesJob.trigger();
2144
+ }).catch((error) => {
2145
+ import_log2.log.warn("Failed to load document, wraparound", {
2146
+ documentId,
2147
+ error
2148
+ }, {
2149
+ F: __dxlog_file7,
2150
+ L: 66,
2151
+ S: this,
2152
+ C: (f, a) => f(...a)
2153
+ });
2154
+ this.addDocuments([
2155
+ documentId
2156
+ ], retryCounter + 1);
2157
+ });
2158
+ }
2159
+ }
2160
+ removeDocuments(documentIds) {
2161
+ for (const documentId of documentIds) {
2162
+ this._syncStates.get(documentId)?.clearSubscriptions?.();
2163
+ this._syncStates.delete(documentId);
2164
+ this._pendingUpdates.delete(documentId);
2165
+ }
2166
+ }
2167
+ async _open() {
2168
+ this._sendUpdatesJob = new import_async2.UpdateScheduler(this._ctx, this._checkAndSendUpdates.bind(this), {
2169
+ maxFrequency: MAX_UPDATE_FREQ
2170
+ });
2171
+ }
2172
+ async _close() {
2173
+ await this._sendUpdatesJob.join();
2174
+ this._syncStates.clear();
2175
+ }
2176
+ async update(updates) {
2177
+ for (const { documentId, mutation, isNew } of updates) {
2178
+ if (isNew) {
2179
+ const doc = await this._params.repo.find(documentId, FIND_PARAMS);
2180
+ doc.update((doc2) => import_automerge.next.loadIncremental(doc2, mutation));
2181
+ this._startSync(doc);
2182
+ } else {
2183
+ this._writeMutation(documentId, mutation);
2184
+ }
2185
+ }
2186
+ }
2187
+ _startSync(doc) {
2188
+ if (this._syncStates.has(doc.documentId)) {
2189
+ import_log2.log.info("Document already being synced", {
2190
+ documentId: doc.documentId
2191
+ }, {
2192
+ F: __dxlog_file7,
2193
+ L: 105,
2194
+ S: this,
2195
+ C: (f, a) => f(...a)
2196
+ });
2197
+ return;
2198
+ }
2199
+ const syncState = {
2200
+ handle: doc
2201
+ };
2202
+ this._subscribeForChanges(syncState);
2203
+ this._syncStates.set(doc.documentId, syncState);
2204
+ }
2205
+ _subscribeForChanges(syncState) {
2206
+ const handler = () => {
2207
+ this._pendingUpdates.add(syncState.handle.documentId);
2208
+ this._sendUpdatesJob.trigger();
2209
+ };
2210
+ syncState.handle.on("heads-changed", handler);
2211
+ syncState.clearSubscriptions = () => syncState.handle.off("heads-changed", handler);
2212
+ }
2213
+ async _checkAndSendUpdates() {
2214
+ const updates = [];
2215
+ const docsWithPendingUpdates = Array.from(this._pendingUpdates);
2216
+ this._pendingUpdates.clear();
2217
+ for (const documentId of docsWithPendingUpdates) {
2218
+ const update = this._getPendingChanges(documentId);
2219
+ if (update) {
2220
+ updates.push({
2221
+ documentId,
2222
+ mutation: update
2223
+ });
2224
+ }
2225
+ }
2226
+ if (updates.length > 0) {
2227
+ this._params.sendUpdates({
2228
+ updates
2229
+ });
2230
+ }
2231
+ }
2232
+ _getPendingChanges(documentId) {
2233
+ const syncState = this._syncStates.get(documentId);
2234
+ (0, import_invariant2.invariant)(syncState, "Sync state for document not found", {
2235
+ F: __dxlog_file7,
2236
+ L: 146,
2237
+ S: this,
2238
+ A: [
2239
+ "syncState",
2240
+ "'Sync state for document not found'"
2241
+ ]
2242
+ });
2243
+ const handle = syncState.handle;
2244
+ if (!handle || !handle.isReady() || !handle.doc()) {
2245
+ return;
2246
+ }
2247
+ const doc = handle.doc();
2248
+ const mutation = syncState.lastSentHead ? import_automerge.next.saveSince(doc, syncState.lastSentHead) : import_automerge.next.save(doc);
2249
+ if (mutation.length === 0) {
2250
+ return;
2251
+ }
2252
+ syncState.lastSentHead = import_automerge.next.getHeads(doc);
2253
+ return mutation;
2254
+ }
2255
+ _writeMutation(documentId, mutation) {
2256
+ const syncState = this._syncStates.get(documentId);
2257
+ (0, import_invariant2.invariant)(syncState, "Sync state for document not found", {
2258
+ F: __dxlog_file7,
2259
+ L: 162,
2260
+ S: this,
2261
+ A: [
2262
+ "syncState",
2263
+ "'Sync state for document not found'"
2264
+ ]
2265
+ });
2266
+ syncState.handle.update((doc) => {
2267
+ const headsBefore = import_automerge.next.getHeads(doc);
2268
+ const newDoc = import_automerge.next.loadIncremental(doc, mutation);
2269
+ if (import_automerge.next.equals(headsBefore, syncState.lastSentHead)) {
2270
+ syncState.lastSentHead = import_automerge.next.getHeads(newDoc);
2271
+ }
2272
+ return newDoc;
2273
+ });
2274
+ }
2275
+ };
2276
2276
  var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
2277
2277
  var DataServiceImpl = class {
2278
2278
  constructor(params) {
@@ -2331,7 +2331,7 @@ var DataServiceImpl = class {
2331
2331
  "'Subscription not found'"
2332
2332
  ]
2333
2333
  });
2334
- synchronizer.update(request.updates);
2334
+ await synchronizer.update(request.updates);
2335
2335
  }
2336
2336
  async flush(request) {
2337
2337
  await this._automergeHost.flush(request);
@@ -2967,6 +2967,7 @@ var ExecutionTrace = Object.freeze({
2967
2967
  return go(trace6, 0);
2968
2968
  }
2969
2969
  });
2970
+ var TRACE_QUERY_EXECUTION = false;
2970
2971
  var QueryExecutor = class extends import_context10.Resource {
2971
2972
  constructor(options) {
2972
2973
  super();
@@ -3006,7 +3007,7 @@ var QueryExecutor = class extends import_context10.Resource {
3006
3007
  async execQuery() {
3007
3008
  (0, import_invariant10.invariant)(this._lifecycleState === import_context10.LifecycleState.OPEN, void 0, {
3008
3009
  F: __dxlog_file11,
3009
- L: 171,
3010
+ L: 173,
3010
3011
  S: this,
3011
3012
  A: [
3012
3013
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3022,6 +3023,9 @@ var QueryExecutor = class extends import_context10.Resource {
3022
3023
  });
3023
3024
  this._trace = trace6;
3024
3025
  const changed = prevResultSet.length !== workingSet.length || prevResultSet.some((item, index) => workingSet[index].objectId !== item.objectId || workingSet[index].spaceId !== item.spaceId || workingSet[index].documentId !== item.documentId);
3026
+ if (TRACE_QUERY_EXECUTION) {
3027
+ console.log(ExecutionTrace.format(trace6));
3028
+ }
3025
3029
  return {
3026
3030
  changed
3027
3031
  };