MCPcopy
hub / github.com/TanStack/query / #dispatch

Method #dispatch

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

Source from the content-addressed store, hash-verified

606 }
607
608 #dispatch(action: Action<TData, TError>): void {
609 const reducer = (
610 state: QueryState<TData, TError>,
611 ): QueryState<TData, TError> => {
612 switch (action.type) {
613 case 'failed':
614 return {
615 ...state,
616 fetchFailureCount: action.failureCount,
617 fetchFailureReason: action.error,
618 }
619 case 'pause':
620 return {
621 ...state,
622 fetchStatus: 'paused',
623 }
624 case 'continue':
625 return {
626 ...state,
627 fetchStatus: 'fetching',
628 }
629 case 'fetch':
630 return {
631 ...state,
632 ...fetchState(state.data, this.options),
633 fetchMeta: action.meta ?? null,
634 }
635 case 'success':
636 const newState = {
637 ...state,
638 data: action.data,
639 dataUpdateCount: state.dataUpdateCount + 1,
640 dataUpdatedAt: action.dataUpdatedAt ?? Date.now(),
641 error: null,
642 isInvalidated: false,
643 status: 'success' as const,
644 ...(!action.manual && {
645 fetchStatus: 'idle' as const,
646 fetchFailureCount: 0,
647 fetchFailureReason: null,
648 }),
649 }
650 // If fetching ends successfully, we don't need revertState as a fallback anymore.
651 // For manual updates, capture the state to revert to it in case of a cancellation.
652 this.#revertState = action.manual ? newState : undefined
653
654 return newState
655 case 'error':
656 const error = action.error
657 return {
658 ...state,
659 error,
660 errorUpdateCount: state.errorUpdateCount + 1,
661 errorUpdatedAt: Date.now(),
662 fetchFailureCount: state.fetchFailureCount + 1,
663 fetchFailureReason: error,
664 fetchStatus: 'idle',
665 status: 'error',

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