Skip to content

Commit

Permalink
fix(useMutation): ensure all side-effects are always called
Browse files Browse the repository at this point in the history
Fixes #249

Now all we need is the new test from #641
  • Loading branch information
tannerlinsley committed Jun 27, 2020
1 parent 2de8528 commit 5a5ad74
Showing 1 changed file with 14 additions and 41 deletions.
55 changes: 14 additions & 41 deletions src/react/useMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,64 +80,37 @@ export function useMutation(mutationFn, config = {}) {

const isLatest = () => latestMutationRef.current === mutationId

dispatch({ type: actionLoading })

let snapshotValue

try {
dispatch({ type: actionLoading })
snapshotValue = await config.onMutate(variables)

let data

if (isLatest()) {
data = await getMutationFn()(variables)
}
let data = await getMutationFn()(variables)

if (isLatest()) {
dispatch({ type: actionResolve, data })
}

if (isLatest()) {
await config.onSuccess(data, variables)
}

if (isLatest()) {
await onSuccess(data, variables)
}

if (isLatest()) {
await config.onSettled(data, null, variables)
}

if (isLatest()) {
await onSettled(data, null, variables)
}
await config.onSuccess(data, variables)
await onSuccess(data, variables)
await config.onSettled(data, null, variables)
await onSettled(data, null, variables)

return data
} catch (error) {
if (isLatest()) {
Console.error(error)
await config.onError(error, variables, snapshotValue)
}

if (isLatest()) {
await onError(error, variables, snapshotValue)
}

if (isLatest()) {
await config.onSettled(undefined, error, variables, snapshotValue)
}

if (isLatest()) {
await onSettled(undefined, error, variables, snapshotValue)
}
Console.error(error)
await config.onError(error, variables, snapshotValue)
await onError(error, variables, snapshotValue)
await config.onSettled(undefined, error, variables, snapshotValue)
await onSettled(undefined, error, variables, snapshotValue)

if (isLatest()) {
dispatch({ type: actionReject, error })
}

if (throwOnError ?? config.throwOnError) {
throw error
}
if (throwOnError ?? config.throwOnError) {
throw error
}
}
},
Expand Down

0 comments on commit 5a5ad74

Please sign in to comment.