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

Method #dispatch

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

Source from the content-addressed store, hash-verified

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

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