@spacelr/sdk 0.10.6 → 0.10.8

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.
package/dist/index.mjs CHANGED
@@ -2277,6 +2277,9 @@ function isTimelineSourceStats(value) {
2277
2277
  }
2278
2278
 
2279
2279
  // libs/sdk/src/modules/database.module.ts
2280
+ var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
2281
+ var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
2282
+ var DEFAULT_CATCHUP_PAGE_SIZE = 100;
2280
2283
  var Paginator = class {
2281
2284
  constructor(http, basePath, opts) {
2282
2285
  this.http = http;
@@ -2714,6 +2717,54 @@ var CollectionRef = class {
2714
2717
  * aborts the request and silences the AbortError. Gap-recovery state
2715
2718
  * machine lands in Task 6.
2716
2719
  */
2720
+ /**
2721
+ * #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
2722
+ * Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
2723
+ * behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
2724
+ * ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
2725
+ * gathered before the caller delivers `onSnapshot`, so the existing
2726
+ * snapshot-ordering buffer only unblocks live `onChange` after the full
2727
+ * drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
2728
+ * caller's existing snapshot try/catch.
2729
+ */
2730
+ async resolveSnapshotDocs(opts, signal) {
2731
+ if (!opts.catchUp) {
2732
+ const builder = this.find(opts.where);
2733
+ if (opts.sort) builder.sort(opts.sort);
2734
+ if (opts.limit !== void 0) builder.limit(opts.limit);
2735
+ if (opts.select) builder.select(opts.select);
2736
+ const result = await builder.execute(signal);
2737
+ return result.documents;
2738
+ }
2739
+ const asCap = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.floor(v) : def;
2740
+ const asPageSize = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 1 ? Math.floor(v) : def;
2741
+ const maxPages = asCap(opts.catchUp.maxPages, DEFAULT_CATCHUP_MAX_PAGES);
2742
+ const maxDocs = asCap(opts.catchUp.maxDocs, DEFAULT_CATCHUP_MAX_DOCS);
2743
+ const pageSize = asPageSize(opts.limit, DEFAULT_CATCHUP_PAGE_SIZE);
2744
+ const all = [];
2745
+ let cursor;
2746
+ for (let page = 0; page < maxPages && all.length < maxDocs; page++) {
2747
+ const builder = this.find(opts.where).sort({ _id: 1 }).limit(pageSize);
2748
+ if (opts.select) builder.select(opts.select);
2749
+ const result = cursor === void 0 ? await builder.execute(signal) : await builder.after(cursor).execute(signal);
2750
+ const docs = result.documents;
2751
+ if (docs.length === 0) break;
2752
+ const room = maxDocs - all.length;
2753
+ if (docs.length >= room) {
2754
+ all.push(...docs.slice(0, room));
2755
+ break;
2756
+ }
2757
+ all.push(...docs);
2758
+ const more = result.mode === "cursor" ? result.hasMore : result.hasMore ?? docs.length >= pageSize;
2759
+ if (!more) break;
2760
+ const nextCursor = docs[docs.length - 1]._id;
2761
+ if (nextCursor === void 0 || nextCursor === null || cursor !== void 0 && nextCursor <= cursor) {
2762
+ break;
2763
+ }
2764
+ cursor = nextCursor;
2765
+ }
2766
+ return all;
2767
+ }
2717
2768
  async subscribeWithSnapshot(opts) {
2718
2769
  if (!this.realtime) {
2719
2770
  throw new Error("Realtime not available: no RealtimeClient configured");
@@ -2785,12 +2836,7 @@ var CollectionRef = class {
2785
2836
  snapshotAbort = new AbortController();
2786
2837
  let recoveryDocs;
2787
2838
  try {
2788
- const builder = this.find(opts.where);
2789
- if (opts.sort) builder.sort(opts.sort);
2790
- if (opts.limit !== void 0) builder.limit(opts.limit);
2791
- if (opts.select) builder.select(opts.select);
2792
- const result = await builder.execute(snapshotAbort.signal);
2793
- recoveryDocs = result.documents;
2839
+ recoveryDocs = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
2794
2840
  } catch (err) {
2795
2841
  if (!isActive) return;
2796
2842
  if (err instanceof DOMException && err.name === "AbortError") return;
@@ -2874,7 +2920,8 @@ var CollectionRef = class {
2874
2920
  };
2875
2921
  const handleGap = (info) => {
2876
2922
  if (!isActive) return;
2877
- if (info.reason !== "outside-retention-window") return;
2923
+ if (info.reason !== "outside-retention-window" && info.reason !== "replay-truncated")
2924
+ return;
2878
2925
  dispatchRecovery();
2879
2926
  };
2880
2927
  let subscribeAckResolved = false;
@@ -2933,12 +2980,7 @@ var CollectionRef = class {
2933
2980
  currentUnsubscribe = initial.unsubscribe;
2934
2981
  let snapshotDocs;
2935
2982
  try {
2936
- const builder = this.find(opts.where);
2937
- if (opts.sort) builder.sort(opts.sort);
2938
- if (opts.limit !== void 0) builder.limit(opts.limit);
2939
- if (opts.select) builder.select(opts.select);
2940
- const result = await builder.execute(snapshotAbort.signal);
2941
- snapshotDocs = result.documents;
2983
+ snapshotDocs = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
2942
2984
  } catch (err) {
2943
2985
  currentUnsubscribe?.();
2944
2986
  isActive = false;