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

Method #dispatch

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

Source from the content-addressed store, hash-verified

631 }
632
633 #dispatch(action: Action<TData, TError>): void {
634 const reducer = (
635 state: QueryState<TData, TError>,
636 ): QueryState<TData, TError> => {
637 switch (action.type) {
638 case 'failed':
639 return {
640 ...state,
641 fetchFailureCount: action.failureCount,
642 fetchFailureReason: action.error,
643 }
644 case 'pause':
645 return {
646 ...state,
647 fetchStatus: 'paused',
648 }
649 case 'continue':
650 return {
651 ...state,
652 fetchStatus: 'fetching',
653 }
654 case 'fetch':
655 return {
656 ...state,
657 ...fetchState(state.data, this.options),
658 fetchMeta: action.meta ?? null,
659 }
660 case 'success':
661 const newState = {
662 ...state,
663 ...successState(action.data, action.dataUpdatedAt),
664 dataUpdateCount: state.dataUpdateCount + 1,
665 ...(!action.manual && {
666 fetchStatus: 'idle' as const,
667 fetchFailureCount: 0,
668 fetchFailureReason: null,
669 }),
670 }
671 // If fetching ends successfully, we don't need revertState as a fallback anymore.
672 // For manual updates, capture the state to revert to it in case of a cancellation.
673 this.#revertState = action.manual ? newState : undefined
674
675 return newState
676 case 'error':
677 const error = action.error
678 return {
679 ...state,
680 error,
681 errorUpdateCount: state.errorUpdateCount + 1,
682 errorUpdatedAt: Date.now(),
683 fetchFailureCount: state.fetchFailureCount + 1,
684 fetchFailureReason: error,
685 fetchStatus: 'idle',
686 status: 'error',
687 // flag existing data as invalidated if we get a background error
688 // note that "no data" always means stale so we can set unconditionally here
689 isInvalidated: true,
690 }

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