next
Version:
The React Framework
66 lines (65 loc) • 2.92 kB
JavaScript
import { getCacheHandlers } from './use-cache/handlers';
/** Run a callback, and execute any *new* revalidations added during its runtime. */ export async function withExecuteRevalidates(store, callback) {
if (!store) {
return callback();
}
// If we executed any revalidates during the request, then we don't want to execute them again.
// save the state so we can check if anything changed after we're done running callbacks.
const savedRevalidationState = cloneRevalidationState(store);
try {
return await callback();
} finally{
// Check if we have any new revalidates, and if so, wait until they are all resolved.
const newRevalidates = diffRevalidationState(savedRevalidationState, cloneRevalidationState(store));
await executeRevalidates(store, newRevalidates);
}
}
function cloneRevalidationState(store) {
return {
pendingRevalidatedTags: store.pendingRevalidatedTags ? [
...store.pendingRevalidatedTags
] : [],
pendingRevalidates: {
...store.pendingRevalidates
},
pendingRevalidateWrites: store.pendingRevalidateWrites ? [
...store.pendingRevalidateWrites
] : []
};
}
function diffRevalidationState(prev, curr) {
const prevTags = new Set(prev.pendingRevalidatedTags);
const prevRevalidateWrites = new Set(prev.pendingRevalidateWrites);
return {
pendingRevalidatedTags: curr.pendingRevalidatedTags.filter((tag)=>!prevTags.has(tag)),
pendingRevalidates: Object.fromEntries(Object.entries(curr.pendingRevalidates).filter(([key])=>!(key in prev.pendingRevalidates))),
pendingRevalidateWrites: curr.pendingRevalidateWrites.filter((promise)=>!prevRevalidateWrites.has(promise))
};
}
async function revalidateTags(tags, incrementalCache) {
if (tags.length === 0) {
return;
}
const promises = [];
if (incrementalCache) {
promises.push(incrementalCache.revalidateTag(tags));
}
const handlers = getCacheHandlers();
if (handlers) {
for (const handler of handlers){
promises.push(handler.expireTags(...tags));
}
}
await Promise.all(promises);
}
export async function executeRevalidates(workStore, state) {
const pendingRevalidatedTags = (state == null ? void 0 : state.pendingRevalidatedTags) ?? workStore.pendingRevalidatedTags ?? [];
const pendingRevalidates = (state == null ? void 0 : state.pendingRevalidates) ?? workStore.pendingRevalidates ?? {};
const pendingRevalidateWrites = (state == null ? void 0 : state.pendingRevalidateWrites) ?? workStore.pendingRevalidateWrites ?? [];
return Promise.all([
revalidateTags(pendingRevalidatedTags, workStore.incrementalCache),
...Object.values(pendingRevalidates),
...pendingRevalidateWrites
]);
}
//# sourceMappingURL=revalidation-utils.js.map