new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Redux Toolkit · RTK Query · all subjects

cache-management/running-thunks

13 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

getRunningQueriesThunk and getRunningMutationsThunk

getRunningQueriesThunk() and getRunningMutationsThunk() are thunks that return ThunkWithReturnValue<Array<QueryActionCreatorResult<any>>> and ThunkWithReturnValue<Array<MutationActionCreatorResult<any>>> respectively. If dispatched, they return either all running queries or mutations. These returned values can be awaited like promises. This is useful for SSR scenarios to await all queries (or mutations) triggered in any way, including via hook calls or manually dispatching initiate actions.

getRunningQueriesThunk example

Example: await Promise.all(dispatch(api.util.getRunningQueriesThunk())). This awaits all currently running queries.

getRunningQueryThunk and getRunningMutationThunk

getRunningQueryThunk<EndpointName extends QueryKeys<Definitions>>(endpointName: EndpointName, args: QueryArgFrom<Definitions[EndpointName]>): ThunkWithReturnValue<QueryActionCreatorResult<Definitions[EndpointName] & { type: 'query' }> | undefined> and getRunningMutationThunk<EndpointName extends MutationKeys<Definitions>>(endpointName: EndpointName, fixedCacheKeyOrRequestId: string): ThunkWithReturnValue<MutationActionCreatorResult<Definitions[EndpointName] & { type: 'mutation' }> | undefined>. These thunks return a single running query (or mutation) for a given endpoint name + argument (or requestId/fixedCacheKey) combination, if it is currently running. If it is not currently running, the function returns undefined.

getRunningQueryThunk and getRunningMutationThunk purpose

getRunningQueryThunk and getRunningMutationThunk are primarily added to add experimental support for suspense in the future. They enable writing custom hooks that look up if RTK Query has already got a running query/mutation for a certain endpoint/argument combination, and retrieving that to throw it as a promise.

getRunningQueryThunk function signature

getRunningQueryThunk takes endpointName (EndpointName) and args (QueryArg) and returns a ThunkWithReturnValue<QueryActionCreatorResult or undefined>.

getRunningMutationThunk function signature

getRunningMutationThunk takes endpointName (EndpointName) and fixedCacheKeyOrRequestId (string) and returns a ThunkWithReturnValue<MutationActionCreatorResult or undefined>.

getRunningQueriesThunk function signature

getRunningQueriesThunk takes no parameters and returns a ThunkWithReturnValue<array of QueryActionCreatorResult<any>>.

getRunningMutationsThunk function signature

getRunningMutationsThunk takes no parameters and returns a ThunkWithReturnValue<array of MutationActionCreatorResult<any>>.

queryThunk core action and instantiation

queryThunk is a core action instantiated during buildThunks. It leverages createAsyncThunk to initiate the query process and is used extensively throughout the codebase both as a match case and to initiate queries. The payload for the query is built using executeEndpoint.

getPendingMeta adds metadata to queryThunk action

getPendingMeta is a function executed by queryThunk before running the payload creator. It adds additional metadata to the action for use in reducers or middleware. The metadata fields added are startedTimeStamp and SHOULD_AUTOBATCH.

queryThunk condition function determines if query continues

The condition function in queryThunk performs conditional checks based on the provided args to decide whether the query should continue or not. If the condition prevents execution, it attaches the field dispatchConditionRejected: true as confirmation that the condition was checked. The condition logic is: if isUpsertQuery(queryThunkArgs) return true; if requestState?.status === 'pending' return false; if isForcedQuery(queryThunkArgs, state) return true; if isQueryDefinition(endpointDefinition) and endpointDefinition?.forceRefetch returns true; if fulfilledVal exists return false; else return true.

refetchQuery in buildMiddleware refires queryThunk

In buildMiddleware, refetchQuery refires queryThunk with arguments for the queryThunk to determine if the query should be sent or not.

getRunningQueriesThunk utility

The api.util.getRunningQueriesThunk() utility returns a thunk that can be dispatched and awaited to wait for all running queries to finish. It is used in SSR scenarios: await Promise.all(dispatch(api.util.getRunningQueriesThunk())).

Give your agent this brain