Skip to content
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
14 changes: 13 additions & 1 deletion gitbook/reactjrx/queries/react/useswitchmutationusd.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ onError: (error) => {
}
```

A cancelled call is not retried, whatever `retry` says. A call cancelled while TanStack Query waits to retry it settles once that wait ends.
A cancelled call is not retried, whatever `retry` says. A call cancelled while it waits `retryDelay` fails at once. One that TanStack Query holds, such as while the device is offline, fails when TanStack Query resumes it.

## Retries

`retry` and `retryDelay` take the same values as in TanStack Query, the client's defaults included. TanStack Query cannot stop its wait between two attempts, so the hook waits `retryDelay` itself, at the end of the failed attempt, and gives TanStack Query a `retryDelay` of 0, which a `MutationCache` sees. TanStack Query then holds the retry while the device is offline or the page hidden, as it does after its own wait.

Three things differ from TanStack Query:

- `failureCount` and `failureReason` change when the wait ends, not when the attempt fails. A call cancelled during the wait never records that failure.
- A `retry` function runs twice for each failure: once in the hook, to know whether to wait, and once in TanStack Query.
- A change to `retry` or `retryDelay` applies to the calls already running. TanStack Query keeps the values that a call started with.

As in TanStack Query, `retryDelay` must not throw. Here, its error replaces the attempt's error, and the retry starts at once.

## Variables

Expand Down
18 changes: 3 additions & 15 deletions src/lib/binding/useSubscribeEffect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { useCallback } from "react"
import { retry, tap, throwError, timer } from "rxjs"
import { makeObservable } from "../utils/makeObservable"
import {
exponentialBackoffDelay,
getDelay,
} from "../utils/operators/retryBackoff"
import { useLiveRef } from "../utils/react/useLiveRef"
import { shouldRetry } from "../utils/shouldRetry"
import { getRetryDelay, shouldRetry } from "../utils/retry"
import type { SubscribeSource } from "./useSubscribe/types"
import { useSubscribe } from "./useSubscribe/useSubscribe"

Expand Down Expand Up @@ -36,9 +32,6 @@ export interface UseSubscribeEffectOptions {
retryDelay?: number | ((attempt: number, error: unknown) => number)
}

const defaultRetryDelay = (attempt: number) =>
getDelay(exponentialBackoffDelay(attempt, 1000), 30_000)

/**
* Subscribes to `source` while the component is mounted. Options are read
* at each error, so changing them never subscribes again.
Expand All @@ -62,18 +55,13 @@ export function useSubscribeEffect<T>(
resetOnSuccess: true,
delay: (error, retryCount) => {
const attempt = retryCount - 1
const { retry: retryOption, retryDelay = defaultRetryDelay } =
optionsRef.current ?? {}
const { retry: retryOption, retryDelay } = optionsRef.current ?? {}

if (!shouldRetry(retryOption, attempt, error)) {
return throwError(() => error)
}

return timer(
typeof retryDelay === "function"
? retryDelay(attempt, error)
: retryDelay,
)
return timer(getRetryDelay(retryDelay, attempt, error))
},
}),
)
Expand Down
Loading
Loading