Skip to content

Commit 500e253

Browse files
committed
Fix parameterized collection caching and in-flight request tracking
- Compute params key directly in _startCollectionFetch to ensure parameterized collections (e.g., { status: 'active' }) are tracked separately from non-parameterized collections for in-flight request coalescing - Add paramsKeyMismatch check to force fetch when ref's paramsKey doesn't match - Add neverFetchedForParams check to ensure new parameterized refs always fetch - Fix isCollectionLoading to use correct key format (was using undefined collectionKey) This fixes the issue where calling col('invoices') followed by col('invoices', { params: { status: 'active' } }) would incorrectly skip the second fetch because in-flight tracking wasn't properly distinguishing parameterized collections.
1 parent 827812b commit 500e253

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

verity/shared/static/lib/core.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ function isItemLoading(typeName, id, levelName = null) {
12421242
}
12431243

12441244
function isCollectionLoading(name, params) {
1245-
const key = collectionKey(name, params);
1245+
const key = `${name}::${paramsKey(params)}`;
12461246
return G.inFlightCol.has(key);
12471247
}
12481248

@@ -1515,8 +1515,12 @@ async function flushBulkQueue(queueKey) {
15151515
async function _startCollectionFetch(name, { force = false, params = undefined } = {}) {
15161516
const C = G.collections.get(name); if (!C) throw new Error(`Unknown collection '${name}'`);
15171517
const { fetch, stalenessMs } = C;
1518-
const { ref, key } = ensureCollectionRefEntry(name, params);
1519-
const inFlightKey = `${name}::${key}`;
1518+
// Compute the params key directly to ensure correct in-flight tracking for parameterized collections
1519+
// This ensures parameterized collections (e.g., { status: 'active' }) are tracked separately from
1520+
// non-parameterized collections, even when requests are in-flight
1521+
const effectiveParamsKey = paramsKey(params);
1522+
const { ref } = ensureCollectionRefEntry(name, params);
1523+
const inFlightKey = `${name}::${effectiveParamsKey}`;
15201524
const snapshot = cloneParams(params ?? ref.meta.paramsSnapshot ?? {});
15211525

15221526
if (G.inFlightCol.has(inFlightKey)) {
@@ -1527,7 +1531,16 @@ async function _startCollectionFetch(name, { force = false, params = undefined }
15271531
return bucket.promise;
15281532
}
15291533

1530-
const shouldFetch = force || isStale(ref.meta.lastFetched, stalenessMs);
1534+
// Verify that the ref's paramsKey matches the expected key
1535+
// If they don't match, the ref's lastFetched is for different params, so we must fetch
1536+
const refParamsKey = ref.meta && ref.meta.paramsKey ? ref.meta.paramsKey : PARAM_DEFAULT_KEY;
1537+
const paramsKeyMismatch = refParamsKey !== effectiveParamsKey;
1538+
1539+
// Also check if the ref has never been fetched for these specific params
1540+
// This handles the case where a new parameterized ref should always fetch
1541+
const neverFetchedForParams = !ref.meta.lastFetched;
1542+
1543+
const shouldFetch = force || paramsKeyMismatch || neverFetchedForParams || isStale(ref.meta.lastFetched, stalenessMs);
15311544
if (!shouldFetch) {
15321545
if (ref.meta.isLoading) assignRef(ref, { meta: { ...ref.meta, isLoading: false } });
15331546
emitLifecycle("collection:fetch:skip", { name, params: snapshot, reason: "fresh" });

0 commit comments

Comments
 (0)