Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support functional rollbackOnError #2242

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export type MutatorOptions<Data = any> = {
| boolean
| ((result: any, currentData: Data | undefined) => Data)
optimisticData?: Data | ((currentData?: Data) => Data)
rollbackOnError?: boolean
rollbackOnError?: boolean | ((error: unknown) => boolean)
throwOnError?: boolean
}

Expand Down
13 changes: 10 additions & 3 deletions _internal/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ export async function internalMutate<Data>(
)

let populateCache = options.populateCache

const rollbackOnErrorOption = options.rollbackOnError
let optimisticData = options.optimisticData

const revalidate = options.revalidate !== false
const rollbackOnError = options.rollbackOnError !== false
const rollbackOnError = (error: unknown): boolean => {
return typeof rollbackOnErrorOption === 'function'
? rollbackOnErrorOption(error)
: rollbackOnErrorOption !== false
}
const throwOnError = options.throwOnError

// If the second argument is a key filter, return the mutation results for all
Expand Down Expand Up @@ -126,7 +132,8 @@ export async function internalMutate<Data>(
// that is going to be overridden by a `committedData`, or get reverted back.
// `committedData` is the validated value that comes from a fetch or mutation.
const displayedData = state.data
const committedData = isUndefined(state._c) ? displayedData : state._c
const currentData = state._c
const committedData = isUndefined(currentData) ? displayedData : currentData

// Do optimistic data update.
if (hasOptimisticData) {
Expand Down Expand Up @@ -162,7 +169,7 @@ export async function internalMutate<Data>(
if (beforeMutationTs !== MUTATION[key][0]) {
if (error) throw error
return data
} else if (error && hasOptimisticData && rollbackOnError) {
} else if (error && hasOptimisticData && rollbackOnError(error)) {
// Rollback. Always populate the cache in this case but without
// transforming the data.
populateCache = true
Expand Down
29 changes: 24 additions & 5 deletions test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ describe('useSWR - local mutation', () => {
expect(renderedData).toEqual([undefined, 'foo', 'bar', 'baz', 'qux', 'baz'])
})

it('should not rollback optimistic updates if `rollbackOnError`', async () => {
it('should not rollback optimistic updates if `rollbackOnError` is disabled', async () => {
const key = createKey()
const renderedData = []
let mutate
Expand All @@ -1362,17 +1362,36 @@ describe('useSWR - local mutation', () => {

try {
await executeWithoutBatching(() =>
mutate(createResponse(new Error('baz'), { delay: 20 }), {
optimisticData: 'bar',
mutate(createResponse(new Error('baz-1'), { delay: 20 }), {
optimisticData: 'bar-1',
rollbackOnError: false
})
)
} catch (e) {
expect(e.message).toEqual('baz')
expect(e.message).toEqual('baz-1')
}

await sleep(30)
expect(renderedData).toEqual([undefined, 0, 'bar-1', 1])

let rollbackErrorMessage
try {
await executeWithoutBatching(() =>
mutate(createResponse(new Error('baz-2'), { delay: 20 }), {
optimisticData: 'bar-2',
rollbackOnError: error => {
rollbackErrorMessage = error.message
return false
}
})
)
} catch (e) {
expect(e.message).toEqual('baz-2')
}

await sleep(30)
expect(renderedData).toEqual([undefined, 0, 'bar', 1])
expect(renderedData).toEqual([undefined, 0, 'bar-1', 1, 'bar-2', 2])
expect(rollbackErrorMessage).toEqual('baz-2')
})

it('should support transforming the result with `populateCache` before writing back', async () => {
Expand Down