@resolveio/server-lib 20.9.7 → 20.9.9

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.
@@ -68,6 +68,7 @@ export declare class SubscriptionManager {
68
68
  private documentMatchesFilter;
69
69
  private shouldInvalidateSubscription;
70
70
  private shouldInvalidateSubscriptionForEvents;
71
+ private getPaginationMeta;
71
72
  private sendWS;
72
73
  getEnableDebug(): boolean;
73
74
  private dependencyDebug;
@@ -732,7 +732,8 @@ var SubscriptionManager = /** @class */ (function () {
732
732
  runAgain: false,
733
733
  collectionDependencies: new Map(),
734
734
  collectionFilters: new Map(),
735
- watchAllCollections: new Set()
735
+ watchAllCollections: new Set(),
736
+ collectionQueryMeta: new Map()
736
737
  });
737
738
  }
738
739
  if (!sub) {
@@ -1368,6 +1369,9 @@ var SubscriptionManager = /** @class */ (function () {
1368
1369
  if (!sub.watchAllCollections) {
1369
1370
  sub.watchAllCollections = new Set();
1370
1371
  }
1372
+ if (!sub.collectionQueryMeta) {
1373
+ sub.collectionQueryMeta = new Map();
1374
+ }
1371
1375
  };
1372
1376
  SubscriptionManager.prototype.updateSubscriptionDependencies = function (sub, snapshot) {
1373
1377
  this.ensureDependencyContainers(sub);
@@ -1375,12 +1379,14 @@ var SubscriptionManager = /** @class */ (function () {
1375
1379
  sub.collectionDependencies.clear();
1376
1380
  sub.collectionFilters.clear();
1377
1381
  sub.watchAllCollections.clear();
1382
+ sub.collectionQueryMeta.clear();
1378
1383
  this.dependencyDebug('Cleared dependency snapshot', { publication: sub.publication });
1379
1384
  return;
1380
1385
  }
1381
1386
  sub.collectionDependencies = snapshot.dependencies;
1382
1387
  sub.collectionFilters = snapshot.filters;
1383
1388
  sub.watchAllCollections = snapshot.watchAllCollections;
1389
+ sub.collectionQueryMeta = snapshot.queryMetadata;
1384
1390
  this.logDependencySnapshot(sub, 'Snapshot updated');
1385
1391
  };
1386
1392
  SubscriptionManager.prototype.normalizeDocumentId = function (rawId) {
@@ -1506,43 +1512,89 @@ var SubscriptionManager = /** @class */ (function () {
1506
1512
  };
1507
1513
  SubscriptionManager.prototype.shouldInvalidateSubscriptionForEvents = function (sub, collection, events) {
1508
1514
  return __awaiter(this, void 0, void 0, function () {
1509
- var events_1, events_1_1, event_1, normalizedDocumentId, e_7_1;
1515
+ var sawInsert, sawDelete, events_1, events_1_1, event_1, normalizedDocumentId, e_7_1, paginationMeta, paginationReasons;
1510
1516
  var e_7, _a;
1511
1517
  return __generator(this, function (_b) {
1512
1518
  switch (_b.label) {
1513
1519
  case 0:
1514
- _b.trys.push([0, 5, 6, 7]);
1515
- events_1 = __values(events), events_1_1 = events_1.next();
1520
+ sawInsert = false;
1521
+ sawDelete = false;
1516
1522
  _b.label = 1;
1517
1523
  case 1:
1518
- if (!!events_1_1.done) return [3 /*break*/, 4];
1524
+ _b.trys.push([1, 6, 7, 8]);
1525
+ events_1 = __values(events), events_1_1 = events_1.next();
1526
+ _b.label = 2;
1527
+ case 2:
1528
+ if (!!events_1_1.done) return [3 /*break*/, 5];
1519
1529
  event_1 = events_1_1.value;
1520
1530
  normalizedDocumentId = this.normalizeDocumentId(event_1.documentId);
1531
+ if (event_1.type === 'insert') {
1532
+ sawInsert = true;
1533
+ }
1534
+ else if (event_1.type === 'delete') {
1535
+ sawDelete = true;
1536
+ }
1521
1537
  return [4 /*yield*/, this.shouldInvalidateSubscription(sub, collection, event_1.type, normalizedDocumentId)];
1522
- case 2:
1538
+ case 3:
1523
1539
  if (_b.sent()) {
1524
1540
  return [2 /*return*/, true];
1525
1541
  }
1526
- _b.label = 3;
1527
- case 3:
1542
+ _b.label = 4;
1543
+ case 4:
1528
1544
  events_1_1 = events_1.next();
1529
- return [3 /*break*/, 1];
1530
- case 4: return [3 /*break*/, 7];
1531
- case 5:
1545
+ return [3 /*break*/, 2];
1546
+ case 5: return [3 /*break*/, 8];
1547
+ case 6:
1532
1548
  e_7_1 = _b.sent();
1533
1549
  e_7 = { error: e_7_1 };
1534
- return [3 /*break*/, 7];
1535
- case 6:
1550
+ return [3 /*break*/, 8];
1551
+ case 7:
1536
1552
  try {
1537
1553
  if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
1538
1554
  }
1539
1555
  finally { if (e_7) throw e_7.error; }
1540
1556
  return [7 /*endfinally*/];
1541
- case 7: return [2 /*return*/, false];
1557
+ case 8:
1558
+ paginationMeta = this.getPaginationMeta(sub, collection);
1559
+ paginationReasons = [];
1560
+ if (sawInsert) {
1561
+ paginationReasons.push('insert');
1562
+ }
1563
+ if (sawDelete) {
1564
+ paginationReasons.push('delete');
1565
+ }
1566
+ if ((paginationMeta === null || paginationMeta === void 0 ? void 0 : paginationMeta.length) && paginationReasons.length) {
1567
+ this.dependencyDebug('Invalidate due to pagination metadata', {
1568
+ publication: sub.publication,
1569
+ collection: collection,
1570
+ events: this.summarizeEvents(events),
1571
+ reasons: paginationReasons,
1572
+ queryMeta: paginationMeta.slice(0, 5)
1573
+ });
1574
+ return [2 /*return*/, true];
1575
+ }
1576
+ return [2 /*return*/, false];
1542
1577
  }
1543
1578
  });
1544
1579
  });
1545
1580
  };
1581
+ SubscriptionManager.prototype.getPaginationMeta = function (sub, collection) {
1582
+ var _a;
1583
+ this.ensureDependencyContainers(sub);
1584
+ var metaList = (_a = sub.collectionQueryMeta) === null || _a === void 0 ? void 0 : _a.get(collection);
1585
+ if (!metaList || !metaList.length) {
1586
+ return null;
1587
+ }
1588
+ var relevantMeta = metaList.filter(function (meta) {
1589
+ if (!meta) {
1590
+ return false;
1591
+ }
1592
+ var limitUsed = typeof meta.limit === 'number' && meta.limit > 0;
1593
+ var skipUsed = typeof meta.skip === 'number' && meta.skip > 0;
1594
+ return limitUsed || skipUsed;
1595
+ });
1596
+ return relevantMeta.length ? relevantMeta : null;
1597
+ };
1546
1598
  SubscriptionManager.prototype.sendWS = function (ws, data) {
1547
1599
  this._websocketManager.send(ws, data);
1548
1600
  };
@@ -1587,12 +1639,21 @@ var SubscriptionManager = /** @class */ (function () {
1587
1639
  count: filters.length
1588
1640
  });
1589
1641
  });
1642
+ var queryMetaSummary = Array.from(sub.collectionQueryMeta.entries()).map(function (_a) {
1643
+ var _b = __read(_a, 2), collectionName = _b[0], metaList = _b[1];
1644
+ return ({
1645
+ collection: collectionName,
1646
+ count: metaList.length,
1647
+ meta: metaList.slice(0, 5)
1648
+ });
1649
+ });
1590
1650
  var watchAll = Array.from(sub.watchAllCollections || []);
1591
1651
  this.dependencyDebug('Dependency snapshot updated', {
1592
1652
  context: context,
1593
1653
  publication: sub.publication,
1594
1654
  dependencies: dependencySummary,
1595
1655
  collectionsWithFilters: filterSummary,
1656
+ collectionsWithMeta: queryMetaSummary,
1596
1657
  watchAll: watchAll
1597
1658
  });
1598
1659
  };