@dxos/echo-pipeline 0.1.50 → 0.1.51-main.0e88376

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 (30) hide show
  1. package/dist/lib/browser/{chunk-CSFWTLPD.mjs → chunk-HQNN3RQA.mjs} +130 -49
  2. package/dist/lib/browser/chunk-HQNN3RQA.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +2 -2
  6. package/dist/lib/browser/testing/index.mjs.map +1 -1
  7. package/dist/lib/node/index.cjs +136 -55
  8. package/dist/lib/node/index.cjs.map +3 -3
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +144 -63
  11. package/dist/lib/node/testing/index.cjs.map +3 -3
  12. package/dist/types/src/dbhost/snapshot-manager.d.ts +2 -1
  13. package/dist/types/src/dbhost/snapshot-manager.d.ts.map +1 -1
  14. package/dist/types/src/metadata/metadata-store.d.ts +2 -1
  15. package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
  16. package/dist/types/src/space/control-pipeline.d.ts +9 -1
  17. package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
  18. package/dist/types/src/space/data-pipeline.d.ts +3 -0
  19. package/dist/types/src/space/data-pipeline.d.ts.map +1 -1
  20. package/dist/types/src/space/space.d.ts.map +1 -1
  21. package/package.json +30 -30
  22. package/src/dbhost/snapshot-manager.ts +4 -3
  23. package/src/metadata/metadata-store.ts +12 -1
  24. package/src/space/control-pipeline.test.ts +4 -0
  25. package/src/space/control-pipeline.ts +36 -1
  26. package/src/space/data-pipeline.ts +44 -8
  27. package/src/space/space.test.ts +1 -4
  28. package/src/space/space.ts +3 -2
  29. package/src/testing/util.ts +1 -1
  30. package/dist/lib/browser/chunk-CSFWTLPD.mjs.map +0 -7
@@ -121,6 +121,10 @@ var MetadataStore = class {
121
121
  }
122
122
  }
123
123
  _getSpace(spaceKey) {
124
+ var _a;
125
+ if ((_a = this._metadata.identity) == null ? void 0 : _a.haloSpace.key.equals(spaceKey)) {
126
+ return this._metadata.identity.haloSpace;
127
+ }
124
128
  const space = this.spaces.find((space2) => space2.key === spaceKey);
125
129
  assert(space, "Space not found");
126
130
  return space;
@@ -131,7 +135,7 @@ var MetadataStore = class {
131
135
  async clear() {
132
136
  log("clearing all metadata", {}, {
133
137
  file: "metadata-store.ts",
134
- line: 131,
138
+ line: 136,
135
139
  scope: this,
136
140
  callSite: (f, a) => f(...a)
137
141
  });
@@ -152,10 +156,14 @@ var MetadataStore = class {
152
156
  ((_c = (_b = this._metadata).spaces) != null ? _c : _b.spaces = []).push(record);
153
157
  await this._save();
154
158
  }
155
- async setSpaceLatestTimeframe(spaceKey, timeframe) {
159
+ async setSpaceDataLatestTimeframe(spaceKey, timeframe) {
156
160
  this._getSpace(spaceKey).dataTimeframe = timeframe;
157
161
  await this._save();
158
162
  }
163
+ async setSpaceControlLatestTimeframe(spaceKey, timeframe) {
164
+ this._getSpace(spaceKey).controlTimeframe = timeframe;
165
+ await this._save();
166
+ }
159
167
  async setCache(spaceKey, cache) {
160
168
  this._getSpace(spaceKey).cache = cache;
161
169
  await this._save();
@@ -827,6 +835,7 @@ var DatabaseHost = class {
827
835
 
828
836
  // packages/core/echo/echo-pipeline/src/dbhost/snapshot-manager.ts
829
837
  import { trackLeaks } from "@dxos/async";
838
+ import { cancelWithContext } from "@dxos/context";
830
839
  import { timed as timed2 } from "@dxos/debug";
831
840
  import { log as log7 } from "@dxos/log";
832
841
  import { schema as schema4 } from "@dxos/protocols";
@@ -853,7 +862,7 @@ var SnapshotManager = class SnapshotManager2 {
853
862
  snapshot
854
863
  }, {
855
864
  file: "snapshot-manager.ts",
856
- line: 30,
865
+ line: 31,
857
866
  scope: this,
858
867
  callSite: (f, a) => f(...a)
859
868
  });
@@ -870,7 +879,7 @@ var SnapshotManager = class SnapshotManager2 {
870
879
  data
871
880
  }, {
872
881
  file: "snapshot-manager.ts",
873
- line: 40,
882
+ line: 41,
874
883
  scope: this,
875
884
  callSite: (f, a) => f(...a)
876
885
  });
@@ -888,12 +897,12 @@ var SnapshotManager = class SnapshotManager2 {
888
897
  async close() {
889
898
  await this._objectSync.close();
890
899
  }
891
- async load(id) {
892
- const local = await this._snapshotStore.loadSnapshot(id);
900
+ async load(ctx, id) {
901
+ const local = await cancelWithContext(ctx, this._snapshotStore.loadSnapshot(id));
893
902
  if (local) {
894
903
  return local;
895
904
  }
896
- const remote = await this._objectSync.download(id);
905
+ const remote = await cancelWithContext(ctx, this._objectSync.download(ctx, id));
897
906
  return schema4.getCodecForType("dxos.echo.snapshot.SpaceSnapshot").decode(remote.payload.value);
898
907
  }
899
908
  async store(snapshot) {
@@ -1017,6 +1026,7 @@ import { Event as Event4, scheduleTask as scheduleTask2, synchronized as synchro
1017
1026
  import { Context as Context3 } from "@dxos/context";
1018
1027
  import { checkCredentialType } from "@dxos/credentials";
1019
1028
  import { getStateMachineFromItem, ItemManager } from "@dxos/echo-db";
1029
+ import { CancelledError } from "@dxos/errors";
1020
1030
  import { log as log9 } from "@dxos/log";
1021
1031
  import { Timeframe as Timeframe3 } from "@dxos/timeframe";
1022
1032
  var __decorate5 = function(decorators, target, key, desc) {
@@ -1040,6 +1050,7 @@ var DataPipeline = class DataPipeline2 {
1040
1050
  this._isOpen = false;
1041
1051
  this._lastTimeframeSaveTime = 0;
1042
1052
  this._lastSnapshotSaveTime = 0;
1053
+ this._lastProcessedEpoch = -1;
1043
1054
  this.onNewEpoch = new Event4();
1044
1055
  }
1045
1056
  get isOpen() {
@@ -1063,17 +1074,10 @@ var DataPipeline = class DataPipeline2 {
1063
1074
  if (!checkCredentialType(credential, "dxos.halo.credentials.Epoch")) {
1064
1075
  return;
1065
1076
  }
1066
- log9("new epoch", {
1067
- credential
1068
- }, {
1069
- file: "data-pipeline.ts",
1070
- line: 109,
1071
- scope: this,
1072
- callSite: (f, a) => f(...a)
1073
- });
1074
- await this._processEpoch(credential.subject.assertion);
1075
1077
  this.currentEpoch = credential;
1076
- this.onNewEpoch.emit(credential);
1078
+ if (this._isOpen) {
1079
+ await this._processEpochInSeparateTask(credential);
1080
+ }
1077
1081
  }
1078
1082
  };
1079
1083
  }
@@ -1112,7 +1116,7 @@ var DataPipeline = class DataPipeline2 {
1112
1116
  }
1113
1117
  log9("close", {}, {
1114
1118
  file: "data-pipeline.ts",
1115
- line: 161,
1119
+ line: 164,
1116
1120
  scope: this,
1117
1121
  callSite: (f, a) => f(...a)
1118
1122
  });
@@ -1127,7 +1131,7 @@ var DataPipeline = class DataPipeline2 {
1127
1131
  } catch (err) {
1128
1132
  log9.catch(err, {}, {
1129
1133
  file: "data-pipeline.ts",
1130
- line: 174,
1134
+ line: 177,
1131
1135
  scope: this,
1132
1136
  callSite: (f, a) => f(...a)
1133
1137
  });
@@ -1137,6 +1141,11 @@ var DataPipeline = class DataPipeline2 {
1137
1141
  await this._params.snapshotManager.close();
1138
1142
  }
1139
1143
  async _consumePipeline() {
1144
+ if (this.currentEpoch) {
1145
+ const waitForOneEpoch = this.onNewEpoch.waitFor(() => true);
1146
+ await this._processEpochInSeparateTask(this.currentEpoch);
1147
+ await waitForOneEpoch;
1148
+ }
1140
1149
  assert8(this._pipeline, "Pipeline is not initialized.");
1141
1150
  for await (const msg of this._pipeline.consume()) {
1142
1151
  const { feedKey, seq, data } = msg;
@@ -1145,7 +1154,7 @@ var DataPipeline = class DataPipeline2 {
1145
1154
  seq
1146
1155
  }, {
1147
1156
  file: "data-pipeline.ts",
1148
- line: 186,
1157
+ line: 195,
1149
1158
  scope: this,
1150
1159
  callSite: (f, a) => f(...a)
1151
1160
  });
@@ -1157,7 +1166,7 @@ var DataPipeline = class DataPipeline2 {
1157
1166
  feedKey
1158
1167
  }, {
1159
1168
  file: "data-pipeline.ts",
1160
- line: 192,
1169
+ line: 201,
1161
1170
  scope: this,
1162
1171
  callSite: (f, a) => f(...a)
1163
1172
  });
@@ -1177,7 +1186,7 @@ var DataPipeline = class DataPipeline2 {
1177
1186
  } catch (err) {
1178
1187
  log9.catch(err, {}, {
1179
1188
  file: "data-pipeline.ts",
1180
- line: 210,
1189
+ line: 219,
1181
1190
  scope: this,
1182
1191
  callSite: (f, a) => f(...a)
1183
1192
  });
@@ -1195,7 +1204,7 @@ var DataPipeline = class DataPipeline2 {
1195
1204
  async _saveTargetTimeframe(timeframe) {
1196
1205
  var _a;
1197
1206
  const newTimeframe = Timeframe3.merge((_a = this._targetTimeframe) != null ? _a : new Timeframe3(), timeframe);
1198
- await this._params.metadataStore.setSpaceLatestTimeframe(this._params.spaceKey, newTimeframe);
1207
+ await this._params.metadataStore.setSpaceDataLatestTimeframe(this._params.spaceKey, newTimeframe);
1199
1208
  this._targetTimeframe = newTimeframe;
1200
1209
  }
1201
1210
  async _saveCache() {
@@ -1212,7 +1221,7 @@ var DataPipeline = class DataPipeline2 {
1212
1221
  } catch (err) {
1213
1222
  log9.warn("Failed to cache properties", err, {
1214
1223
  file: "data-pipeline.ts",
1215
- line: 244,
1224
+ line: 253,
1216
1225
  scope: this,
1217
1226
  callSite: (f, a) => f(...a)
1218
1227
  });
@@ -1228,24 +1237,67 @@ var DataPipeline = class DataPipeline2 {
1228
1237
  await this._saveCache();
1229
1238
  }
1230
1239
  }
1231
- async _processEpoch(epoch) {
1232
- assert8(this._isOpen);
1240
+ async _processEpochInSeparateTask(epoch) {
1241
+ var _a;
1242
+ if (epoch.subject.assertion.number <= this._lastProcessedEpoch) {
1243
+ return;
1244
+ }
1245
+ await ((_a = this._epochCtx) == null ? void 0 : _a.dispose());
1246
+ const ctx = new Context3({
1247
+ onError: (err) => {
1248
+ if (err instanceof CancelledError) {
1249
+ log9("Epoch processing cancelled.", {}, {
1250
+ file: "data-pipeline.ts",
1251
+ line: 285,
1252
+ scope: this,
1253
+ callSite: (f, a) => f(...a)
1254
+ });
1255
+ } else {
1256
+ log9.catch(err, {}, {
1257
+ file: "data-pipeline.ts",
1258
+ line: 287,
1259
+ scope: this,
1260
+ callSite: (f, a) => f(...a)
1261
+ });
1262
+ }
1263
+ }
1264
+ });
1265
+ this._epochCtx = ctx;
1266
+ scheduleTask2(ctx, async () => {
1267
+ if (!this._isOpen) {
1268
+ return;
1269
+ }
1270
+ log9("process epoch", {
1271
+ epoch
1272
+ }, {
1273
+ file: "data-pipeline.ts",
1274
+ line: 297,
1275
+ scope: this,
1276
+ callSite: (f, a) => f(...a)
1277
+ });
1278
+ await this._processEpoch(ctx, epoch.subject.assertion);
1279
+ this.onNewEpoch.emit(epoch);
1280
+ });
1281
+ }
1282
+ async _processEpoch(ctx, epoch) {
1283
+ assert8(this._isOpen, "Space is closed.");
1233
1284
  assert8(this._pipeline);
1285
+ this._lastProcessedEpoch = epoch.number;
1234
1286
  log9("Processing epoch", {
1235
1287
  epoch
1236
1288
  }, {
1237
1289
  file: "data-pipeline.ts",
1238
- line: 273,
1290
+ line: 309,
1239
1291
  scope: this,
1240
1292
  callSite: (f, a) => f(...a)
1241
1293
  });
1242
1294
  if (epoch.snapshotCid) {
1243
- const snapshot = await this._params.snapshotManager.load(epoch.snapshotCid);
1295
+ const snapshot = await this._params.snapshotManager.load(ctx, epoch.snapshotCid);
1244
1296
  this.databaseHost._itemDemuxer.restoreFromSnapshot(snapshot.database);
1245
1297
  }
1246
1298
  log9("restarting pipeline for epoch", {}, {
1247
1299
  file: "data-pipeline.ts",
1248
- line: 281,
1300
+ line: 317,
1249
1301
  scope: this,
1250
1302
  callSite: (f, a) => f(...a)
1251
1303
  });
@@ -1304,10 +1356,15 @@ import { SpaceStateMachine } from "@dxos/credentials";
1304
1356
  import { PublicKey as PublicKey3 } from "@dxos/keys";
1305
1357
  import { log as log10 } from "@dxos/log";
1306
1358
  import { AdmittedFeed } from "@dxos/protocols/proto/dxos/halo/credentials";
1359
+ import { Timeframe as Timeframe4 } from "@dxos/timeframe";
1307
1360
  import { Callback } from "@dxos/util";
1361
+ var TIMEFRAME_SAVE_DEBOUNCE_INTERVAL2 = 500;
1308
1362
  var ControlPipeline = class {
1309
- constructor({ spaceKey, genesisFeed, feedProvider }) {
1363
+ constructor({ spaceKey, genesisFeed, feedProvider, metadataStore }) {
1364
+ this._lastTimeframeSaveTime = Date.now();
1310
1365
  this.onFeedAdmitted = new Callback();
1366
+ this._spaceKey = spaceKey;
1367
+ this._metadata = metadataStore;
1311
1368
  this._pipeline = new Pipeline();
1312
1369
  void this._pipeline.addFeed(genesisFeed);
1313
1370
  this._spaceStateMachine = new SpaceStateMachine(spaceKey);
@@ -1316,7 +1373,7 @@ var ControlPipeline = class {
1316
1373
  key: info.key
1317
1374
  }, {
1318
1375
  file: "control-pipeline.ts",
1319
- line: 39,
1376
+ line: 51,
1320
1377
  scope: this,
1321
1378
  callSite: (f, a) => f(...a)
1322
1379
  });
@@ -1327,7 +1384,7 @@ var ControlPipeline = class {
1327
1384
  } catch (err) {
1328
1385
  log10.catch(err, {}, {
1329
1386
  file: "control-pipeline.ts",
1330
- line: 47,
1387
+ line: 59,
1331
1388
  scope: this,
1332
1389
  callSite: (f, a) => f(...a)
1333
1390
  });
@@ -1350,7 +1407,7 @@ var ControlPipeline = class {
1350
1407
  async start() {
1351
1408
  log10("starting...", {}, {
1352
1409
  file: "control-pipeline.ts",
1353
- line: 71,
1410
+ line: 83,
1354
1411
  scope: this,
1355
1412
  callSite: (f, a) => f(...a)
1356
1413
  });
@@ -1362,7 +1419,7 @@ var ControlPipeline = class {
1362
1419
  seq: msg.seq
1363
1420
  }, {
1364
1421
  file: "control-pipeline.ts",
1365
- line: 76,
1422
+ line: 88,
1366
1423
  scope: this,
1367
1424
  callSite: (f, a) => f(...a)
1368
1425
  });
@@ -1373,16 +1430,18 @@ var ControlPipeline = class {
1373
1430
  msg
1374
1431
  }, {
1375
1432
  file: "control-pipeline.ts",
1376
- line: 83,
1433
+ line: 95,
1377
1434
  scope: this,
1378
1435
  callSite: (f, a) => f(...a)
1379
1436
  });
1437
+ } else {
1438
+ await this._noteTargetStateIfNeeded(this._pipeline.state.pendingTimeframe);
1380
1439
  }
1381
1440
  }
1382
1441
  } catch (err) {
1383
1442
  log10.catch(err, {}, {
1384
1443
  file: "control-pipeline.ts",
1385
- line: 87,
1444
+ line: 101,
1386
1445
  scope: this,
1387
1446
  callSite: (f, a) => f(...a)
1388
1447
  });
@@ -1392,26 +1451,48 @@ var ControlPipeline = class {
1392
1451
  await this._pipeline.start();
1393
1452
  log10("started", {}, {
1394
1453
  file: "control-pipeline.ts",
1395
- line: 93,
1454
+ line: 107,
1396
1455
  scope: this,
1397
1456
  callSite: (f, a) => f(...a)
1398
1457
  });
1399
1458
  }
1459
+ async _noteTargetStateIfNeeded(timeframe) {
1460
+ if (Date.now() - this._lastTimeframeSaveTime > TIMEFRAME_SAVE_DEBOUNCE_INTERVAL2) {
1461
+ this._lastTimeframeSaveTime = Date.now();
1462
+ await this._saveTargetTimeframe(timeframe);
1463
+ }
1464
+ }
1400
1465
  async stop() {
1401
1466
  log10("stopping...", {}, {
1402
1467
  file: "control-pipeline.ts",
1403
- line: 97,
1468
+ line: 121,
1404
1469
  scope: this,
1405
1470
  callSite: (f, a) => f(...a)
1406
1471
  });
1407
1472
  await this._pipeline.stop();
1473
+ await this._saveTargetTimeframe(this._pipeline.state.timeframe);
1408
1474
  log10("stopped", {}, {
1409
1475
  file: "control-pipeline.ts",
1410
- line: 99,
1476
+ line: 124,
1411
1477
  scope: this,
1412
1478
  callSite: (f, a) => f(...a)
1413
1479
  });
1414
1480
  }
1481
+ async _saveTargetTimeframe(timeframe) {
1482
+ var _a;
1483
+ try {
1484
+ const newTimeframe = Timeframe4.merge((_a = this._targetTimeframe) != null ? _a : new Timeframe4(), timeframe);
1485
+ await this._metadata.setSpaceControlLatestTimeframe(this._spaceKey, newTimeframe);
1486
+ this._targetTimeframe = newTimeframe;
1487
+ } catch (err) {
1488
+ log10(err, {}, {
1489
+ file: "control-pipeline.ts",
1490
+ line: 133,
1491
+ scope: this,
1492
+ callSite: (f, a) => f(...a)
1493
+ });
1494
+ }
1495
+ }
1415
1496
  };
1416
1497
 
1417
1498
  // packages/core/echo/echo-pipeline/src/space/space.ts
@@ -1441,7 +1522,8 @@ var Space = class Space2 {
1441
1522
  this._controlPipeline = new ControlPipeline({
1442
1523
  spaceKey: params.spaceKey,
1443
1524
  genesisFeed: params.genesisFeed,
1444
- feedProvider: params.feedProvider
1525
+ feedProvider: params.feedProvider,
1526
+ metadataStore: params.metadataStore
1445
1527
  });
1446
1528
  this._controlPipeline.onFeedAdmitted.set(async (info) => {
1447
1529
  if (info.assertion.designation === AdmittedFeed2.Designation.DATA) {
@@ -1463,7 +1545,7 @@ var Space = class Space2 {
1463
1545
  credential
1464
1546
  }, {
1465
1547
  file: "space.ts",
1466
- line: 106,
1548
+ line: 107,
1467
1549
  scope: this,
1468
1550
  callSite: (f, a) => f(...a)
1469
1551
  });
@@ -1553,7 +1635,7 @@ var Space = class Space2 {
1553
1635
  async open() {
1554
1636
  log11("opening...", {}, {
1555
1637
  file: "space.ts",
1556
- line: 208,
1638
+ line: 209,
1557
1639
  scope: this,
1558
1640
  callSite: (f, a) => f(...a)
1559
1641
  });
@@ -1563,10 +1645,11 @@ var Space = class Space2 {
1563
1645
  await this._controlPipeline.start();
1564
1646
  await this.protocol.start();
1565
1647
  this._dataPipelineCredentialConsumer = this._controlPipeline.spaceState.registerProcessor(this._dataPipeline.createCredentialProcessor());
1648
+ await this._dataPipelineCredentialConsumer.open();
1566
1649
  this._isOpen = true;
1567
1650
  log11("opened", {}, {
1568
1651
  file: "space.ts",
1569
- line: 222,
1652
+ line: 225,
1570
1653
  scope: this,
1571
1654
  callSite: (f, a) => f(...a)
1572
1655
  });
@@ -1577,7 +1660,7 @@ var Space = class Space2 {
1577
1660
  key: this._key
1578
1661
  }, {
1579
1662
  file: "space.ts",
1580
- line: 227,
1663
+ line: 230,
1581
1664
  scope: this,
1582
1665
  callSite: (f, a) => f(...a)
1583
1666
  });
@@ -1592,7 +1675,7 @@ var Space = class Space2 {
1592
1675
  this._isOpen = false;
1593
1676
  log11("closed", {}, {
1594
1677
  file: "space.ts",
1595
- line: 241,
1678
+ line: 244,
1596
1679
  scope: this,
1597
1680
  callSite: (f, a) => f(...a)
1598
1681
  });
@@ -1600,14 +1683,12 @@ var Space = class Space2 {
1600
1683
  async initializeDataPipeline() {
1601
1684
  log11("initializeDataPipeline", {}, {
1602
1685
  file: "space.ts",
1603
- line: 246,
1686
+ line: 249,
1604
1687
  scope: this,
1605
1688
  callSite: (f, a) => f(...a)
1606
1689
  });
1607
1690
  assert9(this._isOpen, "Space must be open to initialize data pipeline.");
1608
1691
  await this._dataPipeline.open();
1609
- assert9(this._dataPipelineCredentialConsumer);
1610
- await this._dataPipelineCredentialConsumer.open();
1611
1692
  }
1612
1693
  };
1613
1694
  __decorate6([
@@ -1934,4 +2015,4 @@ export {
1934
2015
  SpaceProtocolSession,
1935
2016
  SpaceManager
1936
2017
  };
1937
- //# sourceMappingURL=chunk-CSFWTLPD.mjs.map
2018
+ //# sourceMappingURL=chunk-HQNN3RQA.mjs.map