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

Method reducer

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

fetchStateFunction · 0.85
successStateFunction · 0.85

Tested by

no test coverage detected