The refetched product
Resolves once the caches are updated
await patchCachedProduct(result.product);
export async function patchCachedProduct(fresh: Product): Promise<void> {
const key = productSeriesKey(fresh);
if (key === undefined) return;
// The product-data cache is keyed by the same supplier + identity hash.
const cached = await getSupplierProductDataCacheEntry(key);
if (cached && isCachedProductData(cached.data)) {
await putSupplierProductDataCacheEntry(key, {
...cached,
data: mergeFreshPrices(cached.data, fresh, false),
});
}
for (const { cacheKey, ...entry } of await getAllSupplierQueryCacheEntries()) {
if (entry.__cacheMetadata.supplier !== fresh.supplier) continue;
const isCopy = (row: unknown): row is Partial<Product> =>
isCachedProductData(row) && row.cacheKey === fresh.cacheKey;
if (!entry.data.some(isCopy)) continue;
await putSupplierQueryCacheEntry(cacheKey, {
...entry,
data: entry.data.map((row) => (isCopy(row) ? mergeFreshPrices(row, fresh, false) : row)),
});
}
const results = await getSearchResultsRecord();
if (!results.data.some((row) => productSeriesKey(row) === key)) return;
await setSearchResults(
results.data.map((row) =>
productSeriesKey(row) === key ? mergeFreshPrices(row, fresh, true) : row,
),
results.query,
);
}
Updates every cached copy of one product with its fresh prices: the product-data cache, the cached search listings it appears in, and the results on screen. Other cached data and cache ages are left alone, so this doesn't extend how long a search stays cached.