@tanstack/db-sqlite-persistence-core 0.2.20 → 0.2.21

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.
@@ -386,17 +386,12 @@ class PersistedCollectionRuntime {
386
386
  await this.waitForAppliedReceiptsAfter(appliedCursor);
387
387
  if (upstreamLoadSubset) {
388
388
  try {
389
- const maybePromise = upstreamLoadSubset(options);
390
- if (maybePromise instanceof Promise) {
391
- await maybePromise.catch((error) => {
392
- console.warn(
393
- `Failed to load remote subset in persisted wrapper:`,
394
- error
395
- );
396
- this.queueRemoteSubsetEnsure(options);
397
- });
398
- }
389
+ await upstreamLoadSubset(options);
399
390
  } catch (error) {
391
+ if (options.signal?.aborted || typeof error === `object` && error !== null && `name` in error && error.name === `AbortError`) {
392
+ this.pendingRemoteSubsetEnsures.delete(this.getSubsetKey(options));
393
+ throw error;
394
+ }
400
395
  console.warn(`Failed to trigger remote subset load:`, error);
401
396
  this.queueRemoteSubsetEnsure(options);
402
397
  }
@@ -404,6 +399,7 @@ class PersistedCollectionRuntime {
404
399
  }
405
400
  unloadSubset(options, upstreamUnloadSubset) {
406
401
  this.activeSubsets.delete(this.getSubsetKey(options));
402
+ this.pendingRemoteSubsetEnsures.delete(this.getSubsetKey(options));
407
403
  upstreamUnloadSubset?.(options);
408
404
  }
409
405
  async forceReloadSubset(options) {
@@ -963,7 +959,7 @@ class PersistedCollectionRuntime {
963
959
  return `opts:${stableSerialize(normalizeSubsetOptionsForKey(options))}`;
964
960
  }
965
961
  queueRemoteSubsetEnsure(options) {
966
- if (this.mode !== `sync-present` || !this.persistence.coordinator.requestEnsureRemoteSubset) {
962
+ if (this.mode !== `sync-present` || !this.persistence.coordinator.requestEnsureRemoteSubset || this.activeSubsets.get(this.getSubsetKey(options)) !== options) {
967
963
  return;
968
964
  }
969
965
  this.pendingRemoteSubsetEnsures.set(this.getSubsetKey(options), options);
@@ -1297,23 +1293,7 @@ function createWrappedSyncConfig(sourceSyncConfig, runtime) {
1297
1293
  const transactionStack = [];
1298
1294
  const getOpenTransaction = () => transactionStack[transactionStack.length - 1];
1299
1295
  let fullStartPromise = null;
1300
- const cancelledLoadKeys = /* @__PURE__ */ new Set();
1301
- const loadSubscriptionIds = /* @__PURE__ */ new WeakMap();
1302
- let nextLoadSubscriptionId = 0;
1303
- const getLoadKey = (options) => {
1304
- const subscription = options.subscription;
1305
- if (subscription && typeof subscription === `object`) {
1306
- const existingId = loadSubscriptionIds.get(subscription);
1307
- if (existingId) {
1308
- return `sub:${existingId}`;
1309
- }
1310
- nextLoadSubscriptionId++;
1311
- const nextId = String(nextLoadSubscriptionId);
1312
- loadSubscriptionIds.set(subscription, nextId);
1313
- return `sub:${nextId}`;
1314
- }
1315
- return `opts:${stableSerialize(normalizeSubsetOptionsForKey(options))}`;
1316
- };
1296
+ const acquisitions = /* @__PURE__ */ new Map();
1317
1297
  runtime.setSyncControls({
1318
1298
  begin: params.begin,
1319
1299
  write: params.write,
@@ -1568,23 +1548,40 @@ function createWrappedSyncConfig(sourceSyncConfig, runtime) {
1568
1548
  return {
1569
1549
  cleanup: () => {
1570
1550
  startupState.cleanedUp = true;
1551
+ acquisitions.clear();
1571
1552
  sourceResult.cleanup?.();
1572
1553
  runtime.cleanup();
1573
1554
  runtime.clearSyncControls();
1574
1555
  },
1575
1556
  loadSubset: async (options) => {
1576
- const loadKey = getLoadKey(options);
1577
- cancelledLoadKeys.delete(loadKey);
1557
+ const acquisition = { forwarded: false };
1558
+ acquisitions.set(options, acquisition);
1578
1559
  await fullStartPromise;
1579
1560
  const resolvedSourceResult = await sourceResultPromise;
1580
- if (startupState.cleanedUp || cancelledLoadKeys.has(loadKey)) {
1561
+ if (startupState.cleanedUp || acquisitions.get(options) !== acquisition) {
1581
1562
  return;
1582
1563
  }
1583
- await runtime.loadSubset(options, resolvedSourceResult.loadSubset);
1564
+ return runtime.loadSubset(options, (loadOptions) => {
1565
+ if (startupState.cleanedUp || acquisitions.get(options) !== acquisition) {
1566
+ return true;
1567
+ }
1568
+ if (!resolvedSourceResult.loadSubset) return true;
1569
+ acquisition.forwarded = true;
1570
+ try {
1571
+ return resolvedSourceResult.loadSubset(loadOptions);
1572
+ } catch (error) {
1573
+ acquisition.forwarded = false;
1574
+ throw error;
1575
+ }
1576
+ });
1584
1577
  },
1585
1578
  unloadSubset: (options) => {
1586
- cancelledLoadKeys.add(getLoadKey(options));
1587
- runtime.unloadSubset(options, sourceResult.unloadSubset);
1579
+ const acquisition = acquisitions.get(options);
1580
+ acquisitions.delete(options);
1581
+ runtime.unloadSubset(
1582
+ options,
1583
+ acquisition?.forwarded ? sourceResult.unloadSubset : void 0
1584
+ );
1588
1585
  }
1589
1586
  };
1590
1587
  }