Open History to see the last commit
1import React from 'react'
2β
3//
4β
5import { useConfigContext } from './config'
6import {
7 statusIdle,
8 statusLoading,
9 statusSuccess,
10 statusError,
11 useGetLatest,
12 Console,
13 uid,
14 useMountedCallback,
15 noop,
16} from './utils'
17β
18const getDefaultState = () => ({
19 status: statusIdle,
20 data: undefined,
21 error: null,
22})
23β
24const actionReset = {}
25const actionLoading = {}
26const actionResolve = {}
27const actionReject = {}
28β
29function mutationReducer(state, action) {
30 if (action.type === actionReset) {
31 return getDefaultState()
32 }
33 if (action.type === actionLoading) {
34 return {
35 status: statusLoading,
36 }
37 }
38 if (action.type === actionResolve) {
39 return {
40 status: statusSuccess,
41 data: action.data,
42 }
43 }
44 if (action.type === actionReject) {
45 return {
46 status: statusError,
47 error: action.error,
48 }
49 }
50 throw new Error()
51}
52β
53export function useMutation(mutationFn, config = {}) {
54 const [state, unsafeDispatch] = React.useReducer(
55 mutationReducer,
56 null,
57 getDefaultState
58 )
59β
60 const dispatch = useMountedCallback(unsafeDispatch)
61β
62 const getMutationFn = useGetLatest(mutationFn)
63β
64 const getConfig = useGetLatest({
65 ...useConfigContext(),
66 ...config,
67 })
68β
69 const latestMutationRef = React.useRef()
70β
71 const mutate = React.useCallback(
72 async (
73 variables,
74 { onSuccess = noop, onError = noop, onSettled = noop, throwOnError } = {}
75 ) => {
76 const config = getConfig()
77β
78 const mutationId = uid()
79 latestMutationRef.current = mutationId
80β
81 const isLatest = () => latestMutationRef.current === mutationId
82β
83 dispatch({ type: actionLoading })
84β
85 let snapshotValue
86β
87 try {
88 snapshotValue = await config.onMutate(variables)
89β
90 let data
91β
92 if (isLatest()) {
93 data = await getMutationFn()(variables)
94 }
95β
96 if (isLatest()) {
97 await onSuccess(data, variables)
98 }
99β
100 if (isLatest()) {
101 await config.onSuccess(data, variables)
102 }
103β
104 if (isLatest()) {
105 await onSettled(data, null, variables)
106 }
107β
108 if (isLatest()) {
109 await config.onSettled(data, null, variables)
110 }
111β
112 if (isLatest()) {
113 dispatch({ type: actionResolve, data })
114 }
115β
116 return data
117 } catch (error) {
118 if (isLatest()) {
119 Console.error(error)
120 await onError(error, variables, snapshotValue)
121 }
122β
123 if (isLatest()) {
124 await config.onError(error, variables, snapshotValue)
125 }
126β
127 if (isLatest()) {
128 await onSettled(undefined, error, variables, snapshotValue)
129 }
130β
131 if (isLatest()) {
132 await config.onSettled(undefined, error, variables, snapshotValue)
133 }
134β
135 if (isLatest()) {
136 dispatch({ type: actionReject, error })
137β
138 if (throwOnError ?? config.throwOnError) {
139 throw error
140 }
141 }
142 }
143 },
144 [dispatch, getConfig, getMutationFn]
145 )
146β
147 const reset = React.useCallback(() => dispatch({ type: actionReset }), [
148 dispatch,
149 ])
150β
151 React.useEffect(() => {
152 if (getConfig().useErrorBoundary && state.error) {
153 throw state.error
154 }
155 }, [getConfig, state.error])
156β
157 return [mutate, { ...state, reset }]
158}