You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have created a utility function to update the query cache based on some patch function. I want this utility function to do the following:
Get all queries for a given endpoint from the state.
Iterate over each of them:
If the query has an active subscription, the query is updated using a provided patch function.
If the query has no active subscription, the query gets removed from the cache entirely.
This function is used when doing optimistic updates: instantly update all active queries with some patch function and purge the inactive queries for that endpoint so they won't be re-used in the future.
However, I haven't found a way to purge cache entries manually from the state. I don't want to invalidate tags because that causes a refetch of all the active queries as well which makes no sense cause we are optimistically updating those already. Is there a way to purge specific queries from the cache? Or do you see a better approach to what I am trying to do?
The utility function is a work in progress but looks something like this:
export function updateQueryCache<R>({
getState,
dispatch,
api,
endpointName,
patchFunction,
}: UpdateQueryCacheArgs<R>): PatchResult[] {
const state = getState();
const allQueries = Object.entries(state.api.queries);
const subscriptions = state.api.subscriptions;
const endpointQueries = allQueries.filter(([key]) => key.startsWith(`${endpointName}(`));
const patchResults: PatchResult[] = [];
endpointQueries.forEach(([queryKey, query]) => {
if (!query) return;
// Check if this query has active subscriptions
const hasActiveSubscriptions =
subscriptions[queryKey] &&
Object.keys(subscriptions[queryKey] as Record<string, unknown>).length > 0;
// If The query has an active subscription then we want to update it using the patch function.
// If the query has NO active subscription then we want to remove it from the cache which is
// less work then patching it. If the query becomes active again it will be refetched instead of
// re-used from the cache. This is safeguard against having to update many outdated cache
// entries which will never be re-used (aka a performance optimization).
if (hasActiveSubscriptions) {
const patchResult = dispatch(
api.util.updateQueryData(
endpointName,
(query as { originalArgs: unknown }).originalArgs,
patchFunction,
),
) as PatchResult;
patchResults.push(patchResult);
} else {
// TODO: how to remove cache entries?
}
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have created a utility function to update the query cache based on some patch function. I want this utility function to do the following:
This function is used when doing optimistic updates: instantly update all active queries with some patch function and purge the inactive queries for that endpoint so they won't be re-used in the future.
However, I haven't found a way to purge cache entries manually from the state. I don't want to invalidate tags because that causes a refetch of all the active queries as well which makes no sense cause we are optimistically updating those already. Is there a way to purge specific queries from the cache? Or do you see a better approach to what I am trying to do?
The utility function is a work in progress but looks something like this:
Beta Was this translation helpful? Give feedback.
All reactions