@dxos/echo-pipeline 0.7.4-staging.f7e8224 → 0.7.4

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
@@ -48,9 +48,11 @@ __export(node_exports, {
48
48
  QueryServiceImpl: () => QueryServiceImpl,
49
49
  QueryState: () => QueryState,
50
50
  Space: () => import_chunk_MACQJ2EP.Space,
51
+ SpaceDocumentListUpdatedEvent: () => SpaceDocumentListUpdatedEvent,
51
52
  SpaceManager: () => import_chunk_MACQJ2EP.SpaceManager,
52
53
  SpaceProtocol: () => import_chunk_MACQJ2EP.SpaceProtocol,
53
54
  SpaceProtocolSession: () => import_chunk_MACQJ2EP.SpaceProtocolSession,
55
+ SpaceStateManager: () => SpaceStateManager,
54
56
  TimeframeClock: () => import_chunk_MACQJ2EP.TimeframeClock,
55
57
  codec: () => import_chunk_MACQJ2EP.codec,
56
58
  createIdFromSpaceKey: () => import_chunk_MACQJ2EP.createIdFromSpaceKey,
@@ -329,6 +331,7 @@ var CollectionSynchronizer = class extends import_context3.Resource {
329
331
  constructor(params) {
330
332
  super();
331
333
  this._perCollectionStates = /* @__PURE__ */ new Map();
334
+ this._activeCollections = /* @__PURE__ */ new Set();
332
335
  this._connectedPeers = /* @__PURE__ */ new Set();
333
336
  this.remoteStateUpdated = new import_async4.Event();
334
337
  this._sendCollectionState = params.sendCollectionState;
@@ -338,43 +341,58 @@ var CollectionSynchronizer = class extends import_context3.Resource {
338
341
  async _open(ctx) {
339
342
  (0, import_async4.scheduleTaskInterval)(this._ctx, async () => {
340
343
  for (const collectionId of this._perCollectionStates.keys()) {
341
- this.refreshCollection(collectionId);
342
- await (0, import_async4.asyncReturn)();
344
+ if (this._activeCollections.has(collectionId)) {
345
+ this.refreshCollection(collectionId);
346
+ await (0, import_async4.asyncReturn)();
347
+ }
343
348
  }
344
349
  }, POLL_INTERVAL);
345
350
  }
346
351
  getRegisteredCollectionIds() {
347
352
  return [
348
- ...this._perCollectionStates.keys()
353
+ ...this._activeCollections
349
354
  ];
350
355
  }
351
356
  getLocalCollectionState(collectionId) {
352
- return this._getPerCollectionState(collectionId).localState;
357
+ return this._perCollectionStates.get(collectionId)?.localState;
353
358
  }
354
359
  setLocalCollectionState(collectionId, state) {
360
+ this._activeCollections.add(collectionId);
355
361
  (0, import_log4.log)("setLocalCollectionState", {
356
362
  collectionId,
357
363
  state
358
364
  }, {
359
365
  F: __dxlog_file2,
360
- L: 68,
366
+ L: 73,
361
367
  S: this,
362
368
  C: (f, a) => f(...a)
363
369
  });
364
- this._getPerCollectionState(collectionId).localState = state;
370
+ this._getOrCreatePerCollectionState(collectionId).localState = state;
365
371
  queueMicrotask(async () => {
366
- if (!this._ctx.disposed) {
372
+ if (!this._ctx.disposed && this._activeCollections.has(collectionId)) {
367
373
  this._refreshInterestedPeers(collectionId);
368
374
  this.refreshCollection(collectionId);
369
375
  }
370
376
  });
371
377
  }
378
+ clearLocalCollectionState(collectionId) {
379
+ this._activeCollections.delete(collectionId);
380
+ this._perCollectionStates.delete(collectionId);
381
+ (0, import_log4.log)("clearLocalCollectionState", {
382
+ collectionId
383
+ }, {
384
+ F: __dxlog_file2,
385
+ L: 87,
386
+ S: this,
387
+ C: (f, a) => f(...a)
388
+ });
389
+ }
372
390
  getRemoteCollectionStates(collectionId) {
373
- return this._getPerCollectionState(collectionId).remoteStates;
391
+ return this._getOrCreatePerCollectionState(collectionId).remoteStates;
374
392
  }
375
393
  refreshCollection(collectionId) {
376
394
  let scheduleAnotherRefresh = false;
377
- const state = this._getPerCollectionState(collectionId);
395
+ const state = this._getOrCreatePerCollectionState(collectionId);
378
396
  for (const peerId of this._connectedPeers) {
379
397
  if (state.interestedPeers.has(peerId)) {
380
398
  const lastQueried = state.lastQueried.get(peerId) ?? 0;
@@ -421,7 +439,7 @@ var CollectionSynchronizer = class extends import_context3.Resource {
421
439
  * Callback when a peer queries the state of a collection.
422
440
  */
423
441
  onCollectionStateQueried(collectionId, peerId) {
424
- const perCollectionState = this._getPerCollectionState(collectionId);
442
+ const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
425
443
  if (perCollectionState.localState) {
426
444
  this._sendCollectionState(collectionId, peerId, perCollectionState.localState);
427
445
  }
@@ -436,19 +454,19 @@ var CollectionSynchronizer = class extends import_context3.Resource {
436
454
  state
437
455
  }, {
438
456
  F: __dxlog_file2,
439
- L: 148,
457
+ L: 159,
440
458
  S: this,
441
459
  C: (f, a) => f(...a)
442
460
  });
443
461
  validateCollectionState(state);
444
- const perCollectionState = this._getPerCollectionState(collectionId);
462
+ const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
445
463
  perCollectionState.remoteStates.set(peerId, state);
446
464
  this.remoteStateUpdated.emit({
447
465
  peerId,
448
466
  collectionId
449
467
  });
450
468
  }
451
- _getPerCollectionState(collectionId) {
469
+ _getOrCreatePerCollectionState(collectionId) {
452
470
  return (0, import_util.defaultMap)(this._perCollectionStates, collectionId, () => ({
453
471
  localState: void 0,
454
472
  remoteStates: /* @__PURE__ */ new Map(),
@@ -459,9 +477,9 @@ var CollectionSynchronizer = class extends import_context3.Resource {
459
477
  _refreshInterestedPeers(collectionId) {
460
478
  for (const peerId of this._connectedPeers) {
461
479
  if (this._shouldSyncCollection(collectionId, peerId)) {
462
- this._getPerCollectionState(collectionId).interestedPeers.add(peerId);
480
+ this._getOrCreatePerCollectionState(collectionId).interestedPeers.add(peerId);
463
481
  } else {
464
- this._getPerCollectionState(collectionId).interestedPeers.delete(peerId);
482
+ this._getOrCreatePerCollectionState(collectionId).interestedPeers.delete(peerId);
465
483
  }
466
484
  }
467
485
  }
@@ -1326,6 +1344,9 @@ var AutomergeHost = class extends import_context2.Resource {
1326
1344
  documents
1327
1345
  });
1328
1346
  }
1347
+ async clearLocalCollectionState(collectionId) {
1348
+ this._collectionSynchronizer.clearLocalCollectionState(collectionId);
1349
+ }
1329
1350
  _onCollectionStateQueried(collectionId, peerId) {
1330
1351
  this._collectionSynchronizer.onCollectionStateQueried(collectionId, peerId);
1331
1352
  }
@@ -1363,7 +1384,7 @@ var AutomergeHost = class extends import_context2.Resource {
1363
1384
  count: toReplicate.length
1364
1385
  }, {
1365
1386
  F: __dxlog_file4,
1366
- L: 495,
1387
+ L: 499,
1367
1388
  S: this,
1368
1389
  C: (f, a) => f(...a)
1369
1390
  });
@@ -1430,7 +1451,7 @@ var changeIsPresentInDoc = (doc, changeHash) => {
1430
1451
  var decodeCollectionState = (state) => {
1431
1452
  (0, import_invariant3.invariant)(typeof state === "object" && state !== null, "Invalid state", {
1432
1453
  F: __dxlog_file4,
1433
- L: 553,
1454
+ L: 557,
1434
1455
  S: void 0,
1435
1456
  A: [
1436
1457
  "typeof state === 'object' && state !== null",
@@ -1581,12 +1602,12 @@ var logSendSync = (message) => {
1581
1602
  });
1582
1603
  };
1583
1604
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/space-collection.ts";
1584
- var deriveCollectionIdFromSpaceId = (spaceId) => `space:${spaceId}`;
1605
+ var deriveCollectionIdFromSpaceId = (spaceId, rootDocumentId) => rootDocumentId ? `space:${spaceId}:${rootDocumentId}` : `space:${spaceId}`;
1585
1606
  var getSpaceIdFromCollectionId = (collectionId) => {
1586
- const spaceId = collectionId.replace(/^space:/, "");
1607
+ const spaceId = collectionId.split(":")[1];
1587
1608
  (0, import_invariant7.invariant)(import_keys4.SpaceId.isValid(spaceId), void 0, {
1588
1609
  F: __dxlog_file6,
1589
- L: 13,
1610
+ L: 15,
1590
1611
  S: void 0,
1591
1612
  A: [
1592
1613
  "SpaceId.isValid(spaceId)",
@@ -2152,6 +2173,7 @@ var DataServiceImpl = class {
2152
2173
  constructor(params) {
2153
2174
  this._subscriptions = /* @__PURE__ */ new Map();
2154
2175
  this._automergeHost = params.automergeHost;
2176
+ this._spaceStateManager = params.spaceStateManager;
2155
2177
  this._updateIndexes = params.updateIndexes;
2156
2178
  }
2157
2179
  subscribe(request) {
@@ -2165,7 +2187,7 @@ var DataServiceImpl = class {
2165
2187
  ready();
2166
2188
  }).catch((err) => import_log.log.catch(err, void 0, {
2167
2189
  F: __dxlog_file8,
2168
- L: 66,
2190
+ L: 70,
2169
2191
  S: this,
2170
2192
  C: (f, a) => f(...a)
2171
2193
  }));
@@ -2176,7 +2198,7 @@ var DataServiceImpl = class {
2176
2198
  const synchronizer = this._subscriptions.get(request.subscriptionId);
2177
2199
  (0, import_invariant.invariant)(synchronizer, "Subscription not found", {
2178
2200
  F: __dxlog_file8,
2179
- L: 73,
2201
+ L: 77,
2180
2202
  S: this,
2181
2203
  A: [
2182
2204
  "synchronizer",
@@ -2197,7 +2219,7 @@ var DataServiceImpl = class {
2197
2219
  const synchronizer = this._subscriptions.get(request.subscriptionId);
2198
2220
  (0, import_invariant.invariant)(synchronizer, "Subscription not found", {
2199
2221
  F: __dxlog_file8,
2200
- L: 88,
2222
+ L: 92,
2201
2223
  S: this,
2202
2224
  A: [
2203
2225
  "synchronizer",
@@ -2239,18 +2261,29 @@ var DataServiceImpl = class {
2239
2261
  }
2240
2262
  subscribeSpaceSyncState(request) {
2241
2263
  return new import_stream.Stream(({ ctx, next, ready }) => {
2242
- (0, import_invariant.invariant)(import_keys.SpaceId.isValid(request.spaceId), void 0, {
2264
+ const spaceId = request.spaceId;
2265
+ (0, import_invariant.invariant)(import_keys.SpaceId.isValid(spaceId), void 0, {
2243
2266
  F: __dxlog_file8,
2244
- L: 127,
2267
+ L: 132,
2245
2268
  S: this,
2246
2269
  A: [
2247
- "SpaceId.isValid(request.spaceId)",
2270
+ "SpaceId.isValid(spaceId)",
2248
2271
  ""
2249
2272
  ]
2250
2273
  });
2251
- const collectionId = deriveCollectionIdFromSpaceId(request.spaceId);
2274
+ const rootDocumentId = this._spaceStateManager.getSpaceRootDocumentId(spaceId);
2275
+ let collectionId = rootDocumentId && deriveCollectionIdFromSpaceId(spaceId, rootDocumentId);
2276
+ this._spaceStateManager.spaceDocumentListUpdated.on(ctx, (event) => {
2277
+ const newId = deriveCollectionIdFromSpaceId(spaceId, event.spaceRootId);
2278
+ if (newId !== collectionId) {
2279
+ collectionId = newId;
2280
+ scheduler.trigger();
2281
+ }
2282
+ });
2252
2283
  const scheduler = new import_async.UpdateScheduler(ctx, async () => {
2253
- const state = await this._automergeHost.getCollectionSyncState(collectionId);
2284
+ const state = collectionId ? await this._automergeHost.getCollectionSyncState(collectionId) : {
2285
+ peers: []
2286
+ };
2254
2287
  next({
2255
2288
  peers: state.peers.map((peer) => ({
2256
2289
  peerId: peer.peerId,
@@ -2821,6 +2854,9 @@ var SpaceStateManager = class extends import_context11.Resource {
2821
2854
  getRootByDocumentId(documentId) {
2822
2855
  return this._roots.get(documentId);
2823
2856
  }
2857
+ getSpaceRootDocumentId(spaceId) {
2858
+ return this._rootBySpace.get(spaceId);
2859
+ }
2824
2860
  async assignRootToSpace(spaceId, handle) {
2825
2861
  let root;
2826
2862
  if (this._roots.has(handle.documentId)) {
@@ -2840,7 +2876,7 @@ var SpaceStateManager = class extends import_context11.Resource {
2840
2876
  this._rootBySpace.set(spaceId, root.handle.documentId);
2841
2877
  const ctx = new import_context11.Context(void 0, {
2842
2878
  F: __dxlog_file14,
2843
- L: 58
2879
+ L: 62
2844
2880
  });
2845
2881
  this._perRootContext.set(root.handle.documentId, ctx);
2846
2882
  await root.handle.whenReady();
@@ -2851,7 +2887,7 @@ var SpaceStateManager = class extends import_context11.Resource {
2851
2887
  ];
2852
2888
  if (!(0, import_lodash.default)(documentIds, this._lastSpaceDocumentList.get(spaceId))) {
2853
2889
  this._lastSpaceDocumentList.set(spaceId, documentIds);
2854
- this.spaceDocumentListUpdated.emit(new SpaceDocumentListUpdatedEvent(spaceId, documentIds));
2890
+ this.spaceDocumentListUpdated.emit(new SpaceDocumentListUpdatedEvent(spaceId, root.documentId, prevRootId, documentIds));
2855
2891
  }
2856
2892
  }, {
2857
2893
  maxFrequency: 50
@@ -2864,8 +2900,10 @@ var SpaceStateManager = class extends import_context11.Resource {
2864
2900
  }
2865
2901
  };
2866
2902
  var SpaceDocumentListUpdatedEvent = class {
2867
- constructor(spaceId, documentIds) {
2903
+ constructor(spaceId, spaceRootId, previousRootId, documentIds) {
2868
2904
  this.spaceId = spaceId;
2905
+ this.spaceRootId = spaceRootId;
2906
+ this.previousRootId = previousRootId;
2869
2907
  this.documentIds = documentIds;
2870
2908
  }
2871
2909
  };
@@ -2908,6 +2946,7 @@ var EchoHost = class extends import_context7.Resource {
2908
2946
  });
2909
2947
  this._dataService = new DataServiceImpl({
2910
2948
  automergeHost: this._automergeHost,
2949
+ spaceStateManager: this._spaceStateManager,
2911
2950
  updateIndexes: async () => {
2912
2951
  await this._indexer.updateIndexes();
2913
2952
  }
@@ -2971,7 +3010,11 @@ var EchoHost = class extends import_context7.Resource {
2971
3010
  await this._queryService.open(ctx);
2972
3011
  await this._spaceStateManager.open(ctx);
2973
3012
  this._spaceStateManager.spaceDocumentListUpdated.on(this._ctx, (e) => {
3013
+ if (e.previousRootId) {
3014
+ void this._automergeHost.clearLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId, e.previousRootId));
3015
+ }
2974
3016
  void this._automergeHost.updateLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId), e.documentIds);
3017
+ void this._automergeHost.updateLocalCollectionState(deriveCollectionIdFromSpaceId(e.spaceId, e.spaceRootId), e.documentIds);
2975
3018
  });
2976
3019
  }
2977
3020
  async _close(ctx) {
@@ -3010,7 +3053,7 @@ var EchoHost = class extends import_context7.Resource {
3010
3053
  async createSpaceRoot(spaceKey) {
3011
3054
  (0, import_invariant8.invariant)(this._lifecycleState === import_context7.LifecycleState.OPEN, void 0, {
3012
3055
  F: __dxlog_file15,
3013
- L: 209,
3056
+ L: 217,
3014
3057
  S: this,
3015
3058
  A: [
3016
3059
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3038,7 +3081,7 @@ var EchoHost = class extends import_context7.Resource {
3038
3081
  async openSpaceRoot(spaceId, automergeUrl) {
3039
3082
  (0, import_invariant8.invariant)(this._lifecycleState === import_context7.LifecycleState.OPEN, void 0, {
3040
3083
  F: __dxlog_file15,
3041
- L: 228,
3084
+ L: 236,
3042
3085
  S: this,
3043
3086
  A: [
3044
3087
  "this._lifecycleState === LifecycleState.OPEN",
@@ -3064,10 +3107,6 @@ var EchoHost = class extends import_context7.Resource {
3064
3107
  async removeReplicator(replicator) {
3065
3108
  await this._automergeHost.removeReplicator(replicator);
3066
3109
  }
3067
- async getSpaceSyncState(spaceId) {
3068
- const collectionId = deriveCollectionIdFromSpaceId(spaceId);
3069
- return this._automergeHost.getCollectionSyncState(collectionId);
3070
- }
3071
3110
  };
3072
3111
  function _using_ctx() {
3073
3112
  var _disposeSuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed) {
@@ -3485,9 +3524,11 @@ var findInlineObjectOfType = (spaceDoc, typename) => {
3485
3524
  QueryServiceImpl,
3486
3525
  QueryState,
3487
3526
  Space,
3527
+ SpaceDocumentListUpdatedEvent,
3488
3528
  SpaceManager,
3489
3529
  SpaceProtocol,
3490
3530
  SpaceProtocolSession,
3531
+ SpaceStateManager,
3491
3532
  TimeframeClock,
3492
3533
  codec,
3493
3534
  createIdFromSpaceKey,