@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.
@@ -36,177 +36,21 @@ import { SpaceId as SpaceId2 } from "@dxos/keys";
36
36
  import { log as log7 } from "@dxos/log";
37
37
 
38
38
  // packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts
39
- import { next as A } from "@automerge/automerge";
39
+ import { next as A2 } from "@automerge/automerge";
40
40
  import { UpdateScheduler } from "@dxos/async";
41
- import { Resource } from "@dxos/context";
42
- import { invariant } from "@dxos/invariant";
43
- import { log } from "@dxos/log";
44
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts";
45
- var MAX_UPDATE_FREQ = 10;
46
- var DocumentsSynchronizer = class extends Resource {
47
- constructor(_params) {
48
- super(), this._params = _params, this._syncStates = /* @__PURE__ */ new Map(), this._pendingUpdates = /* @__PURE__ */ new Set(), this._sendUpdatesJob = void 0;
49
- }
50
- addDocuments(documentIds, retryCounter = 0) {
51
- if (retryCounter > 3) {
52
- log.warn("Failed to load document, retry limit reached", {
53
- documentIds
54
- }, {
55
- F: __dxlog_file,
56
- L: 50,
57
- S: this,
58
- C: (f, a) => f(...a)
59
- });
60
- return;
61
- }
62
- for (const documentId of documentIds) {
63
- this._params.repo.find(documentId).then(async (doc) => {
64
- await doc.whenReady();
65
- this._startSync(doc);
66
- this._pendingUpdates.add(doc.documentId);
67
- this._sendUpdatesJob.trigger();
68
- }).catch((error) => {
69
- log.warn("Failed to load document, wraparound", {
70
- documentId,
71
- error
72
- }, {
73
- F: __dxlog_file,
74
- L: 64,
75
- S: this,
76
- C: (f, a) => f(...a)
77
- });
78
- this.addDocuments([
79
- documentId
80
- ], retryCounter + 1);
81
- });
82
- }
83
- }
84
- removeDocuments(documentIds) {
85
- for (const documentId of documentIds) {
86
- this._syncStates.get(documentId)?.clearSubscriptions?.();
87
- this._syncStates.delete(documentId);
88
- this._pendingUpdates.delete(documentId);
89
- }
90
- }
91
- async _open() {
92
- this._sendUpdatesJob = new UpdateScheduler(this._ctx, this._checkAndSendUpdates.bind(this), {
93
- maxFrequency: MAX_UPDATE_FREQ
94
- });
95
- }
96
- async _close() {
97
- await this._sendUpdatesJob.join();
98
- this._syncStates.clear();
99
- }
100
- update(updates) {
101
- for (const { documentId, mutation, isNew } of updates) {
102
- if (isNew) {
103
- const { handle: doc } = this._params.repo.findWithProgress(documentId);
104
- doc.update((doc2) => A.loadIncremental(doc2, mutation));
105
- this._startSync(doc);
106
- } else {
107
- this._writeMutation(documentId, mutation);
108
- }
109
- }
110
- }
111
- _startSync(doc) {
112
- if (this._syncStates.has(doc.documentId)) {
113
- log.info("Document already being synced", {
114
- documentId: doc.documentId
115
- }, {
116
- F: __dxlog_file,
117
- L: 103,
118
- S: this,
119
- C: (f, a) => f(...a)
120
- });
121
- return;
122
- }
123
- const syncState = {
124
- handle: doc
125
- };
126
- this._subscribeForChanges(syncState);
127
- this._syncStates.set(doc.documentId, syncState);
128
- }
129
- _subscribeForChanges(syncState) {
130
- const handler = () => {
131
- this._pendingUpdates.add(syncState.handle.documentId);
132
- this._sendUpdatesJob.trigger();
133
- };
134
- syncState.handle.on("heads-changed", handler);
135
- syncState.clearSubscriptions = () => syncState.handle.off("heads-changed", handler);
136
- }
137
- async _checkAndSendUpdates() {
138
- const updates = [];
139
- const docsWithPendingUpdates = Array.from(this._pendingUpdates);
140
- this._pendingUpdates.clear();
141
- for (const documentId of docsWithPendingUpdates) {
142
- const update = this._getPendingChanges(documentId);
143
- if (update) {
144
- updates.push({
145
- documentId,
146
- mutation: update
147
- });
148
- }
149
- }
150
- if (updates.length > 0) {
151
- this._params.sendUpdates({
152
- updates
153
- });
154
- }
155
- }
156
- _getPendingChanges(documentId) {
157
- const syncState = this._syncStates.get(documentId);
158
- invariant(syncState, "Sync state for document not found", {
159
- F: __dxlog_file,
160
- L: 144,
161
- S: this,
162
- A: [
163
- "syncState",
164
- "'Sync state for document not found'"
165
- ]
166
- });
167
- const handle = syncState.handle;
168
- if (!handle || !handle.isReady() || !handle.doc()) {
169
- return;
170
- }
171
- const doc = handle.doc();
172
- const mutation = syncState.lastSentHead ? A.saveSince(doc, syncState.lastSentHead) : A.save(doc);
173
- if (mutation.length === 0) {
174
- return;
175
- }
176
- syncState.lastSentHead = A.getHeads(doc);
177
- return mutation;
178
- }
179
- _writeMutation(documentId, mutation) {
180
- const syncState = this._syncStates.get(documentId);
181
- invariant(syncState, "Sync state for document not found", {
182
- F: __dxlog_file,
183
- L: 160,
184
- S: this,
185
- A: [
186
- "syncState",
187
- "'Sync state for document not found'"
188
- ]
189
- });
190
- syncState.handle.update((doc) => {
191
- const headsBefore = A.getHeads(doc);
192
- const newDoc = A.loadIncremental(doc, mutation);
193
- if (A.equals(headsBefore, syncState.lastSentHead)) {
194
- syncState.lastSentHead = A.getHeads(newDoc);
195
- }
196
- return newDoc;
197
- });
198
- }
199
- };
41
+ import { Resource as Resource5 } from "@dxos/context";
42
+ import { invariant as invariant6 } from "@dxos/invariant";
43
+ import { log as log6 } from "@dxos/log";
200
44
 
201
45
  // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
202
46
  import { getBackend, getHeads, isAutomerge, equals as headsEquals, save } from "@automerge/automerge";
203
47
  import { Repo, interpretAsDocumentId } from "@automerge/automerge-repo";
204
48
  import { Event as Event2, asyncTimeout } from "@dxos/async";
205
- import { Context, Resource as Resource4, cancelWithContext } from "@dxos/context";
49
+ import { Context, Resource as Resource3, cancelWithContext } from "@dxos/context";
206
50
  import { DatabaseDirectory } from "@dxos/echo-protocol";
207
- import { invariant as invariant3 } from "@dxos/invariant";
51
+ import { invariant as invariant2 } from "@dxos/invariant";
208
52
  import { PublicKey } from "@dxos/keys";
209
- import { log as log4 } from "@dxos/log";
53
+ import { log as log3 } from "@dxos/log";
210
54
  import { objectPointerCodec } from "@dxos/protocols";
211
55
  import { trace as trace2 } from "@dxos/tracing";
212
56
  import { bufferToArray } from "@dxos/util";
@@ -214,8 +58,8 @@ import { bufferToArray } from "@dxos/util";
214
58
  // packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts
215
59
  import { next as am } from "@automerge/automerge";
216
60
  import { asyncReturn, Event, scheduleTask, scheduleTaskInterval } from "@dxos/async";
217
- import { Resource as Resource2 } from "@dxos/context";
218
- import { log as log2 } from "@dxos/log";
61
+ import { Resource } from "@dxos/context";
62
+ import { log } from "@dxos/log";
219
63
  import { trace } from "@dxos/tracing";
220
64
  import { defaultMap } from "@dxos/util";
221
65
  function _ts_decorate(decorators, target, key, desc) {
@@ -224,10 +68,10 @@ function _ts_decorate(decorators, target, key, desc) {
224
68
  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;
225
69
  return c > 3 && r && Object.defineProperty(target, key, r), r;
226
70
  }
227
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts";
71
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/collection-synchronizer.ts";
228
72
  var MIN_QUERY_INTERVAL = 5e3;
229
73
  var POLL_INTERVAL = 3e4;
230
- var CollectionSynchronizer = class extends Resource2 {
74
+ var CollectionSynchronizer = class extends Resource {
231
75
  constructor(params) {
232
76
  super();
233
77
  /**
@@ -261,11 +105,11 @@ var CollectionSynchronizer = class extends Resource2 {
261
105
  }
262
106
  setLocalCollectionState(collectionId, state) {
263
107
  this._activeCollections.add(collectionId);
264
- log2("setLocalCollectionState", {
108
+ log("setLocalCollectionState", {
265
109
  collectionId,
266
110
  state
267
111
  }, {
268
- F: __dxlog_file2,
112
+ F: __dxlog_file,
269
113
  L: 76,
270
114
  S: this,
271
115
  C: (f, a) => f(...a)
@@ -281,10 +125,10 @@ var CollectionSynchronizer = class extends Resource2 {
281
125
  clearLocalCollectionState(collectionId) {
282
126
  this._activeCollections.delete(collectionId);
283
127
  this._perCollectionStates.delete(collectionId);
284
- log2("clearLocalCollectionState", {
128
+ log("clearLocalCollectionState", {
285
129
  collectionId
286
130
  }, {
287
- F: __dxlog_file2,
131
+ F: __dxlog_file,
288
132
  L: 90,
289
133
  S: this,
290
134
  C: (f, a) => f(...a)
@@ -362,12 +206,12 @@ var CollectionSynchronizer = class extends Resource2 {
362
206
  * Callback when a peer sends the state of a collection.
363
207
  */
364
208
  onRemoteStateReceived(collectionId, peerId, state) {
365
- log2("onRemoteStateReceived", {
209
+ log("onRemoteStateReceived", {
366
210
  collectionId,
367
211
  peerId,
368
212
  state
369
213
  }, {
370
- F: __dxlog_file2,
214
+ F: __dxlog_file,
371
215
  L: 171,
372
216
  S: this,
373
217
  C: (f, a) => f(...a)
@@ -467,8 +311,8 @@ var getSpanName = (peerId) => {
467
311
  import { NetworkAdapter } from "@automerge/automerge-repo";
468
312
  import { synchronized, Trigger } from "@dxos/async";
469
313
  import { LifecycleState } from "@dxos/context";
470
- import { invariant as invariant2 } from "@dxos/invariant";
471
- import { log as log3 } from "@dxos/log";
314
+ import { invariant } from "@dxos/invariant";
315
+ import { log as log2 } from "@dxos/log";
472
316
  import { isNonNullable } from "@dxos/util";
473
317
 
474
318
  // packages/core/echo/echo-pipeline/src/automerge/network-protocol.ts
@@ -483,7 +327,7 @@ function _ts_decorate2(decorators, target, key, desc) {
483
327
  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;
484
328
  return c > 3 && r && Object.defineProperty(target, key, r), r;
485
329
  }
486
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
330
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
487
331
  var EchoNetworkAdapter = class extends NetworkAdapter {
488
332
  constructor(_params) {
489
333
  super(), this._params = _params, this._replicators = /* @__PURE__ */ new Set(), this._connections = /* @__PURE__ */ new Map(), this._lifecycleState = LifecycleState.CLOSED, this._connected = new Trigger(), this._ready = new Trigger();
@@ -528,8 +372,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
528
372
  });
529
373
  }
530
374
  async addReplicator(replicator) {
531
- invariant2(this._lifecycleState === LifecycleState.OPEN, void 0, {
532
- F: __dxlog_file3,
375
+ invariant(this._lifecycleState === LifecycleState.OPEN, void 0, {
376
+ F: __dxlog_file2,
533
377
  L: 115,
534
378
  S: this,
535
379
  A: [
@@ -537,8 +381,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
537
381
  ""
538
382
  ]
539
383
  });
540
- invariant2(this.peerId, void 0, {
541
- F: __dxlog_file3,
384
+ invariant(this.peerId, void 0, {
385
+ F: __dxlog_file2,
542
386
  L: 116,
543
387
  S: this,
544
388
  A: [
@@ -546,8 +390,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
546
390
  ""
547
391
  ]
548
392
  });
549
- invariant2(!this._replicators.has(replicator), void 0, {
550
- F: __dxlog_file3,
393
+ invariant(!this._replicators.has(replicator), void 0, {
394
+ F: __dxlog_file2,
551
395
  L: 117,
552
396
  S: this,
553
397
  A: [
@@ -570,8 +414,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
570
414
  });
571
415
  }
572
416
  async removeReplicator(replicator) {
573
- invariant2(this._lifecycleState === LifecycleState.OPEN, void 0, {
574
- F: __dxlog_file3,
417
+ invariant(this._lifecycleState === LifecycleState.OPEN, void 0, {
418
+ F: __dxlog_file2,
575
419
  L: 136,
576
420
  S: this,
577
421
  A: [
@@ -579,8 +423,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
579
423
  ""
580
424
  ]
581
425
  });
582
- invariant2(this._replicators.has(replicator), void 0, {
583
- F: __dxlog_file3,
426
+ invariant(this._replicators.has(replicator), void 0, {
427
+ F: __dxlog_file2,
584
428
  L: 137,
585
429
  S: this,
586
430
  A: [
@@ -635,8 +479,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
635
479
  this._params.monitor?.recordMessageSent(message, durationMs);
636
480
  }).catch((err) => {
637
481
  if (connectionEntry.isOpen) {
638
- log3.catch(err, void 0, {
639
- F: __dxlog_file3,
482
+ log2.catch(err, void 0, {
483
+ F: __dxlog_file2,
640
484
  L: 197,
641
485
  S: this,
642
486
  C: (f, a) => f(...a)
@@ -654,16 +498,16 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
654
498
  }).filter(isNonNullable);
655
499
  }
656
500
  _onConnectionOpen(connection) {
657
- log3("Connection opened", {
501
+ log2("Connection opened", {
658
502
  peerId: connection.peerId
659
503
  }, {
660
- F: __dxlog_file3,
504
+ F: __dxlog_file2,
661
505
  L: 215,
662
506
  S: this,
663
507
  C: (f, a) => f(...a)
664
508
  });
665
- invariant2(!this._connections.has(connection.peerId), void 0, {
666
- F: __dxlog_file3,
509
+ invariant(!this._connections.has(connection.peerId), void 0, {
510
+ F: __dxlog_file2,
667
511
  L: 216,
668
512
  S: this,
669
513
  A: [
@@ -691,8 +535,8 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
691
535
  }
692
536
  } catch (err) {
693
537
  if (connectionEntry.isOpen) {
694
- log3.catch(err, void 0, {
695
- F: __dxlog_file3,
538
+ log2.catch(err, void 0, {
539
+ F: __dxlog_file2,
696
540
  L: 235,
697
541
  S: this,
698
542
  C: (f, a) => f(...a)
@@ -700,10 +544,10 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
700
544
  }
701
545
  }
702
546
  });
703
- log3("emit peer-candidate", {
547
+ log2("emit peer-candidate", {
704
548
  peerId: connection.peerId
705
549
  }, {
706
- F: __dxlog_file3,
550
+ F: __dxlog_file2,
707
551
  L: 240,
708
552
  S: this,
709
553
  C: (f, a) => f(...a)
@@ -732,17 +576,17 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
732
576
  * TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
733
577
  */
734
578
  _onConnectionAuthScopeChanged(connection) {
735
- log3("Connection auth scope changed", {
579
+ log2("Connection auth scope changed", {
736
580
  peerId: connection.peerId
737
581
  }, {
738
- F: __dxlog_file3,
582
+ F: __dxlog_file2,
739
583
  L: 268,
740
584
  S: this,
741
585
  C: (f, a) => f(...a)
742
586
  });
743
587
  const entry = this._connections.get(connection.peerId);
744
- invariant2(entry, void 0, {
745
- F: __dxlog_file3,
588
+ invariant(entry, void 0, {
589
+ F: __dxlog_file2,
746
590
  L: 270,
747
591
  S: this,
748
592
  A: [
@@ -756,17 +600,17 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
756
600
  this._emitPeerCandidate(connection);
757
601
  }
758
602
  _onConnectionClosed(connection) {
759
- log3("Connection closed", {
603
+ log2("Connection closed", {
760
604
  peerId: connection.peerId
761
605
  }, {
762
- F: __dxlog_file3,
606
+ F: __dxlog_file2,
763
607
  L: 276,
764
608
  S: this,
765
609
  C: (f, a) => f(...a)
766
610
  });
767
611
  const entry = this._connections.get(connection.peerId);
768
- invariant2(entry, void 0, {
769
- F: __dxlog_file3,
612
+ invariant(entry, void 0, {
613
+ F: __dxlog_file2,
770
614
  L: 278,
771
615
  S: this,
772
616
  A: [
@@ -779,14 +623,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
779
623
  peerId: connection.peerId
780
624
  });
781
625
  this._params.monitor?.recordPeerDisconnected(connection.peerId);
782
- void entry.reader.cancel().catch((err) => log3.catch(err, void 0, {
783
- F: __dxlog_file3,
626
+ void entry.reader.cancel().catch((err) => log2.catch(err, void 0, {
627
+ F: __dxlog_file2,
784
628
  L: 284,
785
629
  S: this,
786
630
  C: (f, a) => f(...a)
787
631
  }));
788
- void entry.writer.abort().catch((err) => log3.catch(err, void 0, {
789
- F: __dxlog_file3,
632
+ void entry.writer.abort().catch((err) => log2.catch(err, void 0, {
633
+ F: __dxlog_file2,
790
634
  L: 285,
791
635
  S: this,
792
636
  C: (f, a) => f(...a)
@@ -841,8 +685,8 @@ var HeadsStore = class {
841
685
  };
842
686
 
843
687
  // packages/core/echo/echo-pipeline/src/automerge/leveldb-storage-adapter.ts
844
- import { LifecycleState as LifecycleState2, Resource as Resource3 } from "@dxos/context";
845
- var LevelDBStorageAdapter = class extends Resource3 {
688
+ import { LifecycleState as LifecycleState2, Resource as Resource2 } from "@dxos/context";
689
+ var LevelDBStorageAdapter = class extends Resource2 {
846
690
  constructor(_params) {
847
691
  super(), this._params = _params;
848
692
  }
@@ -952,14 +796,14 @@ function _ts_decorate3(decorators, target, key, desc) {
952
796
  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;
953
797
  return c > 3 && r && Object.defineProperty(target, key, r), r;
954
798
  }
955
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
799
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
956
800
  var FIND_PARAMS = {
957
801
  allowableStates: [
958
802
  "ready",
959
803
  "requesting"
960
804
  ]
961
805
  };
962
- var AutomergeHost = class extends Resource4 {
806
+ var AutomergeHost = class extends Resource3 {
963
807
  constructor({ db, indexMetadataStore, dataMonitor, peerIdProvider, getSpaceKeyByRootDocumentId }) {
964
808
  super();
965
809
  this._collectionSynchronizer = new CollectionSynchronizer({
@@ -1113,7 +957,7 @@ var AutomergeHost = class extends Resource4 {
1113
957
  if (headsToWait.length > 0) {
1114
958
  await Promise.all(headsToWait.map(async (entry, index) => {
1115
959
  const handle = await this.loadDoc(Context.default(void 0, {
1116
- F: __dxlog_file4,
960
+ F: __dxlog_file3,
1117
961
  L: 288
1118
962
  }), entry.documentId);
1119
963
  await waitForHeads(handle, entry.heads);
@@ -1123,20 +967,20 @@ var AutomergeHost = class extends Resource4 {
1123
967
  }
1124
968
  async reIndexHeads(documentIds) {
1125
969
  for (const documentId of documentIds) {
1126
- log4.info("re-indexing heads for document", {
970
+ log3.info("re-indexing heads for document", {
1127
971
  documentId
1128
972
  }, {
1129
- F: __dxlog_file4,
973
+ F: __dxlog_file3,
1130
974
  L: 302,
1131
975
  S: this,
1132
976
  C: (f, a) => f(...a)
1133
977
  });
1134
978
  const handle = await this._repo.find(documentId, FIND_PARAMS);
1135
979
  if (!handle.isReady()) {
1136
- log4.warn("document is not available locally, skipping", {
980
+ log3.warn("document is not available locally, skipping", {
1137
981
  documentId
1138
982
  }, {
1139
- F: __dxlog_file4,
983
+ F: __dxlog_file3,
1140
984
  L: 305,
1141
985
  S: this,
1142
986
  C: (f, a) => f(...a)
@@ -1148,8 +992,8 @@ var AutomergeHost = class extends Resource4 {
1148
992
  this._headsStore.setHeads(documentId, heads, batch);
1149
993
  await batch.write();
1150
994
  }
1151
- log4.info("done re-indexing heads", void 0, {
1152
- F: __dxlog_file4,
995
+ log3.info("done re-indexing heads", void 0, {
996
+ F: __dxlog_file3,
1153
997
  L: 314,
1154
998
  S: this,
1155
999
  C: (f, a) => f(...a)
@@ -1364,13 +1208,13 @@ var AutomergeHost = class extends Resource4 {
1364
1208
  if (toReplicate.length === 0) {
1365
1209
  return;
1366
1210
  }
1367
- log4.info("replicating documents after collection sync", {
1211
+ log3.info("replicating documents after collection sync", {
1368
1212
  collectionId,
1369
1213
  peerId,
1370
1214
  toReplicate,
1371
1215
  count: toReplicate.length
1372
1216
  }, {
1373
- F: __dxlog_file4,
1217
+ F: __dxlog_file3,
1374
1218
  L: 557,
1375
1219
  S: this,
1376
1220
  C: (f, a) => f(...a)
@@ -1429,8 +1273,8 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1429
1273
  return !!getBackend(doc).getChangeByHash(changeHash);
1430
1274
  };
1431
1275
  var decodeCollectionState = (state) => {
1432
- invariant3(typeof state === "object" && state !== null, "Invalid state", {
1433
- F: __dxlog_file4,
1276
+ invariant2(typeof state === "object" && state !== null, "Invalid state", {
1277
+ F: __dxlog_file3,
1434
1278
  L: 608,
1435
1279
  S: void 0,
1436
1280
  A: [
@@ -1445,21 +1289,21 @@ var encodeCollectionState = (state) => {
1445
1289
  };
1446
1290
 
1447
1291
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1448
- import { invariant as invariant6 } from "@dxos/invariant";
1292
+ import { invariant as invariant5 } from "@dxos/invariant";
1449
1293
  import { PublicKey as PublicKey2 } from "@dxos/keys";
1450
- import { log as log6 } from "@dxos/log";
1294
+ import { log as log5 } from "@dxos/log";
1451
1295
  import { ComplexSet, defaultMap as defaultMap2 } from "@dxos/util";
1452
1296
 
1453
1297
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts
1454
- import * as A2 from "@automerge/automerge";
1298
+ import * as A from "@automerge/automerge";
1455
1299
  import { cbor } from "@automerge/automerge-repo";
1456
- import { Resource as Resource5 } from "@dxos/context";
1457
- import { invariant as invariant4 } from "@dxos/invariant";
1458
- import { log as log5 } from "@dxos/log";
1300
+ import { Resource as Resource4 } from "@dxos/context";
1301
+ import { invariant as invariant3 } from "@dxos/invariant";
1302
+ import { log as log4 } from "@dxos/log";
1459
1303
  import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
1460
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1304
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1461
1305
  var DEFAULT_FACTORY = (params) => new AutomergeReplicator(...params);
1462
- var MeshReplicatorConnection = class extends Resource5 {
1306
+ var MeshReplicatorConnection = class extends Resource4 {
1463
1307
  constructor(_params) {
1464
1308
  super(), this._params = _params, this.remoteDeviceKey = null, this._remotePeerId = null, this._isEnabled = false;
1465
1309
  let readableStreamController;
@@ -1471,8 +1315,8 @@ var MeshReplicatorConnection = class extends Resource5 {
1471
1315
  });
1472
1316
  this.writable = new WritableStream({
1473
1317
  write: async (message, controller) => {
1474
- invariant4(this._isEnabled, "Writing to a disabled connection", {
1475
- F: __dxlog_file5,
1318
+ invariant3(this._isEnabled, "Writing to a disabled connection", {
1319
+ F: __dxlog_file4,
1476
1320
  L: 51,
1477
1321
  S: this,
1478
1322
  A: [
@@ -1500,12 +1344,12 @@ var MeshReplicatorConnection = class extends Resource5 {
1500
1344
  onStartReplication: async (info, remotePeerId) => {
1501
1345
  this.remoteDeviceKey = remotePeerId;
1502
1346
  this._remotePeerId = info.id;
1503
- log5("onStartReplication", {
1347
+ log4("onStartReplication", {
1504
1348
  id: info.id,
1505
1349
  thisPeerId: this.peerId,
1506
1350
  remotePeerId: remotePeerId.toHex()
1507
1351
  }, {
1508
- F: __dxlog_file5,
1352
+ F: __dxlog_file4,
1509
1353
  L: 80,
1510
1354
  S: this,
1511
1355
  C: (f, a) => f(...a)
@@ -1531,8 +1375,8 @@ var MeshReplicatorConnection = class extends Resource5 {
1531
1375
  }
1532
1376
  }
1533
1377
  get peerId() {
1534
- invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1535
- F: __dxlog_file5,
1378
+ invariant3(this._remotePeerId != null, "Remote peer has not connected yet.", {
1379
+ F: __dxlog_file4,
1536
1380
  L: 106,
1537
1381
  S: this,
1538
1382
  A: [
@@ -1556,8 +1400,8 @@ var MeshReplicatorConnection = class extends Resource5 {
1556
1400
  * Call after the remote peer has connected.
1557
1401
  */
1558
1402
  enable() {
1559
- invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1560
- F: __dxlog_file5,
1403
+ invariant3(this._remotePeerId != null, "Remote peer has not connected yet.", {
1404
+ F: __dxlog_file4,
1561
1405
  L: 127,
1562
1406
  S: this,
1563
1407
  A: [
@@ -1575,8 +1419,8 @@ var MeshReplicatorConnection = class extends Resource5 {
1575
1419
  }
1576
1420
  };
1577
1421
  var logSendSync = (message) => {
1578
- log5("sendSyncMessage", () => {
1579
- const decodedSyncMessage = message.type === "sync" && message.data ? A2.decodeSyncMessage(message.data) : void 0;
1422
+ log4("sendSyncMessage", () => {
1423
+ const decodedSyncMessage = message.type === "sync" && message.data ? A.decodeSyncMessage(message.data) : void 0;
1580
1424
  return {
1581
1425
  sync: decodedSyncMessage && {
1582
1426
  headsLength: decodedSyncMessage.heads.length,
@@ -1588,7 +1432,7 @@ var logSendSync = (message) => {
1588
1432
  to: message.targetId
1589
1433
  };
1590
1434
  }, {
1591
- F: __dxlog_file5,
1435
+ F: __dxlog_file4,
1592
1436
  L: 140,
1593
1437
  S: void 0,
1594
1438
  C: (f, a) => f(...a)
@@ -1596,14 +1440,14 @@ var logSendSync = (message) => {
1596
1440
  };
1597
1441
 
1598
1442
  // packages/core/echo/echo-pipeline/src/automerge/space-collection.ts
1599
- import { invariant as invariant5 } from "@dxos/invariant";
1443
+ import { invariant as invariant4 } from "@dxos/invariant";
1600
1444
  import { SpaceId } from "@dxos/keys";
1601
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1445
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1602
1446
  var deriveCollectionIdFromSpaceId = (spaceId, rootDocumentId) => rootDocumentId ? `space:${spaceId}:${rootDocumentId}` : `space:${spaceId}`;
1603
1447
  var getSpaceIdFromCollectionId = (collectionId) => {
1604
1448
  const spaceId = collectionId.split(":")[1];
1605
- invariant5(SpaceId.isValid(spaceId), void 0, {
1606
- F: __dxlog_file6,
1449
+ invariant4(SpaceId.isValid(spaceId), void 0, {
1450
+ F: __dxlog_file5,
1607
1451
  L: 16,
1608
1452
  S: void 0,
1609
1453
  A: [
@@ -1615,7 +1459,7 @@ var getSpaceIdFromCollectionId = (collectionId) => {
1615
1459
  };
1616
1460
 
1617
1461
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1618
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1462
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1619
1463
  var MeshEchoReplicator = class {
1620
1464
  constructor() {
1621
1465
  /**
@@ -1652,8 +1496,8 @@ var MeshEchoReplicator = class {
1652
1496
  this._context = null;
1653
1497
  }
1654
1498
  createExtension(extensionFactory) {
1655
- invariant6(this._context, void 0, {
1656
- F: __dxlog_file7,
1499
+ invariant5(this._context, void 0, {
1500
+ F: __dxlog_file6,
1657
1501
  L: 67,
1658
1502
  S: this,
1659
1503
  A: [
@@ -1665,16 +1509,16 @@ var MeshEchoReplicator = class {
1665
1509
  ownPeerId: this._context.peerId,
1666
1510
  replicatorFactory: extensionFactory,
1667
1511
  onRemoteConnected: async () => {
1668
- log6("onRemoteConnected", {
1512
+ log5("onRemoteConnected", {
1669
1513
  peerId: connection.peerId
1670
1514
  }, {
1671
- F: __dxlog_file7,
1515
+ F: __dxlog_file6,
1672
1516
  L: 73,
1673
1517
  S: this,
1674
1518
  C: (f, a) => f(...a)
1675
1519
  });
1676
- invariant6(this._context, void 0, {
1677
- F: __dxlog_file7,
1520
+ invariant5(this._context, void 0, {
1521
+ F: __dxlog_file6,
1678
1522
  L: 74,
1679
1523
  S: this,
1680
1524
  A: [
@@ -1696,10 +1540,10 @@ var MeshEchoReplicator = class {
1696
1540
  }
1697
1541
  },
1698
1542
  onRemoteDisconnected: async () => {
1699
- log6("onRemoteDisconnected", {
1543
+ log5("onRemoteDisconnected", {
1700
1544
  peerId: connection.peerId
1701
1545
  }, {
1702
- F: __dxlog_file7,
1546
+ F: __dxlog_file6,
1703
1547
  L: 88,
1704
1548
  S: this,
1705
1549
  C: (f, a) => f(...a)
@@ -1708,10 +1552,10 @@ var MeshEchoReplicator = class {
1708
1552
  const existingConnections = this._connectionsPerPeer.get(connection.peerId) ?? [];
1709
1553
  const index = existingConnections.indexOf(connection);
1710
1554
  if (index < 0) {
1711
- log6.warn("disconnected connection not found", {
1555
+ log5.warn("disconnected connection not found", {
1712
1556
  peerId: connection.peerId
1713
1557
  }, {
1714
- F: __dxlog_file7,
1558
+ F: __dxlog_file6,
1715
1559
  L: 96,
1716
1560
  S: this,
1717
1561
  C: (f, a) => f(...a)
@@ -1729,17 +1573,17 @@ var MeshEchoReplicator = class {
1729
1573
  }
1730
1574
  },
1731
1575
  shouldAdvertise: async (params) => {
1732
- log6("shouldAdvertise", {
1576
+ log5("shouldAdvertise", {
1733
1577
  peerId: connection.peerId,
1734
1578
  documentId: params.documentId
1735
1579
  }, {
1736
- F: __dxlog_file7,
1580
+ F: __dxlog_file6,
1737
1581
  L: 114,
1738
1582
  S: this,
1739
1583
  C: (f, a) => f(...a)
1740
1584
  });
1741
- invariant6(this._context, void 0, {
1742
- F: __dxlog_file7,
1585
+ invariant5(this._context, void 0, {
1586
+ F: __dxlog_file6,
1743
1587
  L: 115,
1744
1588
  S: this,
1745
1589
  A: [
@@ -1754,12 +1598,12 @@ var MeshEchoReplicator = class {
1754
1598
  documentId: params.documentId,
1755
1599
  peerId: connection.peerId
1756
1600
  });
1757
- log6("document not found locally for share policy check", {
1601
+ log5("document not found locally for share policy check", {
1758
1602
  peerId: connection.peerId,
1759
1603
  documentId: params.documentId,
1760
1604
  acceptDocument: remoteDocumentExists
1761
1605
  }, {
1762
- F: __dxlog_file7,
1606
+ F: __dxlog_file6,
1763
1607
  L: 123,
1764
1608
  S: this,
1765
1609
  C: (f, a) => f(...a)
@@ -1769,11 +1613,11 @@ var MeshEchoReplicator = class {
1769
1613
  const spaceId = await createIdFromSpaceKey(spaceKey);
1770
1614
  const authorizedDevices = this._authorizedDevices.get(spaceId);
1771
1615
  if (!connection.remoteDeviceKey) {
1772
- log6("device key not found for share policy check", {
1616
+ log5("device key not found for share policy check", {
1773
1617
  peerId: connection.peerId,
1774
1618
  documentId: params.documentId
1775
1619
  }, {
1776
- F: __dxlog_file7,
1620
+ F: __dxlog_file6,
1777
1621
  L: 139,
1778
1622
  S: this,
1779
1623
  C: (f, a) => f(...a)
@@ -1781,7 +1625,7 @@ var MeshEchoReplicator = class {
1781
1625
  return false;
1782
1626
  }
1783
1627
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1784
- log6("share policy check", {
1628
+ log5("share policy check", {
1785
1629
  localPeer: this._context.peerId,
1786
1630
  remotePeer: connection.peerId,
1787
1631
  documentId: params.documentId,
@@ -1789,15 +1633,15 @@ var MeshEchoReplicator = class {
1789
1633
  spaceKey,
1790
1634
  isAuthorized
1791
1635
  }, {
1792
- F: __dxlog_file7,
1636
+ F: __dxlog_file6,
1793
1637
  L: 147,
1794
1638
  S: this,
1795
1639
  C: (f, a) => f(...a)
1796
1640
  });
1797
1641
  return isAuthorized;
1798
1642
  } catch (err) {
1799
- log6.catch(err, void 0, {
1800
- F: __dxlog_file7,
1643
+ log5.catch(err, void 0, {
1644
+ F: __dxlog_file6,
1801
1645
  L: 157,
1802
1646
  S: this,
1803
1647
  C: (f, a) => f(...a)
@@ -1809,11 +1653,11 @@ var MeshEchoReplicator = class {
1809
1653
  const spaceId = getSpaceIdFromCollectionId(collectionId);
1810
1654
  const authorizedDevices = this._authorizedDevices.get(spaceId);
1811
1655
  if (!connection.remoteDeviceKey) {
1812
- log6("device key not found for collection sync check", {
1656
+ log5("device key not found for collection sync check", {
1813
1657
  peerId: connection.peerId,
1814
1658
  collectionId
1815
1659
  }, {
1816
- F: __dxlog_file7,
1660
+ F: __dxlog_file6,
1817
1661
  L: 167,
1818
1662
  S: this,
1819
1663
  C: (f, a) => f(...a)
@@ -1828,11 +1672,11 @@ var MeshEchoReplicator = class {
1828
1672
  return connection.replicatorExtension;
1829
1673
  }
1830
1674
  async authorizeDevice(spaceKey, deviceKey) {
1831
- log6("authorizeDevice", {
1675
+ log5("authorizeDevice", {
1832
1676
  spaceKey,
1833
1677
  deviceKey
1834
1678
  }, {
1835
- F: __dxlog_file7,
1679
+ F: __dxlog_file6,
1836
1680
  L: 184,
1837
1681
  S: this,
1838
1682
  C: (f, a) => f(...a)
@@ -2210,6 +2054,164 @@ var getByteCount = (message) => {
2210
2054
  return message.type.length + message.senderId.length + message.targetId.length + (message.data?.byteLength ?? 0) + (message.documentId?.length ?? 0);
2211
2055
  };
2212
2056
 
2057
+ // packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts
2058
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/documents-synchronizer.ts";
2059
+ var MAX_UPDATE_FREQ = 10;
2060
+ var DocumentsSynchronizer = class extends Resource5 {
2061
+ constructor(_params) {
2062
+ super(), this._params = _params, this._syncStates = /* @__PURE__ */ new Map(), this._pendingUpdates = /* @__PURE__ */ new Set(), this._sendUpdatesJob = void 0;
2063
+ }
2064
+ addDocuments(documentIds, retryCounter = 0) {
2065
+ if (retryCounter > 3) {
2066
+ log6.warn("Failed to load document, retry limit reached", {
2067
+ documentIds
2068
+ }, {
2069
+ F: __dxlog_file7,
2070
+ L: 52,
2071
+ S: this,
2072
+ C: (f, a) => f(...a)
2073
+ });
2074
+ return;
2075
+ }
2076
+ for (const documentId of documentIds) {
2077
+ this._params.repo.find(documentId).then(async (doc) => {
2078
+ await doc.whenReady();
2079
+ this._startSync(doc);
2080
+ this._pendingUpdates.add(doc.documentId);
2081
+ this._sendUpdatesJob.trigger();
2082
+ }).catch((error) => {
2083
+ log6.warn("Failed to load document, wraparound", {
2084
+ documentId,
2085
+ error
2086
+ }, {
2087
+ F: __dxlog_file7,
2088
+ L: 66,
2089
+ S: this,
2090
+ C: (f, a) => f(...a)
2091
+ });
2092
+ this.addDocuments([
2093
+ documentId
2094
+ ], retryCounter + 1);
2095
+ });
2096
+ }
2097
+ }
2098
+ removeDocuments(documentIds) {
2099
+ for (const documentId of documentIds) {
2100
+ this._syncStates.get(documentId)?.clearSubscriptions?.();
2101
+ this._syncStates.delete(documentId);
2102
+ this._pendingUpdates.delete(documentId);
2103
+ }
2104
+ }
2105
+ async _open() {
2106
+ this._sendUpdatesJob = new UpdateScheduler(this._ctx, this._checkAndSendUpdates.bind(this), {
2107
+ maxFrequency: MAX_UPDATE_FREQ
2108
+ });
2109
+ }
2110
+ async _close() {
2111
+ await this._sendUpdatesJob.join();
2112
+ this._syncStates.clear();
2113
+ }
2114
+ async update(updates) {
2115
+ for (const { documentId, mutation, isNew } of updates) {
2116
+ if (isNew) {
2117
+ const doc = await this._params.repo.find(documentId, FIND_PARAMS);
2118
+ doc.update((doc2) => A2.loadIncremental(doc2, mutation));
2119
+ this._startSync(doc);
2120
+ } else {
2121
+ this._writeMutation(documentId, mutation);
2122
+ }
2123
+ }
2124
+ }
2125
+ _startSync(doc) {
2126
+ if (this._syncStates.has(doc.documentId)) {
2127
+ log6.info("Document already being synced", {
2128
+ documentId: doc.documentId
2129
+ }, {
2130
+ F: __dxlog_file7,
2131
+ L: 105,
2132
+ S: this,
2133
+ C: (f, a) => f(...a)
2134
+ });
2135
+ return;
2136
+ }
2137
+ const syncState = {
2138
+ handle: doc
2139
+ };
2140
+ this._subscribeForChanges(syncState);
2141
+ this._syncStates.set(doc.documentId, syncState);
2142
+ }
2143
+ _subscribeForChanges(syncState) {
2144
+ const handler = () => {
2145
+ this._pendingUpdates.add(syncState.handle.documentId);
2146
+ this._sendUpdatesJob.trigger();
2147
+ };
2148
+ syncState.handle.on("heads-changed", handler);
2149
+ syncState.clearSubscriptions = () => syncState.handle.off("heads-changed", handler);
2150
+ }
2151
+ async _checkAndSendUpdates() {
2152
+ const updates = [];
2153
+ const docsWithPendingUpdates = Array.from(this._pendingUpdates);
2154
+ this._pendingUpdates.clear();
2155
+ for (const documentId of docsWithPendingUpdates) {
2156
+ const update = this._getPendingChanges(documentId);
2157
+ if (update) {
2158
+ updates.push({
2159
+ documentId,
2160
+ mutation: update
2161
+ });
2162
+ }
2163
+ }
2164
+ if (updates.length > 0) {
2165
+ this._params.sendUpdates({
2166
+ updates
2167
+ });
2168
+ }
2169
+ }
2170
+ _getPendingChanges(documentId) {
2171
+ const syncState = this._syncStates.get(documentId);
2172
+ invariant6(syncState, "Sync state for document not found", {
2173
+ F: __dxlog_file7,
2174
+ L: 146,
2175
+ S: this,
2176
+ A: [
2177
+ "syncState",
2178
+ "'Sync state for document not found'"
2179
+ ]
2180
+ });
2181
+ const handle = syncState.handle;
2182
+ if (!handle || !handle.isReady() || !handle.doc()) {
2183
+ return;
2184
+ }
2185
+ const doc = handle.doc();
2186
+ const mutation = syncState.lastSentHead ? A2.saveSince(doc, syncState.lastSentHead) : A2.save(doc);
2187
+ if (mutation.length === 0) {
2188
+ return;
2189
+ }
2190
+ syncState.lastSentHead = A2.getHeads(doc);
2191
+ return mutation;
2192
+ }
2193
+ _writeMutation(documentId, mutation) {
2194
+ const syncState = this._syncStates.get(documentId);
2195
+ invariant6(syncState, "Sync state for document not found", {
2196
+ F: __dxlog_file7,
2197
+ L: 162,
2198
+ S: this,
2199
+ A: [
2200
+ "syncState",
2201
+ "'Sync state for document not found'"
2202
+ ]
2203
+ });
2204
+ syncState.handle.update((doc) => {
2205
+ const headsBefore = A2.getHeads(doc);
2206
+ const newDoc = A2.loadIncremental(doc, mutation);
2207
+ if (A2.equals(headsBefore, syncState.lastSentHead)) {
2208
+ syncState.lastSentHead = A2.getHeads(newDoc);
2209
+ }
2210
+ return newDoc;
2211
+ });
2212
+ }
2213
+ };
2214
+
2213
2215
  // packages/core/echo/echo-pipeline/src/db-host/data-service.ts
2214
2216
  var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/db-host/data-service.ts";
2215
2217
  var DataServiceImpl = class {
@@ -2273,7 +2275,7 @@ var DataServiceImpl = class {
2273
2275
  "'Subscription not found'"
2274
2276
  ]
2275
2277
  });
2276
- synchronizer.update(request.updates);
2278
+ await synchronizer.update(request.updates);
2277
2279
  }
2278
2280
  async flush(request) {
2279
2281
  await this._automergeHost.flush(request);
@@ -2961,6 +2963,7 @@ var ExecutionTrace = Object.freeze({
2961
2963
  return go(trace6, 0);
2962
2964
  }
2963
2965
  });
2966
+ var TRACE_QUERY_EXECUTION = false;
2964
2967
  var QueryExecutor = class extends Resource6 {
2965
2968
  constructor(options) {
2966
2969
  super();
@@ -3000,7 +3003,7 @@ var QueryExecutor = class extends Resource6 {
3000
3003
  async execQuery() {
3001
3004
  invariant10(this._lifecycleState === LifecycleState3.OPEN, void 0, {
3002
3005
  F: __dxlog_file11,
3003
- L: 171,
3006
+ L: 173,
3004
3007
  S: this,
3005
3008
  A: [
3006
3009
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3016,6 +3019,9 @@ var QueryExecutor = class extends Resource6 {
3016
3019
  });
3017
3020
  this._trace = trace6;
3018
3021
  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);
3022
+ if (TRACE_QUERY_EXECUTION) {
3023
+ console.log(ExecutionTrace.format(trace6));
3024
+ }
3019
3025
  return {
3020
3026
  changed
3021
3027
  };