MCPcopy Create free account
hub / github.com/TanStack/query / reducer

Method reducer

packages/query-core/src/query.ts:962–1032  ·  view source on GitHub ↗
(
      state: QueryState<TData, TError>,
    )

Source from the content-addressed store, hash-verified

960
961 #dispatch(action: Action<TData, TError>): void {
962 const reducer = (
963 state: QueryState<TData, TError>,
964 ): QueryState<TData, TError> => {
965 switch (action.type) {
966 case 'failed':
967 return {
968 ...state,
969 fetchFailureCount: action.failureCount,
970 fetchFailureReason: action.error,
971 }
972 case 'pause':
973 return {
974 ...state,
975 fetchStatus: 'paused',
976 }
977 case 'continue':
978 return {
979 ...state,
980 fetchStatus: 'fetching',
981 }
982 case 'fetch':
983 return {
984 ...state,
985 ...fetchState(state.data, this.options),
986 fetchMeta: action.meta ?? null,
987 }
988 case 'success': {
989 const newState = {
990 ...state,
991 ...successState(action.data, action.dataUpdatedAt),
992 dataUpdateCount: state.dataUpdateCount + 1,
993 ...(!action.manual && {
994 fetchStatus: 'idle' as const,
995 fetchFailureCount: 0,
996 fetchFailureReason: null,
997 }),
998 }
999 // If fetching ends successfully, we don't need revertState as a fallback anymore.
1000 // For manual updates, capture the state to revert to it in case of a cancellation.
1001 this.#revertState = action.manual ? newState : undefined
1002
1003 return newState
1004 }
1005 case 'error': {
1006 const error = action.error
1007 return {
1008 ...state,
1009 error,
1010 errorUpdateCount: state.errorUpdateCount + 1,
1011 errorUpdatedAt: Date.now(),
1012 fetchFailureCount: state.fetchFailureCount + 1,
1013 fetchFailureReason: error,
1014 fetchStatus: 'idle',
1015 status: 'error',
1016 // flag existing data as invalidated if we get a background error
1017 // note that "no data" always means stale so we can set unconditionally here
1018 isInvalidated: true,
1019 }

Callers

nothing calls this directly

Calls 2

fetchStateFunction · 0.85
successStateFunction · 0.85

Tested by

no test coverage detected