MCPcopy Create free account
hub / github.com/TanStack/query / #dispatch

Method #dispatch

packages/query-core/src/query.ts:822–904  ·  view source on GitHub ↗
(action: Action<TData, TError>)

Source from the content-addressed store, hash-verified

820 }
821
822 #dispatch(action: Action<TData, TError>): void {
823 const reducer = (
824 state: QueryState<TData, TError>,
825 ): QueryState<TData, TError> => {
826 switch (action.type) {
827 case 'failed':
828 return {
829 ...state,
830 fetchFailureCount: action.failureCount,
831 fetchFailureReason: action.error,
832 }
833 case 'pause':
834 return {
835 ...state,
836 fetchStatus: 'paused',
837 }
838 case 'continue':
839 return {
840 ...state,
841 fetchStatus: 'fetching',
842 }
843 case 'fetch':
844 return {
845 ...state,
846 ...fetchState(state.data, this.options),
847 fetchMeta: action.meta ?? null,
848 }
849 case 'success':
850 const newState = {
851 ...state,
852 ...successState(action.data, action.dataUpdatedAt),
853 dataUpdateCount: state.dataUpdateCount + 1,
854 ...(!action.manual && {
855 fetchStatus: 'idle' as const,
856 fetchFailureCount: 0,
857 fetchFailureReason: null,
858 }),
859 }
860 // If fetching ends successfully, we don't need revertState as a fallback anymore.
861 // For manual updates, capture the state to revert to it in case of a cancellation.
862 this.#revertState = action.manual ? newState : undefined
863
864 return newState
865 case 'error':
866 const error = action.error
867 return {
868 ...state,
869 error,
870 errorUpdateCount: state.errorUpdateCount + 1,
871 errorUpdatedAt: Date.now(),
872 fetchFailureCount: state.fetchFailureCount + 1,
873 fetchFailureReason: error,
874 fetchStatus: 'idle',
875 status: 'error',
876 // flag existing data as invalidated if we get a background error
877 // note that "no data" always means stale so we can set unconditionally here
878 isInvalidated: true,
879 }

Callers 4

setDataMethod · 0.95
setStateMethod · 0.95
invalidateMethod · 0.95
fetchMethod · 0.95

Calls 2

onQueryUpdateMethod · 0.80
notifyMethod · 0.45

Tested by

no test coverage detected