@dxos/echo-pipeline 0.8.2 → 0.8.3-main.7f5a14c

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.
Files changed (47) hide show
  1. package/dist/lib/browser/{chunk-3XSXS5EX.mjs → chunk-35I6ERLG.mjs} +2 -2
  2. package/dist/lib/browser/chunk-35I6ERLG.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +355 -352
  4. package/dist/lib/browser/index.mjs.map +3 -3
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +4 -4
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-SG2PL5RH.cjs → chunk-JXX6LF5U.cjs} +5 -5
  9. package/dist/lib/node/chunk-JXX6LF5U.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +321 -320
  11. package/dist/lib/node/index.cjs.map +3 -3
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +13 -13
  14. package/dist/lib/node/testing/index.cjs.map +3 -3
  15. package/dist/lib/node-esm/{chunk-3BZP75TJ.mjs → chunk-5BHLPT24.mjs} +2 -2
  16. package/dist/lib/node-esm/chunk-5BHLPT24.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +355 -352
  18. package/dist/lib/node-esm/index.mjs.map +3 -3
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/testing/index.mjs +4 -4
  21. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  22. package/dist/types/src/automerge/echo-network-adapter.d.ts +3 -3
  23. package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
  24. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
  25. package/dist/types/src/db-host/documents-synchronizer.d.ts +1 -1
  26. package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
  27. package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -1
  28. package/dist/types/src/query/query-executor.d.ts.map +1 -1
  29. package/package.json +35 -35
  30. package/src/automerge/automerge-host.ts +3 -3
  31. package/src/automerge/automerge-repo.test.ts +48 -4
  32. package/src/automerge/echo-network-adapter.test.ts +7 -8
  33. package/src/automerge/echo-network-adapter.ts +50 -46
  34. package/src/automerge/mesh-echo-replicator.ts +1 -0
  35. package/src/db-host/data-service.ts +1 -1
  36. package/src/db-host/documents-iterator.ts +1 -1
  37. package/src/db-host/documents-synchronizer.test.ts +1 -1
  38. package/src/db-host/documents-synchronizer.ts +5 -3
  39. package/src/db-host/query-service.ts +3 -3
  40. package/src/edge/echo-edge-replicator.ts +2 -3
  41. package/src/pipeline/pipeline.ts +1 -1
  42. package/src/query/query-executor.ts +6 -6
  43. package/src/testing/change-metadata.ts +1 -1
  44. package/src/testing/test-replicator.ts +2 -2
  45. package/dist/lib/browser/chunk-3XSXS5EX.mjs.map +0 -7
  46. package/dist/lib/node/chunk-SG2PL5RH.cjs.map +0 -7
  47. package/dist/lib/node-esm/chunk-3BZP75TJ.mjs.map +0 -7
@@ -28,11 +28,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var node_exports = {};
30
30
  __export(node_exports, {
31
- AuthExtension: () => import_chunk_SG2PL5RH.AuthExtension,
32
- AuthStatus: () => import_chunk_SG2PL5RH.AuthStatus,
31
+ AuthExtension: () => import_chunk_JXX6LF5U.AuthExtension,
32
+ AuthStatus: () => import_chunk_JXX6LF5U.AuthStatus,
33
33
  AutomergeHost: () => AutomergeHost,
34
- CredentialRetrieverExtension: () => import_chunk_SG2PL5RH.CredentialRetrieverExtension,
35
- CredentialServerExtension: () => import_chunk_SG2PL5RH.CredentialServerExtension,
34
+ CredentialRetrieverExtension: () => import_chunk_JXX6LF5U.CredentialRetrieverExtension,
35
+ CredentialServerExtension: () => import_chunk_JXX6LF5U.CredentialServerExtension,
36
36
  DataServiceImpl: () => DataServiceImpl,
37
37
  DatabaseRoot: () => DatabaseRoot,
38
38
  DocumentsSynchronizer: () => DocumentsSynchronizer,
@@ -42,25 +42,25 @@ __export(node_exports, {
42
42
  ExecutionTrace: () => ExecutionTrace,
43
43
  FIND_PARAMS: () => FIND_PARAMS,
44
44
  LevelDBStorageAdapter: () => LevelDBStorageAdapter,
45
- MOCK_AUTH_PROVIDER: () => import_chunk_SG2PL5RH.MOCK_AUTH_PROVIDER,
46
- MOCK_AUTH_VERIFIER: () => import_chunk_SG2PL5RH.MOCK_AUTH_VERIFIER,
45
+ MOCK_AUTH_PROVIDER: () => import_chunk_JXX6LF5U.MOCK_AUTH_PROVIDER,
46
+ MOCK_AUTH_VERIFIER: () => import_chunk_JXX6LF5U.MOCK_AUTH_VERIFIER,
47
47
  MeshEchoReplicator: () => MeshEchoReplicator,
48
- MetadataStore: () => import_chunk_SG2PL5RH.MetadataStore,
49
- Pipeline: () => import_chunk_SG2PL5RH.Pipeline,
48
+ MetadataStore: () => import_chunk_JXX6LF5U.MetadataStore,
49
+ Pipeline: () => import_chunk_JXX6LF5U.Pipeline,
50
50
  QueryExecutor: () => QueryExecutor,
51
51
  QueryPlan: () => QueryPlan,
52
52
  QueryPlanner: () => QueryPlanner,
53
53
  QueryServiceImpl: () => QueryServiceImpl,
54
- Space: () => import_chunk_SG2PL5RH.Space,
54
+ Space: () => import_chunk_JXX6LF5U.Space,
55
55
  SpaceDocumentListUpdatedEvent: () => SpaceDocumentListUpdatedEvent,
56
- SpaceManager: () => import_chunk_SG2PL5RH.SpaceManager,
57
- SpaceProtocol: () => import_chunk_SG2PL5RH.SpaceProtocol,
58
- SpaceProtocolSession: () => import_chunk_SG2PL5RH.SpaceProtocolSession,
56
+ SpaceManager: () => import_chunk_JXX6LF5U.SpaceManager,
57
+ SpaceProtocol: () => import_chunk_JXX6LF5U.SpaceProtocol,
58
+ SpaceProtocolSession: () => import_chunk_JXX6LF5U.SpaceProtocolSession,
59
59
  SpaceStateManager: () => SpaceStateManager,
60
- TimeframeClock: () => import_chunk_SG2PL5RH.TimeframeClock,
61
- codec: () => import_chunk_SG2PL5RH.codec,
62
- createIdFromSpaceKey: () => import_chunk_SG2PL5RH.createIdFromSpaceKey,
63
- createMappedFeedWriter: () => import_chunk_SG2PL5RH.createMappedFeedWriter,
60
+ TimeframeClock: () => import_chunk_JXX6LF5U.TimeframeClock,
61
+ codec: () => import_chunk_JXX6LF5U.codec,
62
+ createIdFromSpaceKey: () => import_chunk_JXX6LF5U.createIdFromSpaceKey,
63
+ createMappedFeedWriter: () => import_chunk_JXX6LF5U.createMappedFeedWriter,
64
64
  deriveCollectionIdFromSpaceId: () => deriveCollectionIdFromSpaceId,
65
65
  diffCollectionState: () => diffCollectionState,
66
66
  encodingOptions: () => encodingOptions,
@@ -68,15 +68,15 @@ __export(node_exports, {
68
68
  filterMatchValue: () => import_chunk_HOPOFWAL.filterMatchValue,
69
69
  findInlineObjectOfType: () => findInlineObjectOfType,
70
70
  getSpaceIdFromCollectionId: () => getSpaceIdFromCollectionId,
71
- hasInvitationExpired: () => import_chunk_SG2PL5RH.hasInvitationExpired,
72
- mapFeedIndexesToTimeframe: () => import_chunk_SG2PL5RH.mapFeedIndexesToTimeframe,
73
- mapTimeframeToFeedIndexes: () => import_chunk_SG2PL5RH.mapTimeframeToFeedIndexes,
74
- startAfter: () => import_chunk_SG2PL5RH.startAfter,
75
- valueEncoding: () => import_chunk_SG2PL5RH.valueEncoding
71
+ hasInvitationExpired: () => import_chunk_JXX6LF5U.hasInvitationExpired,
72
+ mapFeedIndexesToTimeframe: () => import_chunk_JXX6LF5U.mapFeedIndexesToTimeframe,
73
+ mapTimeframeToFeedIndexes: () => import_chunk_JXX6LF5U.mapTimeframeToFeedIndexes,
74
+ startAfter: () => import_chunk_JXX6LF5U.startAfter,
75
+ valueEncoding: () => import_chunk_JXX6LF5U.valueEncoding
76
76
  });
77
77
  module.exports = __toCommonJS(node_exports);
78
78
  var import_chunk_HOPOFWAL = require("./chunk-HOPOFWAL.cjs");
79
- var import_chunk_SG2PL5RH = require("./chunk-SG2PL5RH.cjs");
79
+ var import_chunk_JXX6LF5U = require("./chunk-JXX6LF5U.cjs");
80
80
  var import_chunk_Q7SFCCGT = require("./chunk-Q7SFCCGT.cjs");
81
81
  var import_async = require("@dxos/async");
82
82
  var import_stream = require("@dxos/codec-protobuf/stream");
@@ -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();
@@ -635,10 +479,16 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
635
479
  timeout: 1e4
636
480
  });
637
481
  }
482
+ onConnectionAuthScopeChanged(peer) {
483
+ const entry = this._connections.get(peer);
484
+ if (entry) {
485
+ this._onConnectionAuthScopeChanged(entry.connection);
486
+ }
487
+ }
638
488
  async addReplicator(replicator) {
639
489
  (0, import_invariant4.invariant)(this._lifecycleState === import_context4.LifecycleState.OPEN, void 0, {
640
- F: __dxlog_file3,
641
- L: 115,
490
+ F: __dxlog_file2,
491
+ L: 129,
642
492
  S: this,
643
493
  A: [
644
494
  "this._lifecycleState === LifecycleState.OPEN",
@@ -646,8 +496,8 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
646
496
  ]
647
497
  });
648
498
  (0, import_invariant4.invariant)(this.peerId, void 0, {
649
- F: __dxlog_file3,
650
- L: 116,
499
+ F: __dxlog_file2,
500
+ L: 130,
651
501
  S: this,
652
502
  A: [
653
503
  "this.peerId",
@@ -655,8 +505,8 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
655
505
  ]
656
506
  });
657
507
  (0, import_invariant4.invariant)(!this._replicators.has(replicator), void 0, {
658
- F: __dxlog_file3,
659
- L: 117,
508
+ F: __dxlog_file2,
509
+ L: 131,
660
510
  S: this,
661
511
  A: [
662
512
  "!this._replicators.has(replicator)",
@@ -673,14 +523,14 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
673
523
  getContainingSpaceForDocument: this._params.getContainingSpaceForDocument,
674
524
  getContainingSpaceIdForDocument: async (documentId) => {
675
525
  const key = await this._params.getContainingSpaceForDocument(documentId);
676
- return key ? (0, import_chunk_SG2PL5RH.createIdFromSpaceKey)(key) : null;
526
+ return key ? (0, import_chunk_JXX6LF5U.createIdFromSpaceKey)(key) : null;
677
527
  }
678
528
  });
679
529
  }
680
530
  async removeReplicator(replicator) {
681
531
  (0, import_invariant4.invariant)(this._lifecycleState === import_context4.LifecycleState.OPEN, void 0, {
682
- F: __dxlog_file3,
683
- L: 136,
532
+ F: __dxlog_file2,
533
+ L: 150,
684
534
  S: this,
685
535
  A: [
686
536
  "this._lifecycleState === LifecycleState.OPEN",
@@ -688,8 +538,8 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
688
538
  ]
689
539
  });
690
540
  (0, import_invariant4.invariant)(this._replicators.has(replicator), void 0, {
691
- F: __dxlog_file3,
692
- L: 137,
541
+ F: __dxlog_file2,
542
+ L: 151,
693
543
  S: this,
694
544
  A: [
695
545
  "this._replicators.has(replicator)",
@@ -732,20 +582,27 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
732
582
  };
733
583
  this._send(message);
734
584
  }
585
+ // TODO(dmaretskyi): Remove.
586
+ getPeersInterestedInCollection(collectionId) {
587
+ return Array.from(this._connections.values()).map((connection) => {
588
+ return connection.connection.shouldSyncCollection({
589
+ collectionId
590
+ }) ? connection.connection.peerId : null;
591
+ }).filter(import_util3.isNonNullable);
592
+ }
735
593
  _send(message) {
736
594
  const connectionEntry = this._connections.get(message.targetId);
737
595
  if (!connectionEntry) {
738
596
  throw new Error("Connection not found.");
739
597
  }
740
- const writeStart = Date.now();
598
+ const start = Date.now();
741
599
  connectionEntry.writer.write(message).then(() => {
742
- const durationMs = Date.now() - writeStart;
743
- this._params.monitor?.recordMessageSent(message, durationMs);
600
+ this._params.monitor?.recordMessageSent(message, Date.now() - start);
744
601
  }).catch((err) => {
745
602
  if (connectionEntry.isOpen) {
746
603
  import_log5.log.catch(err, void 0, {
747
- F: __dxlog_file3,
748
- L: 197,
604
+ F: __dxlog_file2,
605
+ L: 221,
749
606
  S: this,
750
607
  C: (f, a) => f(...a)
751
608
  });
@@ -753,45 +610,35 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
753
610
  this._params.monitor?.recordMessageSendingFailed(message);
754
611
  });
755
612
  }
756
- // TODO(dmaretskyi): Remove.
757
- getPeersInterestedInCollection(collectionId) {
758
- return Array.from(this._connections.values()).map((connection) => {
759
- return connection.connection.shouldSyncCollection({
760
- collectionId
761
- }) ? connection.connection.peerId : null;
762
- }).filter(import_util3.isNonNullable);
763
- }
764
613
  _onConnectionOpen(connection) {
765
- (0, import_log5.log)("Connection opened", {
614
+ (0, import_log5.log)("connection opened", {
766
615
  peerId: connection.peerId
767
616
  }, {
768
- F: __dxlog_file3,
769
- L: 215,
617
+ F: __dxlog_file2,
618
+ L: 229,
770
619
  S: this,
771
620
  C: (f, a) => f(...a)
772
621
  });
773
622
  (0, import_invariant4.invariant)(!this._connections.has(connection.peerId), void 0, {
774
- F: __dxlog_file3,
775
- L: 216,
623
+ F: __dxlog_file2,
624
+ L: 230,
776
625
  S: this,
777
626
  A: [
778
627
  "!this._connections.has(connection.peerId as PeerId)",
779
628
  ""
780
629
  ]
781
630
  });
782
- const reader = connection.readable.getReader();
783
- const writer = connection.writable.getWriter();
784
631
  const connectionEntry = {
632
+ isOpen: true,
785
633
  connection,
786
- reader,
787
- writer,
788
- isOpen: true
634
+ reader: connection.readable.getReader(),
635
+ writer: connection.writable.getWriter()
789
636
  };
790
637
  this._connections.set(connection.peerId, connectionEntry);
791
638
  queueMicrotask(async () => {
792
639
  try {
793
640
  while (true) {
794
- const { done, value } = await reader.read();
641
+ const { done, value } = await connectionEntry.reader.read();
795
642
  if (done) {
796
643
  break;
797
644
  }
@@ -800,8 +647,8 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
800
647
  } catch (err) {
801
648
  if (connectionEntry.isOpen) {
802
649
  import_log5.log.catch(err, void 0, {
803
- F: __dxlog_file3,
804
- L: 235,
650
+ F: __dxlog_file2,
651
+ L: 254,
805
652
  S: this,
806
653
  C: (f, a) => f(...a)
807
654
  });
@@ -811,8 +658,8 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
811
658
  (0, import_log5.log)("emit peer-candidate", {
812
659
  peerId: connection.peerId
813
660
  }, {
814
- F: __dxlog_file3,
815
- L: 240,
661
+ F: __dxlog_file2,
662
+ L: 259,
816
663
  S: this,
817
664
  C: (f, a) => f(...a)
818
665
  });
@@ -829,77 +676,71 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
829
676
  }
830
677
  this._params.monitor?.recordMessageReceived(message);
831
678
  }
832
- onConnectionAuthScopeChanged(peer) {
833
- const entry = this._connections.get(peer);
834
- if (entry) {
835
- this._onConnectionAuthScopeChanged(entry.connection);
836
- }
837
- }
838
- /**
839
- * Trigger doc-synchronizer shared documents set recalculation. Happens on peer-candidate.
840
- * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
841
- */
842
- _onConnectionAuthScopeChanged(connection) {
843
- (0, import_log5.log)("Connection auth scope changed", {
679
+ _onConnectionClosed(connection) {
680
+ (0, import_log5.log)("connection closed", {
844
681
  peerId: connection.peerId
845
682
  }, {
846
- F: __dxlog_file3,
847
- L: 268,
683
+ F: __dxlog_file2,
684
+ L: 276,
848
685
  S: this,
849
686
  C: (f, a) => f(...a)
850
687
  });
851
688
  const entry = this._connections.get(connection.peerId);
852
689
  (0, import_invariant4.invariant)(entry, void 0, {
853
- F: __dxlog_file3,
854
- L: 270,
690
+ F: __dxlog_file2,
691
+ L: 278,
855
692
  S: this,
856
693
  A: [
857
694
  "entry",
858
695
  ""
859
696
  ]
860
697
  });
698
+ entry.isOpen = false;
861
699
  this.emit("peer-disconnected", {
862
700
  peerId: connection.peerId
863
701
  });
864
- this._emitPeerCandidate(connection);
702
+ this._params.monitor?.recordPeerDisconnected(connection.peerId);
703
+ void entry.reader.cancel().catch((err) => import_log5.log.catch(err, void 0, {
704
+ F: __dxlog_file2,
705
+ L: 284,
706
+ S: this,
707
+ C: (f, a) => f(...a)
708
+ }));
709
+ void entry.writer.abort().catch((err) => import_log5.log.catch(err, void 0, {
710
+ F: __dxlog_file2,
711
+ L: 285,
712
+ S: this,
713
+ C: (f, a) => f(...a)
714
+ }));
715
+ this._connections.delete(connection.peerId);
865
716
  }
866
- _onConnectionClosed(connection) {
867
- (0, import_log5.log)("Connection closed", {
717
+ /**
718
+ * Trigger doc-synchronizer shared documents set recalculation. Happens on peer-candidate.
719
+ * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
720
+ */
721
+ _onConnectionAuthScopeChanged(connection) {
722
+ (0, import_log5.log)("Connection auth scope changed", {
868
723
  peerId: connection.peerId
869
724
  }, {
870
- F: __dxlog_file3,
871
- L: 276,
725
+ F: __dxlog_file2,
726
+ L: 294,
872
727
  S: this,
873
728
  C: (f, a) => f(...a)
874
729
  });
875
730
  const entry = this._connections.get(connection.peerId);
876
731
  (0, import_invariant4.invariant)(entry, void 0, {
877
- F: __dxlog_file3,
878
- L: 278,
732
+ F: __dxlog_file2,
733
+ L: 296,
879
734
  S: this,
880
735
  A: [
881
736
  "entry",
882
737
  ""
883
738
  ]
884
739
  });
885
- entry.isOpen = false;
886
740
  this.emit("peer-disconnected", {
887
741
  peerId: connection.peerId
888
742
  });
889
- this._params.monitor?.recordPeerDisconnected(connection.peerId);
890
- void entry.reader.cancel().catch((err) => import_log5.log.catch(err, void 0, {
891
- F: __dxlog_file3,
892
- L: 284,
893
- S: this,
894
- C: (f, a) => f(...a)
895
- }));
896
- void entry.writer.abort().catch((err) => import_log5.log.catch(err, void 0, {
897
- F: __dxlog_file3,
898
- L: 285,
899
- S: this,
900
- C: (f, a) => f(...a)
901
- }));
902
- this._connections.delete(connection.peerId);
743
+ this._emitPeerCandidate(connection);
903
744
  }
904
745
  _emitPeerCandidate(connection) {
905
746
  this.emit("peer-candidate", {
@@ -1052,7 +893,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1052
893
  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
894
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1054
895
  }
1055
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
896
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
1056
897
  var FIND_PARAMS = {
1057
898
  allowableStates: [
1058
899
  "ready",
@@ -1213,7 +1054,7 @@ var AutomergeHost = class extends import_context2.Resource {
1213
1054
  if (headsToWait.length > 0) {
1214
1055
  await Promise.all(headsToWait.map(async (entry, index) => {
1215
1056
  const handle = await this.loadDoc(import_context2.Context.default(void 0, {
1216
- F: __dxlog_file4,
1057
+ F: __dxlog_file3,
1217
1058
  L: 288
1218
1059
  }), entry.documentId);
1219
1060
  await waitForHeads(handle, entry.heads);
@@ -1223,10 +1064,10 @@ var AutomergeHost = class extends import_context2.Resource {
1223
1064
  }
1224
1065
  async reIndexHeads(documentIds) {
1225
1066
  for (const documentId of documentIds) {
1226
- import_log3.log.info("re-indexing heads for document", {
1067
+ (0, import_log3.log)("re-indexing heads for document", {
1227
1068
  documentId
1228
1069
  }, {
1229
- F: __dxlog_file4,
1070
+ F: __dxlog_file3,
1230
1071
  L: 302,
1231
1072
  S: this,
1232
1073
  C: (f, a) => f(...a)
@@ -1236,7 +1077,7 @@ var AutomergeHost = class extends import_context2.Resource {
1236
1077
  import_log3.log.warn("document is not available locally, skipping", {
1237
1078
  documentId
1238
1079
  }, {
1239
- F: __dxlog_file4,
1080
+ F: __dxlog_file3,
1240
1081
  L: 305,
1241
1082
  S: this,
1242
1083
  C: (f, a) => f(...a)
@@ -1248,8 +1089,8 @@ var AutomergeHost = class extends import_context2.Resource {
1248
1089
  this._headsStore.setHeads(documentId, heads, batch);
1249
1090
  await batch.write();
1250
1091
  }
1251
- import_log3.log.info("done re-indexing heads", void 0, {
1252
- F: __dxlog_file4,
1092
+ (0, import_log3.log)("done re-indexing heads", void 0, {
1093
+ F: __dxlog_file3,
1253
1094
  L: 314,
1254
1095
  S: this,
1255
1096
  C: (f, a) => f(...a)
@@ -1464,13 +1305,13 @@ var AutomergeHost = class extends import_context2.Resource {
1464
1305
  if (toReplicate.length === 0) {
1465
1306
  return;
1466
1307
  }
1467
- import_log3.log.info("replicating documents after collection sync", {
1308
+ (0, import_log3.log)("replicating documents after collection sync", {
1468
1309
  collectionId,
1469
1310
  peerId,
1470
1311
  toReplicate,
1471
1312
  count: toReplicate.length
1472
1313
  }, {
1473
- F: __dxlog_file4,
1314
+ F: __dxlog_file3,
1474
1315
  L: 557,
1475
1316
  S: this,
1476
1317
  C: (f, a) => f(...a)
@@ -1530,7 +1371,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1530
1371
  };
1531
1372
  var decodeCollectionState = (state) => {
1532
1373
  (0, import_invariant3.invariant)(typeof state === "object" && state !== null, "Invalid state", {
1533
- F: __dxlog_file4,
1374
+ F: __dxlog_file3,
1534
1375
  L: 608,
1535
1376
  S: void 0,
1536
1377
  A: [
@@ -1543,7 +1384,7 @@ var decodeCollectionState = (state) => {
1543
1384
  var encodeCollectionState = (state) => {
1544
1385
  return state;
1545
1386
  };
1546
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1387
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1547
1388
  var DEFAULT_FACTORY = (params) => new import_teleport_extension_automerge_replicator.AutomergeReplicator(...params);
1548
1389
  var MeshReplicatorConnection = class extends import_context6.Resource {
1549
1390
  constructor(_params) {
@@ -1558,7 +1399,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1558
1399
  this.writable = new WritableStream({
1559
1400
  write: async (message, controller) => {
1560
1401
  (0, import_invariant6.invariant)(this._isEnabled, "Writing to a disabled connection", {
1561
- F: __dxlog_file5,
1402
+ F: __dxlog_file4,
1562
1403
  L: 51,
1563
1404
  S: this,
1564
1405
  A: [
@@ -1591,7 +1432,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1591
1432
  thisPeerId: this.peerId,
1592
1433
  remotePeerId: remotePeerId.toHex()
1593
1434
  }, {
1594
- F: __dxlog_file5,
1435
+ F: __dxlog_file4,
1595
1436
  L: 80,
1596
1437
  S: this,
1597
1438
  C: (f, a) => f(...a)
@@ -1618,7 +1459,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1618
1459
  }
1619
1460
  get peerId() {
1620
1461
  (0, import_invariant6.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1621
- F: __dxlog_file5,
1462
+ F: __dxlog_file4,
1622
1463
  L: 106,
1623
1464
  S: this,
1624
1465
  A: [
@@ -1643,7 +1484,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1643
1484
  */
1644
1485
  enable() {
1645
1486
  (0, import_invariant6.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1646
- F: __dxlog_file5,
1487
+ F: __dxlog_file4,
1647
1488
  L: 127,
1648
1489
  S: this,
1649
1490
  A: [
@@ -1662,7 +1503,7 @@ var MeshReplicatorConnection = class extends import_context6.Resource {
1662
1503
  };
1663
1504
  var logSendSync = (message) => {
1664
1505
  (0, import_log7.log)("sendSyncMessage", () => {
1665
- const decodedSyncMessage = message.type === "sync" && message.data ? A2.decodeSyncMessage(message.data) : void 0;
1506
+ const decodedSyncMessage = message.type === "sync" && message.data ? A.decodeSyncMessage(message.data) : void 0;
1666
1507
  return {
1667
1508
  sync: decodedSyncMessage && {
1668
1509
  headsLength: decodedSyncMessage.heads.length,
@@ -1674,18 +1515,18 @@ var logSendSync = (message) => {
1674
1515
  to: message.targetId
1675
1516
  };
1676
1517
  }, {
1677
- F: __dxlog_file5,
1518
+ F: __dxlog_file4,
1678
1519
  L: 140,
1679
1520
  S: void 0,
1680
1521
  C: (f, a) => f(...a)
1681
1522
  });
1682
1523
  };
1683
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1524
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1684
1525
  var deriveCollectionIdFromSpaceId = (spaceId, rootDocumentId) => rootDocumentId ? `space:${spaceId}:${rootDocumentId}` : `space:${spaceId}`;
1685
1526
  var getSpaceIdFromCollectionId = (collectionId) => {
1686
1527
  const spaceId = collectionId.split(":")[1];
1687
1528
  (0, import_invariant7.invariant)(import_keys4.SpaceId.isValid(spaceId), void 0, {
1688
- F: __dxlog_file6,
1529
+ F: __dxlog_file5,
1689
1530
  L: 16,
1690
1531
  S: void 0,
1691
1532
  A: [
@@ -1695,7 +1536,7 @@ var getSpaceIdFromCollectionId = (collectionId) => {
1695
1536
  });
1696
1537
  return spaceId;
1697
1538
  };
1698
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1539
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1699
1540
  var MeshEchoReplicator = class {
1700
1541
  constructor() {
1701
1542
  this._connectionsPerPeer = /* @__PURE__ */ new Map();
@@ -1721,7 +1562,7 @@ var MeshEchoReplicator = class {
1721
1562
  }
1722
1563
  createExtension(extensionFactory) {
1723
1564
  (0, import_invariant5.invariant)(this._context, void 0, {
1724
- F: __dxlog_file7,
1565
+ F: __dxlog_file6,
1725
1566
  L: 67,
1726
1567
  S: this,
1727
1568
  A: [
@@ -1736,13 +1577,13 @@ var MeshEchoReplicator = class {
1736
1577
  (0, import_log6.log)("onRemoteConnected", {
1737
1578
  peerId: connection.peerId
1738
1579
  }, {
1739
- F: __dxlog_file7,
1580
+ F: __dxlog_file6,
1740
1581
  L: 73,
1741
1582
  S: this,
1742
1583
  C: (f, a) => f(...a)
1743
1584
  });
1744
1585
  (0, import_invariant5.invariant)(this._context, void 0, {
1745
- F: __dxlog_file7,
1586
+ F: __dxlog_file6,
1746
1587
  L: 74,
1747
1588
  S: this,
1748
1589
  A: [
@@ -1767,7 +1608,7 @@ var MeshEchoReplicator = class {
1767
1608
  (0, import_log6.log)("onRemoteDisconnected", {
1768
1609
  peerId: connection.peerId
1769
1610
  }, {
1770
- F: __dxlog_file7,
1611
+ F: __dxlog_file6,
1771
1612
  L: 88,
1772
1613
  S: this,
1773
1614
  C: (f, a) => f(...a)
@@ -1779,7 +1620,7 @@ var MeshEchoReplicator = class {
1779
1620
  import_log6.log.warn("disconnected connection not found", {
1780
1621
  peerId: connection.peerId
1781
1622
  }, {
1782
- F: __dxlog_file7,
1623
+ F: __dxlog_file6,
1783
1624
  L: 96,
1784
1625
  S: this,
1785
1626
  C: (f, a) => f(...a)
@@ -1801,13 +1642,13 @@ var MeshEchoReplicator = class {
1801
1642
  peerId: connection.peerId,
1802
1643
  documentId: params.documentId
1803
1644
  }, {
1804
- F: __dxlog_file7,
1645
+ F: __dxlog_file6,
1805
1646
  L: 114,
1806
1647
  S: this,
1807
1648
  C: (f, a) => f(...a)
1808
1649
  });
1809
1650
  (0, import_invariant5.invariant)(this._context, void 0, {
1810
- F: __dxlog_file7,
1651
+ F: __dxlog_file6,
1811
1652
  L: 115,
1812
1653
  S: this,
1813
1654
  A: [
@@ -1827,22 +1668,22 @@ var MeshEchoReplicator = class {
1827
1668
  documentId: params.documentId,
1828
1669
  acceptDocument: remoteDocumentExists
1829
1670
  }, {
1830
- F: __dxlog_file7,
1671
+ F: __dxlog_file6,
1831
1672
  L: 123,
1832
1673
  S: this,
1833
1674
  C: (f, a) => f(...a)
1834
1675
  });
1835
1676
  return remoteDocumentExists;
1836
1677
  }
1837
- const spaceId = await (0, import_chunk_SG2PL5RH.createIdFromSpaceKey)(spaceKey);
1678
+ const spaceId = await (0, import_chunk_JXX6LF5U.createIdFromSpaceKey)(spaceKey);
1838
1679
  const authorizedDevices = this._authorizedDevices.get(spaceId);
1839
1680
  if (!connection.remoteDeviceKey) {
1840
1681
  (0, import_log6.log)("device key not found for share policy check", {
1841
1682
  peerId: connection.peerId,
1842
1683
  documentId: params.documentId
1843
1684
  }, {
1844
- F: __dxlog_file7,
1845
- L: 139,
1685
+ F: __dxlog_file6,
1686
+ L: 140,
1846
1687
  S: this,
1847
1688
  C: (f, a) => f(...a)
1848
1689
  });
@@ -1857,16 +1698,16 @@ var MeshEchoReplicator = class {
1857
1698
  spaceKey,
1858
1699
  isAuthorized
1859
1700
  }, {
1860
- F: __dxlog_file7,
1861
- L: 147,
1701
+ F: __dxlog_file6,
1702
+ L: 148,
1862
1703
  S: this,
1863
1704
  C: (f, a) => f(...a)
1864
1705
  });
1865
1706
  return isAuthorized;
1866
1707
  } catch (err) {
1867
1708
  import_log6.log.catch(err, void 0, {
1868
- F: __dxlog_file7,
1869
- L: 157,
1709
+ F: __dxlog_file6,
1710
+ L: 158,
1870
1711
  S: this,
1871
1712
  C: (f, a) => f(...a)
1872
1713
  });
@@ -1881,8 +1722,8 @@ var MeshEchoReplicator = class {
1881
1722
  peerId: connection.peerId,
1882
1723
  collectionId
1883
1724
  }, {
1884
- F: __dxlog_file7,
1885
- L: 167,
1725
+ F: __dxlog_file6,
1726
+ L: 168,
1886
1727
  S: this,
1887
1728
  C: (f, a) => f(...a)
1888
1729
  });
@@ -1900,12 +1741,12 @@ var MeshEchoReplicator = class {
1900
1741
  spaceKey,
1901
1742
  deviceKey
1902
1743
  }, {
1903
- F: __dxlog_file7,
1904
- L: 184,
1744
+ F: __dxlog_file6,
1745
+ L: 185,
1905
1746
  S: this,
1906
1747
  C: (f, a) => f(...a)
1907
1748
  });
1908
- const spaceId = await (0, import_chunk_SG2PL5RH.createIdFromSpaceKey)(spaceKey);
1749
+ const spaceId = await (0, import_chunk_JXX6LF5U.createIdFromSpaceKey)(spaceKey);
1909
1750
  (0, import_util4.defaultMap)(this._authorizedDevices, spaceId, () => new import_util4.ComplexSet(import_keys3.PublicKey.hash)).add(deviceKey);
1910
1751
  for (const connection of this._connections) {
1911
1752
  if (connection.isEnabled && connection.remoteDeviceKey && connection.remoteDeviceKey.equals(deviceKey)) {
@@ -2273,6 +2114,162 @@ var createStorageAverages = () => ({
2273
2114
  var getByteCount = (message) => {
2274
2115
  return message.type.length + message.senderId.length + message.targetId.length + (message.data?.byteLength ?? 0) + (message.documentId?.length ?? 0);
2275
2116
  };
2117
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts";
2118
+ var MAX_UPDATE_FREQ = 10;
2119
+ var DocumentsSynchronizer = class extends import_context.Resource {
2120
+ constructor(_params) {
2121
+ super(), this._params = _params, this._syncStates = /* @__PURE__ */ new Map(), this._pendingUpdates = /* @__PURE__ */ new Set(), this._sendUpdatesJob = void 0;
2122
+ }
2123
+ addDocuments(documentIds, retryCounter = 0) {
2124
+ if (retryCounter > 3) {
2125
+ import_log2.log.warn("Failed to load document, retry limit reached", {
2126
+ documentIds
2127
+ }, {
2128
+ F: __dxlog_file7,
2129
+ L: 52,
2130
+ S: this,
2131
+ C: (f, a) => f(...a)
2132
+ });
2133
+ return;
2134
+ }
2135
+ for (const documentId of documentIds) {
2136
+ this._params.repo.find(documentId).then(async (doc) => {
2137
+ await doc.whenReady();
2138
+ this._startSync(doc);
2139
+ this._pendingUpdates.add(doc.documentId);
2140
+ this._sendUpdatesJob.trigger();
2141
+ }).catch((error) => {
2142
+ import_log2.log.warn("Failed to load document, wraparound", {
2143
+ documentId,
2144
+ error
2145
+ }, {
2146
+ F: __dxlog_file7,
2147
+ L: 66,
2148
+ S: this,
2149
+ C: (f, a) => f(...a)
2150
+ });
2151
+ this.addDocuments([
2152
+ documentId
2153
+ ], retryCounter + 1);
2154
+ });
2155
+ }
2156
+ }
2157
+ removeDocuments(documentIds) {
2158
+ for (const documentId of documentIds) {
2159
+ this._syncStates.get(documentId)?.clearSubscriptions?.();
2160
+ this._syncStates.delete(documentId);
2161
+ this._pendingUpdates.delete(documentId);
2162
+ }
2163
+ }
2164
+ async _open() {
2165
+ this._sendUpdatesJob = new import_async2.UpdateScheduler(this._ctx, this._checkAndSendUpdates.bind(this), {
2166
+ maxFrequency: MAX_UPDATE_FREQ
2167
+ });
2168
+ }
2169
+ async _close() {
2170
+ await this._sendUpdatesJob.join();
2171
+ this._syncStates.clear();
2172
+ }
2173
+ async update(updates) {
2174
+ for (const { documentId, mutation, isNew } of updates) {
2175
+ if (isNew) {
2176
+ const doc = await this._params.repo.find(documentId, FIND_PARAMS);
2177
+ doc.update((doc2) => import_automerge.next.loadIncremental(doc2, mutation));
2178
+ this._startSync(doc);
2179
+ } else {
2180
+ this._writeMutation(documentId, mutation);
2181
+ }
2182
+ }
2183
+ }
2184
+ _startSync(doc) {
2185
+ if (this._syncStates.has(doc.documentId)) {
2186
+ (0, import_log2.log)("Document already being synced", {
2187
+ documentId: doc.documentId
2188
+ }, {
2189
+ F: __dxlog_file7,
2190
+ L: 105,
2191
+ S: this,
2192
+ C: (f, a) => f(...a)
2193
+ });
2194
+ return;
2195
+ }
2196
+ const syncState = {
2197
+ handle: doc
2198
+ };
2199
+ this._subscribeForChanges(syncState);
2200
+ this._syncStates.set(doc.documentId, syncState);
2201
+ }
2202
+ _subscribeForChanges(syncState) {
2203
+ const handler = () => {
2204
+ this._pendingUpdates.add(syncState.handle.documentId);
2205
+ this._sendUpdatesJob.trigger();
2206
+ };
2207
+ syncState.handle.on("heads-changed", handler);
2208
+ syncState.clearSubscriptions = () => syncState.handle.off("heads-changed", handler);
2209
+ }
2210
+ async _checkAndSendUpdates() {
2211
+ const updates = [];
2212
+ const docsWithPendingUpdates = Array.from(this._pendingUpdates);
2213
+ this._pendingUpdates.clear();
2214
+ for (const documentId of docsWithPendingUpdates) {
2215
+ const update = this._getPendingChanges(documentId);
2216
+ if (update) {
2217
+ updates.push({
2218
+ documentId,
2219
+ mutation: update
2220
+ });
2221
+ }
2222
+ }
2223
+ if (updates.length > 0) {
2224
+ this._params.sendUpdates({
2225
+ updates
2226
+ });
2227
+ }
2228
+ }
2229
+ _getPendingChanges(documentId) {
2230
+ const syncState = this._syncStates.get(documentId);
2231
+ (0, import_invariant2.invariant)(syncState, "Sync state for document not found", {
2232
+ F: __dxlog_file7,
2233
+ L: 146,
2234
+ S: this,
2235
+ A: [
2236
+ "syncState",
2237
+ "'Sync state for document not found'"
2238
+ ]
2239
+ });
2240
+ const handle = syncState.handle;
2241
+ if (!handle || !handle.isReady() || !handle.doc()) {
2242
+ return;
2243
+ }
2244
+ const doc = handle.doc();
2245
+ const mutation = syncState.lastSentHead ? import_automerge.next.saveSince(doc, syncState.lastSentHead) : import_automerge.next.save(doc);
2246
+ if (mutation.length === 0) {
2247
+ return;
2248
+ }
2249
+ syncState.lastSentHead = import_automerge.next.getHeads(doc);
2250
+ return mutation;
2251
+ }
2252
+ _writeMutation(documentId, mutation) {
2253
+ const syncState = this._syncStates.get(documentId);
2254
+ (0, import_invariant2.invariant)(syncState, "Sync state for document not found", {
2255
+ F: __dxlog_file7,
2256
+ L: 162,
2257
+ S: this,
2258
+ A: [
2259
+ "syncState",
2260
+ "'Sync state for document not found'"
2261
+ ]
2262
+ });
2263
+ syncState.handle.update((doc) => {
2264
+ const headsBefore = import_automerge.next.getHeads(doc);
2265
+ const newDoc = import_automerge.next.loadIncremental(doc, mutation);
2266
+ if (import_automerge.next.equals(headsBefore, syncState.lastSentHead)) {
2267
+ syncState.lastSentHead = import_automerge.next.getHeads(newDoc);
2268
+ }
2269
+ return newDoc;
2270
+ });
2271
+ }
2272
+ };
2276
2273
  var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
2277
2274
  var DataServiceImpl = class {
2278
2275
  constructor(params) {
@@ -2331,7 +2328,7 @@ var DataServiceImpl = class {
2331
2328
  "'Subscription not found'"
2332
2329
  ]
2333
2330
  });
2334
- synchronizer.update(request.updates);
2331
+ await synchronizer.update(request.updates);
2335
2332
  }
2336
2333
  async flush(request) {
2337
2334
  await this._automergeHost.flush(request);
@@ -2440,7 +2437,7 @@ var createSelectedDocumentsIterator = (automergeHost) => (
2440
2437
  doc = A3.view(doc, heads);
2441
2438
  const end = Date.now();
2442
2439
  if (end - begin > LOG_VIEW_OPERATION_THRESHOLD) {
2443
- import_log8.log.info("Checking out document version is taking too long", {
2440
+ (0, import_log8.log)("Checking out document version is taking too long", {
2444
2441
  duration: end - begin,
2445
2442
  requestedHeads: heads,
2446
2443
  originalHeads: currentHeads
@@ -2967,6 +2964,7 @@ var ExecutionTrace = Object.freeze({
2967
2964
  return go(trace6, 0);
2968
2965
  }
2969
2966
  });
2967
+ var TRACE_QUERY_EXECUTION = false;
2970
2968
  var QueryExecutor = class extends import_context10.Resource {
2971
2969
  constructor(options) {
2972
2970
  super();
@@ -3006,7 +3004,7 @@ var QueryExecutor = class extends import_context10.Resource {
3006
3004
  async execQuery() {
3007
3005
  (0, import_invariant10.invariant)(this._lifecycleState === import_context10.LifecycleState.OPEN, void 0, {
3008
3006
  F: __dxlog_file11,
3009
- L: 171,
3007
+ L: 173,
3010
3008
  S: this,
3011
3009
  A: [
3012
3010
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3022,6 +3020,9 @@ var QueryExecutor = class extends import_context10.Resource {
3022
3020
  });
3023
3021
  this._trace = trace6;
3024
3022
  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);
3023
+ if (TRACE_QUERY_EXECUTION) {
3024
+ console.log(ExecutionTrace.format(trace6));
3025
+ }
3025
3026
  return {
3026
3027
  changed
3027
3028
  };
@@ -3385,7 +3386,7 @@ var QueryExecutor = class extends import_context10.Resource {
3385
3386
  return {
3386
3387
  objectId,
3387
3388
  documentId,
3388
- spaceId: await (0, import_chunk_SG2PL5RH.createIdFromSpaceKey)(import_keys5.PublicKey.from(spaceKey)),
3389
+ spaceId: await (0, import_chunk_JXX6LF5U.createIdFromSpaceKey)(import_keys5.PublicKey.from(spaceKey)),
3389
3390
  doc: object
3390
3391
  };
3391
3392
  }
@@ -3548,7 +3549,7 @@ var QueryServiceImpl = class extends import_context9.Resource {
3548
3549
  * Re-index all loaded documents.
3549
3550
  */
3550
3551
  async reindex() {
3551
- import_log9.log.info("Reindexing all documents...", void 0, {
3552
+ (0, import_log9.log)("Reindexing all documents...", void 0, {
3552
3553
  F: __dxlog_file12,
3553
3554
  L: 141,
3554
3555
  S: this,
@@ -3561,7 +3562,7 @@ var QueryServiceImpl = class extends import_context9.Resource {
3561
3562
  ids.set(id, heads);
3562
3563
  }
3563
3564
  if (ids.size % 100 === 0) {
3564
- import_log9.log.info("Collected documents...", {
3565
+ (0, import_log9.log)("Collected documents...", {
3565
3566
  count: ids.size
3566
3567
  }, {
3567
3568
  F: __dxlog_file12,
@@ -3571,7 +3572,7 @@ var QueryServiceImpl = class extends import_context9.Resource {
3571
3572
  });
3572
3573
  }
3573
3574
  }
3574
- import_log9.log.info("Marking all documents as dirty...", {
3575
+ (0, import_log9.log)("Marking all documents as dirty...", {
3575
3576
  count: ids.size
3576
3577
  }, {
3577
3578
  F: __dxlog_file12,
@@ -4193,7 +4194,7 @@ var EchoEdgeReplicator = class {
4193
4194
  this._sharePolicyEnabled = !disableSharePolicy;
4194
4195
  }
4195
4196
  async connect(context) {
4196
- import_log12.log.info("connecting...", {
4197
+ (0, import_log12.log)("connecting...", {
4197
4198
  peerId: context.peerId,
4198
4199
  connectedSpaces: this._connectedSpaces.size
4199
4200
  }, {
@@ -4205,7 +4206,7 @@ var EchoEdgeReplicator = class {
4205
4206
  this._context = context;
4206
4207
  this._ctx = import_context12.Context.default(void 0, {
4207
4208
  F: __dxlog_file18,
4208
- L: 64
4209
+ L: 63
4209
4210
  });
4210
4211
  this._ctx.onDispose(this._edgeConnection.onReconnected(() => {
4211
4212
  this._ctx && (0, import_async8.scheduleMicroTask)(this._ctx, () => this._handleReconnect());
@@ -4304,7 +4305,7 @@ var EchoEdgeReplicator = class {
4304
4305
  async _openConnection(spaceId, reconnects = 0) {
4305
4306
  (0, import_invariant14.invariant)(this._context, void 0, {
4306
4307
  F: __dxlog_file18,
4307
- L: 125,
4308
+ L: 124,
4308
4309
  S: this,
4309
4310
  A: [
4310
4311
  "this._context",
@@ -4313,7 +4314,7 @@ var EchoEdgeReplicator = class {
4313
4314
  });
4314
4315
  (0, import_invariant14.invariant)(!this._connections.has(spaceId), void 0, {
4315
4316
  F: __dxlog_file18,
4316
- L: 126,
4317
+ L: 125,
4317
4318
  S: this,
4318
4319
  A: [
4319
4320
  "!this._connections.has(spaceId)",
@@ -4337,13 +4338,13 @@ var EchoEdgeReplicator = class {
4337
4338
  return;
4338
4339
  }
4339
4340
  const restartDelay = Math.min(MAX_RESTART_DELAY, INITIAL_RESTART_DELAY * reconnects) + Math.random() * RESTART_DELAY_JITTER;
4340
- import_log12.log.info("connection restart scheduled", {
4341
+ (0, import_log12.log)("connection restart scheduled", {
4341
4342
  spaceId,
4342
4343
  reconnects,
4343
4344
  restartDelay
4344
4345
  }, {
4345
4346
  F: __dxlog_file18,
4346
- L: 149,
4347
+ L: 148,
4347
4348
  S: this,
4348
4349
  C: (f, a) => f(...a)
4349
4350
  });
@@ -4413,7 +4414,7 @@ var EdgeReplicatorConnection = class extends import_context12.Resource {
4413
4414
  async _open(ctx) {
4414
4415
  (0, import_log12.log)("opening...", void 0, {
4415
4416
  F: __dxlog_file18,
4416
- L: 252,
4417
+ L: 251,
4417
4418
  S: this,
4418
4419
  C: (f, a) => f(...a)
4419
4420
  });
@@ -4426,7 +4427,7 @@ var EdgeReplicatorConnection = class extends import_context12.Resource {
4426
4427
  async _close() {
4427
4428
  (0, import_log12.log)("closing...", void 0, {
4428
4429
  F: __dxlog_file18,
4429
- L: 267,
4430
+ L: 266,
4430
4431
  S: this,
4431
4432
  C: (f, a) => f(...a)
4432
4433
  });
@@ -4437,7 +4438,7 @@ var EdgeReplicatorConnection = class extends import_context12.Resource {
4437
4438
  get peerId() {
4438
4439
  (0, import_invariant14.invariant)(this._remotePeerId, "Not connected", {
4439
4440
  F: __dxlog_file18,
4440
- L: 276,
4441
+ L: 275,
4441
4442
  S: this,
4442
4443
  A: [
4443
4444
  "this._remotePeerId",
@@ -4462,7 +4463,7 @@ var EdgeReplicatorConnection = class extends import_context12.Resource {
4462
4463
  remoteId: this._remotePeerId
4463
4464
  }, {
4464
4465
  F: __dxlog_file18,
4465
- L: 291,
4466
+ L: 290,
4466
4467
  S: this,
4467
4468
  C: (f, a) => f(...a)
4468
4469
  });
@@ -4488,7 +4489,7 @@ var EdgeReplicatorConnection = class extends import_context12.Resource {
4488
4489
  remoteId: this._remotePeerId
4489
4490
  }, {
4490
4491
  F: __dxlog_file18,
4491
- L: 320,
4492
+ L: 319,
4492
4493
  S: this,
4493
4494
  C: (f, a) => f(...a)
4494
4495
  });
@@ -4511,7 +4512,7 @@ var EdgeReplicatorConnection = class extends import_context12.Resource {
4511
4512
  remoteId: this._remotePeerId
4512
4513
  }, {
4513
4514
  F: __dxlog_file18,
4514
- L: 349,
4515
+ L: 348,
4515
4516
  S: this,
4516
4517
  C: (f, a) => f(...a)
4517
4518
  });