@dxos/echo-pipeline 0.7.3 → 0.7.4-staging.99db212

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 (31) hide show
  1. package/dist/lib/browser/index.mjs +76 -37
  2. package/dist/lib/browser/index.mjs.map +3 -3
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +78 -37
  5. package/dist/lib/node/index.cjs.map +3 -3
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +76 -37
  8. package/dist/lib/node-esm/index.mjs.map +3 -3
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/automerge/automerge-host.d.ts +1 -0
  11. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  12. package/dist/types/src/automerge/collection-synchronizer.d.ts +3 -1
  13. package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
  14. package/dist/types/src/automerge/space-collection.d.ts +2 -1
  15. package/dist/types/src/automerge/space-collection.d.ts.map +1 -1
  16. package/dist/types/src/db-host/data-service.d.ts +3 -0
  17. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  18. package/dist/types/src/db-host/echo-host.d.ts +1 -2
  19. package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
  20. package/dist/types/src/db-host/index.d.ts +1 -0
  21. package/dist/types/src/db-host/index.d.ts.map +1 -1
  22. package/dist/types/src/db-host/space-state-manager.d.ts +4 -1
  23. package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
  24. package/package.json +34 -34
  25. package/src/automerge/automerge-host.ts +4 -0
  26. package/src/automerge/collection-synchronizer.ts +24 -13
  27. package/src/automerge/space-collection.ts +4 -2
  28. package/src/db-host/data-service.ts +17 -3
  29. package/src/db-host/echo-host.ts +9 -6
  30. package/src/db-host/index.ts +1 -0
  31. package/src/db-host/space-state-manager.ts +9 -1
@@ -223,6 +223,7 @@ var CollectionSynchronizer = class extends Resource2 {
223
223
  * CollectionId -> State.
224
224
  */
225
225
  this._perCollectionStates = /* @__PURE__ */ new Map();
226
+ this._activeCollections = /* @__PURE__ */ new Set();
226
227
  this._connectedPeers = /* @__PURE__ */ new Set();
227
228
  this.remoteStateUpdated = new Event();
228
229
  this._sendCollectionState = params.sendCollectionState;
@@ -232,43 +233,58 @@ var CollectionSynchronizer = class extends Resource2 {
232
233
  async _open(ctx) {
233
234
  scheduleTaskInterval(this._ctx, async () => {
234
235
  for (const collectionId of this._perCollectionStates.keys()) {
235
- this.refreshCollection(collectionId);
236
- await asyncReturn();
236
+ if (this._activeCollections.has(collectionId)) {
237
+ this.refreshCollection(collectionId);
238
+ await asyncReturn();
239
+ }
237
240
  }
238
241
  }, POLL_INTERVAL);
239
242
  }
240
243
  getRegisteredCollectionIds() {
241
244
  return [
242
- ...this._perCollectionStates.keys()
245
+ ...this._activeCollections
243
246
  ];
244
247
  }
245
248
  getLocalCollectionState(collectionId) {
246
- return this._getPerCollectionState(collectionId).localState;
249
+ return this._perCollectionStates.get(collectionId)?.localState;
247
250
  }
248
251
  setLocalCollectionState(collectionId, state) {
252
+ this._activeCollections.add(collectionId);
249
253
  log2("setLocalCollectionState", {
250
254
  collectionId,
251
255
  state
252
256
  }, {
253
257
  F: __dxlog_file2,
254
- L: 68,
258
+ L: 73,
255
259
  S: this,
256
260
  C: (f, a) => f(...a)
257
261
  });
258
- this._getPerCollectionState(collectionId).localState = state;
262
+ this._getOrCreatePerCollectionState(collectionId).localState = state;
259
263
  queueMicrotask(async () => {
260
- if (!this._ctx.disposed) {
264
+ if (!this._ctx.disposed && this._activeCollections.has(collectionId)) {
261
265
  this._refreshInterestedPeers(collectionId);
262
266
  this.refreshCollection(collectionId);
263
267
  }
264
268
  });
265
269
  }
270
+ clearLocalCollectionState(collectionId) {
271
+ this._activeCollections.delete(collectionId);
272
+ this._perCollectionStates.delete(collectionId);
273
+ log2("clearLocalCollectionState", {
274
+ collectionId
275
+ }, {
276
+ F: __dxlog_file2,
277
+ L: 87,
278
+ S: this,
279
+ C: (f, a) => f(...a)
280
+ });
281
+ }
266
282
  getRemoteCollectionStates(collectionId) {
267
- return this._getPerCollectionState(collectionId).remoteStates;
283
+ return this._getOrCreatePerCollectionState(collectionId).remoteStates;
268
284
  }
269
285
  refreshCollection(collectionId) {
270
286
  let scheduleAnotherRefresh = false;
271
- const state = this._getPerCollectionState(collectionId);
287
+ const state = this._getOrCreatePerCollectionState(collectionId);
272
288
  for (const peerId of this._connectedPeers) {
273
289
  if (state.interestedPeers.has(peerId)) {
274
290
  const lastQueried = state.lastQueried.get(peerId) ?? 0;
@@ -315,7 +331,7 @@ var CollectionSynchronizer = class extends Resource2 {
315
331
  * Callback when a peer queries the state of a collection.
316
332
  */
317
333
  onCollectionStateQueried(collectionId, peerId) {
318
- const perCollectionState = this._getPerCollectionState(collectionId);
334
+ const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
319
335
  if (perCollectionState.localState) {
320
336
  this._sendCollectionState(collectionId, peerId, perCollectionState.localState);
321
337
  }
@@ -330,19 +346,19 @@ var CollectionSynchronizer = class extends Resource2 {
330
346
  state
331
347
  }, {
332
348
  F: __dxlog_file2,
333
- L: 148,
349
+ L: 159,
334
350
  S: this,
335
351
  C: (f, a) => f(...a)
336
352
  });
337
353
  validateCollectionState(state);
338
- const perCollectionState = this._getPerCollectionState(collectionId);
354
+ const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
339
355
  perCollectionState.remoteStates.set(peerId, state);
340
356
  this.remoteStateUpdated.emit({
341
357
  peerId,
342
358
  collectionId
343
359
  });
344
360
  }
345
- _getPerCollectionState(collectionId) {
361
+ _getOrCreatePerCollectionState(collectionId) {
346
362
  return defaultMap(this._perCollectionStates, collectionId, () => ({
347
363
  localState: void 0,
348
364
  remoteStates: /* @__PURE__ */ new Map(),
@@ -353,9 +369,9 @@ var CollectionSynchronizer = class extends Resource2 {
353
369
  _refreshInterestedPeers(collectionId) {
354
370
  for (const peerId of this._connectedPeers) {
355
371
  if (this._shouldSyncCollection(collectionId, peerId)) {
356
- this._getPerCollectionState(collectionId).interestedPeers.add(peerId);
372
+ this._getOrCreatePerCollectionState(collectionId).interestedPeers.add(peerId);
357
373
  } else {
358
- this._getPerCollectionState(collectionId).interestedPeers.delete(peerId);
374
+ this._getOrCreatePerCollectionState(collectionId).interestedPeers.delete(peerId);
359
375
  }
360
376
  }
361
377
  }
@@ -1241,6 +1257,9 @@ var AutomergeHost = class extends Resource4 {
1241
1257
  documents
1242
1258
  });
1243
1259
  }
1260
+ async clearLocalCollectionState(collectionId) {
1261
+ this._collectionSynchronizer.clearLocalCollectionState(collectionId);
1262
+ }
1244
1263
  _onCollectionStateQueried(collectionId, peerId) {
1245
1264
  this._collectionSynchronizer.onCollectionStateQueried(collectionId, peerId);
1246
1265
  }
@@ -1278,7 +1297,7 @@ var AutomergeHost = class extends Resource4 {
1278
1297
  count: toReplicate.length
1279
1298
  }, {
1280
1299
  F: __dxlog_file4,
1281
- L: 495,
1300
+ L: 499,
1282
1301
  S: this,
1283
1302
  C: (f, a) => f(...a)
1284
1303
  });
@@ -1345,7 +1364,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1345
1364
  var decodeCollectionState = (state) => {
1346
1365
  invariant3(typeof state === "object" && state !== null, "Invalid state", {
1347
1366
  F: __dxlog_file4,
1348
- L: 553,
1367
+ L: 557,
1349
1368
  S: void 0,
1350
1369
  A: [
1351
1370
  "typeof state === 'object' && state !== null",
@@ -1514,12 +1533,12 @@ var logSendSync = (message) => {
1514
1533
  import { invariant as invariant5 } from "@dxos/invariant";
1515
1534
  import { SpaceId } from "@dxos/keys";
1516
1535
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1517
- var deriveCollectionIdFromSpaceId = (spaceId) => `space:${spaceId}`;
1536
+ var deriveCollectionIdFromSpaceId = (spaceId, rootDocumentId) => rootDocumentId ? `space:${spaceId}:${rootDocumentId}` : `space:${spaceId}`;
1518
1537
  var getSpaceIdFromCollectionId = (collectionId) => {
1519
- const spaceId = collectionId.replace(/^space:/, "");
1538
+ const spaceId = collectionId.split(":")[1];
1520
1539
  invariant5(SpaceId.isValid(spaceId), void 0, {
1521
1540
  F: __dxlog_file6,
1522
- L: 13,
1541
+ L: 15,
1523
1542
  S: void 0,
1524
1543
  A: [
1525
1544
  "SpaceId.isValid(spaceId)",
@@ -2103,6 +2122,7 @@ var DataServiceImpl = class {
2103
2122
  */
2104
2123
  this._subscriptions = /* @__PURE__ */ new Map();
2105
2124
  this._automergeHost = params.automergeHost;
2125
+ this._spaceStateManager = params.spaceStateManager;
2106
2126
  this._updateIndexes = params.updateIndexes;
2107
2127
  }
2108
2128
  subscribe(request) {
@@ -2116,7 +2136,7 @@ var DataServiceImpl = class {
2116
2136
  ready();
2117
2137
  }).catch((err) => log7.catch(err, void 0, {
2118
2138
  F: __dxlog_file8,
2119
- L: 66,
2139
+ L: 70,
2120
2140
  S: this,
2121
2141
  C: (f, a) => f(...a)
2122
2142
  }));
@@ -2127,7 +2147,7 @@ var DataServiceImpl = class {
2127
2147
  const synchronizer = this._subscriptions.get(request.subscriptionId);
2128
2148
  invariant7(synchronizer, "Subscription not found", {
2129
2149
  F: __dxlog_file8,
2130
- L: 73,
2150
+ L: 77,
2131
2151
  S: this,
2132
2152
  A: [
2133
2153
  "synchronizer",
@@ -2148,7 +2168,7 @@ var DataServiceImpl = class {
2148
2168
  const synchronizer = this._subscriptions.get(request.subscriptionId);
2149
2169
  invariant7(synchronizer, "Subscription not found", {
2150
2170
  F: __dxlog_file8,
2151
- L: 88,
2171
+ L: 92,
2152
2172
  S: this,
2153
2173
  A: [
2154
2174
  "synchronizer",
@@ -2190,18 +2210,29 @@ var DataServiceImpl = class {
2190
2210
  }
2191
2211
  subscribeSpaceSyncState(request) {
2192
2212
  return new Stream(({ ctx, next, ready }) => {
2193
- invariant7(SpaceId2.isValid(request.spaceId), void 0, {
2213
+ const spaceId = request.spaceId;
2214
+ invariant7(SpaceId2.isValid(spaceId), void 0, {
2194
2215
  F: __dxlog_file8,
2195
- L: 127,
2216
+ L: 132,
2196
2217
  S: this,
2197
2218
  A: [
2198
- "SpaceId.isValid(request.spaceId)",
2219
+ "SpaceId.isValid(spaceId)",
2199
2220
  ""
2200
2221
  ]
2201
2222
  });
2202
- const collectionId = deriveCollectionIdFromSpaceId(request.spaceId);
2223
+ const rootDocumentId = this._spaceStateManager.getSpaceRootDocumentId(spaceId);
2224
+ let collectionId = rootDocumentId && deriveCollectionIdFromSpaceId(spaceId, rootDocumentId);
2225
+ this._spaceStateManager.spaceDocumentListUpdated.on(ctx, (event) => {
2226
+ const newId = deriveCollectionIdFromSpaceId(spaceId, event.spaceRootId);
2227
+ if (newId !== collectionId) {
2228
+ collectionId = newId;
2229
+ scheduler.trigger();
2230
+ }
2231
+ });
2203
2232
  const scheduler = new UpdateScheduler2(ctx, async () => {
2204
- const state = await this._automergeHost.getCollectionSyncState(collectionId);
2233
+ const state = collectionId ? await this._automergeHost.getCollectionSyncState(collectionId) : {
2234
+ peers: []
2235
+ };
2205
2236
  next({
2206
2237
  peers: state.peers.map((peer) => ({
2207
2238
  peerId: peer.peerId,
@@ -2827,6 +2858,9 @@ var SpaceStateManager = class extends Resource8 {
2827
2858
  getRootByDocumentId(documentId) {
2828
2859
  return this._roots.get(documentId);
2829
2860
  }
2861
+ getSpaceRootDocumentId(spaceId) {
2862
+ return this._rootBySpace.get(spaceId);
2863
+ }
2830
2864
  async assignRootToSpace(spaceId, handle) {
2831
2865
  let root;
2832
2866
  if (this._roots.has(handle.documentId)) {
@@ -2846,7 +2880,7 @@ var SpaceStateManager = class extends Resource8 {
2846
2880
  this._rootBySpace.set(spaceId, root.handle.documentId);
2847
2881
  const ctx = new Context5(void 0, {
2848
2882
  F: __dxlog_file14,
2849
- L: 58
2883
+ L: 62
2850
2884
  });
2851
2885
  this._perRootContext.set(root.handle.documentId, ctx);
2852
2886
  await root.handle.whenReady();
@@ -2857,7 +2891,7 @@ var SpaceStateManager = class extends Resource8 {
2857
2891
  ];
2858
2892
  if (!isEqual(documentIds, this._lastSpaceDocumentList.get(spaceId))) {
2859
2893
  this._lastSpaceDocumentList.set(spaceId, documentIds);
2860
- this.spaceDocumentListUpdated.emit(new SpaceDocumentListUpdatedEvent(spaceId, documentIds));
2894
+ this.spaceDocumentListUpdated.emit(new SpaceDocumentListUpdatedEvent(spaceId, root.documentId, prevRootId, documentIds));
2861
2895
  }
2862
2896
  }, {
2863
2897
  maxFrequency: 50
@@ -2870,8 +2904,10 @@ var SpaceStateManager = class extends Resource8 {
2870
2904
  }
2871
2905
  };
2872
2906
  var SpaceDocumentListUpdatedEvent = class {
2873
- constructor(spaceId, documentIds) {
2907
+ constructor(spaceId, spaceRootId, previousRootId, documentIds) {
2874
2908
  this.spaceId = spaceId;
2909
+ this.spaceRootId = spaceRootId;
2910
+ this.previousRootId = previousRootId;
2875
2911
  this.documentIds = documentIds;
2876
2912
  }
2877
2913
  };
@@ -2916,6 +2952,7 @@ var EchoHost = class extends Resource9 {
2916
2952
  });
2917
2953
  this._dataService = new DataServiceImpl({
2918
2954
  automergeHost: this._automergeHost,
2955
+ spaceStateManager: this._spaceStateManager,
2919
2956
  updateIndexes: async () => {
2920
2957
  await this._indexer.updateIndexes();
2921
2958
  }
@@ -2979,7 +3016,11 @@ var EchoHost = class extends Resource9 {
2979
3016
  await this._queryService.open(ctx);
2980
3017
  await this._spaceStateManager.open(ctx);
2981
3018
  this._spaceStateManager.spaceDocumentListUpdated.on(this._ctx, (e) => {
3019
+ if (e.previousRootId) {
3020
+ void this._automergeHost.clearLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId, e.previousRootId));
3021
+ }
2982
3022
  void this._automergeHost.updateLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId), e.documentIds);
3023
+ void this._automergeHost.updateLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId, e.spaceRootId), e.documentIds);
2983
3024
  });
2984
3025
  }
2985
3026
  async _close(ctx) {
@@ -3018,7 +3059,7 @@ var EchoHost = class extends Resource9 {
3018
3059
  async createSpaceRoot(spaceKey) {
3019
3060
  invariant11(this._lifecycleState === LifecycleState4.OPEN, void 0, {
3020
3061
  F: __dxlog_file15,
3021
- L: 209,
3062
+ L: 217,
3022
3063
  S: this,
3023
3064
  A: [
3024
3065
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3046,7 +3087,7 @@ var EchoHost = class extends Resource9 {
3046
3087
  async openSpaceRoot(spaceId, automergeUrl) {
3047
3088
  invariant11(this._lifecycleState === LifecycleState4.OPEN, void 0, {
3048
3089
  F: __dxlog_file15,
3049
- L: 228,
3090
+ L: 236,
3050
3091
  S: this,
3051
3092
  A: [
3052
3093
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3072,10 +3113,6 @@ var EchoHost = class extends Resource9 {
3072
3113
  async removeReplicator(replicator) {
3073
3114
  await this._automergeHost.removeReplicator(replicator);
3074
3115
  }
3075
- async getSpaceSyncState(spaceId) {
3076
- const collectionId = deriveCollectionIdFromSpaceId(spaceId);
3077
- return this._automergeHost.getCollectionSyncState(collectionId);
3078
- }
3079
3116
  };
3080
3117
 
3081
3118
  // packages/core/echo/echo-pipeline/src/edge/echo-edge-replicator.ts
@@ -3508,9 +3545,11 @@ export {
3508
3545
  QueryServiceImpl,
3509
3546
  QueryState,
3510
3547
  Space,
3548
+ SpaceDocumentListUpdatedEvent,
3511
3549
  SpaceManager,
3512
3550
  SpaceProtocol,
3513
3551
  SpaceProtocolSession,
3552
+ SpaceStateManager,
3514
3553
  TimeframeClock,
3515
3554
  codec,
3516
3555
  createIdFromSpaceKey,