(action: Action<TData, TError>)
| 950 | } |
| 951 | |
| 952 | #dispatch(action: Action<TData, TError>): void { |
| 953 | const reducer = ( |
| 954 | state: QueryState<TData, TError>, |
| 955 | ): QueryState<TData, TError> => { |
| 956 | switch (action.type) { |
| 957 | case 'failed': |
| 958 | return { |
| 959 | ...state, |
| 960 | fetchFailureCount: action.failureCount, |
| 961 | fetchFailureReason: action.error, |
| 962 | } |
| 963 | case 'pause': |
| 964 | return { |
| 965 | ...state, |
| 966 | fetchStatus: 'paused', |
| 967 | } |
| 968 | case 'continue': |
| 969 | return { |
| 970 | ...state, |
| 971 | fetchStatus: 'fetching', |
| 972 | } |
| 973 | case 'fetch': |
| 974 | return { |
| 975 | ...state, |
| 976 | ...fetchState(state.data, this.options), |
| 977 | fetchMeta: action.meta ?? null, |
| 978 | } |
| 979 | case 'success': { |
| 980 | const newState = { |
| 981 | ...state, |
| 982 | ...successState(action.data, action.dataUpdatedAt), |
| 983 | dataUpdateCount: state.dataUpdateCount + 1, |
| 984 | ...(!action.manual && { |
| 985 | fetchStatus: 'idle' as const, |
| 986 | fetchFailureCount: 0, |
| 987 | fetchFailureReason: null, |
| 988 | }), |
| 989 | } |
| 990 | // If fetching ends successfully, we don't need revertState as a fallback anymore. |
| 991 | // For manual updates, capture the state to revert to it in case of a cancellation. |
| 992 | this.#revertState = action.manual ? newState : undefined |
| 993 | |
| 994 | return newState |
| 995 | } |
| 996 | case 'error': { |
| 997 | const error = action.error |
| 998 | return { |
| 999 | ...state, |
| 1000 | error, |
| 1001 | errorUpdateCount: state.errorUpdateCount + 1, |
| 1002 | errorUpdatedAt: Date.now(), |
| 1003 | fetchFailureCount: state.fetchFailureCount + 1, |
| 1004 | fetchFailureReason: error, |
| 1005 | fetchStatus: 'idle', |
| 1006 | status: 'error', |
| 1007 | // flag existing data as invalidated if we get a background error |
| 1008 | // note that "no data" always means stale so we can set unconditionally here |
| 1009 | isInvalidated: true, |
no test coverage detected