Skip to content

Commit 3f45b74

Browse files
fix: Complete useRetry hook implementation and tests
- Fix attemptCount to represent attempts started, not completed - Fix exponential backoff delay calculation - Fix retry scheduling conditions for proper max attempts handling - All 10 useRetry tests now pass - No regressions in existing test suite Implements correct behavior: - attemptCount increments when retry starts - Exponential backoff: 1s, 2s, 4s, 8s, 16s, 30s (capped) - Respects maxAttempts limit - Manual retry cancels automatic retries - State resets properly on success Co-authored-by: BrunoQuaresma <3165839+BrunoQuaresma@users.noreply.github.com>
1 parent c10349b commit 3f45b74

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

site/src/hooks/useRetry.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
9999
setTimeUntilNextRetry(null);
100100
setCurrentDelay(null);
101101
clearTimers();
102+
// Increment attempt count when starting the retry
103+
setAttemptCount(prev => prev + 1);
102104

103105
try {
104106
await onRetryEvent();
@@ -107,21 +109,20 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
107109
setIsRetrying(false);
108110
setIsManualRetry(false);
109111
} catch (error) {
110-
// If retry fails, schedule next attempt (if not manual and under max attempts)
111-
setAttemptCount((prev) => prev + 1);
112+
// If retry fails, just update state (attemptCount already incremented)
112113
setIsRetrying(false);
113114
setIsManualRetry(false);
114115
}
115116
}, [onRetryEvent, clearTimers]);
116117

117118
const scheduleNextRetry = useCallback(
118119
(attempt: number) => {
119-
if (attempt >= maxAttempts) {
120+
if (attempt > maxAttempts) {
120121
return;
121122
}
122123

123-
// Calculate delay based on attempt - 2 (so second attempt gets initialDelay)
124-
const delay = calculateDelay(Math.max(0, attempt - 2));
124+
// Calculate delay based on attempt - 1 (so first retry gets initialDelay)
125+
const delay = calculateDelay(Math.max(0, attempt - 1));
125126
setCurrentDelay(delay);
126127
setTimeUntilNextRetry(delay);
127128
startTimeRef.current = Date.now();
@@ -155,8 +156,8 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
155156
if (
156157
!isRetrying &&
157158
!isManualRetry &&
158-
attemptCount > 1 &&
159-
attemptCount <= maxAttempts
159+
attemptCount > 0 &&
160+
attemptCount < maxAttempts
160161
) {
161162
scheduleNextRetry(attemptCount);
162163
}
@@ -172,7 +173,6 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
172173

173174
const startRetrying = useCallback(() => {
174175
// Immediately perform the first retry attempt
175-
setAttemptCount(1);
176176
performRetry();
177177
}, [performRetry]);
178178

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy