Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c115f13
feat: Phase 1 - Terminal reconnection foundation
blink-so[bot] Jul 1, 2025
c10349b
fix: Improve useRetry hook logic
blink-so[bot] Jul 1, 2025
3f45b74
fix: Complete useRetry hook implementation and tests
blink-so[bot] Jul 1, 2025
834b2e5
style: Apply biome formatting fixes to useRetry hook
blink-so[bot] Jul 1, 2025
d398265
fix: Use window.setTimeout/setInterval for browser compatibility
blink-so[bot] Jul 1, 2025
dd7adda
refactor: consolidate useRetry state with useReducer
blink-so[bot] Jul 1, 2025
5766fc0
Reset TerminalPage files to main branch state
blink-so[bot] Jul 2, 2025
b1e453b
Add useWithRetry hook for simplified retry functionality
blink-so[bot] Jul 2, 2025
d4326fb
Clean up useWithRetry hook implementation
blink-so[bot] Jul 2, 2025
8323192
Remove useRetry hook and replace with useWithRetry
blink-so[bot] Jul 2, 2025
3022566
Refactor useWithRetry hook according to specifications
blink-so[bot] Jul 2, 2025
bde014c
Preserve attemptCount when max attempts reached
blink-so[bot] Jul 2, 2025
cb363db
Fix formatting
BrunoQuaresma Jul 2, 2025
55036a4
Fix hook and tests
BrunoQuaresma Jul 2, 2025
000f0e4
feat(hooks): remove max attempts limit from useWithRetry hook
BrunoQuaresma Jul 3, 2025
f9832c0
refactor(hooks): remove attemptCount from useWithRetry state and rena…
BrunoQuaresma Jul 3, 2025
4fd4885
fix(hooks): update useWithRetry tests for nextRetryAt API and add use…
BrunoQuaresma Jul 3, 2025
7b14acd
fix(hooks): prevent race condition in useWithRetry after unmount
BrunoQuaresma Jul 3, 2025
323f6ba
Fix formatting
BrunoQuaresma Jul 3, 2025
062bfa5
fix(hooks): prevent duplicate calls to useWithRetry while loading
BrunoQuaresma Jul 3, 2025
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
Prev Previous commit
Next Next commit
fix: Improve useRetry hook logic
- Fix startRetrying to immediately perform first retry
- Adjust retry scheduling conditions
- Fix delay calculation for exponential backoff

Still debugging test failures
  • Loading branch information
blink-so[bot] committed Jul 1, 2025
commit c10349b25d2fc016d870b13f58d70e1d7f51cba9
88 changes: 52 additions & 36 deletions site/src/hooks/useRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
const [isRetrying, setIsRetrying] = useState(false);
const [currentDelay, setCurrentDelay] = useState<number | null>(null);
const [attemptCount, setAttemptCount] = useState(0);
const [timeUntilNextRetry, setTimeUntilNextRetry] = useState<number | null>(null);
const [timeUntilNextRetry, setTimeUntilNextRetry] = useState<number | null>(
null,
);
const [isManualRetry, setIsManualRetry] = useState(false);

const timeoutRef = useRef<number | null>(null);
Expand All @@ -84,10 +86,13 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
startTimeRef.current = null;
}, []);

const calculateDelay = useCallback((attempt: number): number => {
const delay = initialDelay * Math.pow(multiplier, attempt);
return Math.min(delay, maxDelay);
}, [initialDelay, multiplier, maxDelay]);
const calculateDelay = useCallback(
(attempt: number): number => {
const delay = initialDelay * multiplier ** attempt;
return Math.min(delay, maxDelay);
},
[initialDelay, multiplier, maxDelay],
);

const performRetry = useCallback(async () => {
setIsRetrying(true);
Expand All @@ -103,47 +108,56 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
setIsManualRetry(false);
} catch (error) {
// If retry fails, schedule next attempt (if not manual and under max attempts)
setAttemptCount(prev => prev + 1);
setAttemptCount((prev) => prev + 1);
setIsRetrying(false);
setIsManualRetry(false);
}
}, [onRetryEvent, clearTimers]);

const scheduleNextRetry = useCallback((attempt: number) => {
if (attempt >= maxAttempts) {
return;
}
const scheduleNextRetry = useCallback(
(attempt: number) => {
if (attempt >= maxAttempts) {
return;
}

const delay = calculateDelay(attempt);
setCurrentDelay(delay);
setTimeUntilNextRetry(delay);
startTimeRef.current = Date.now();

// Start countdown timer
countdownRef.current = setInterval(() => {
if (startTimeRef.current) {
const elapsed = Date.now() - startTimeRef.current;
const remaining = Math.max(0, delay - elapsed);
setTimeUntilNextRetry(remaining);

if (remaining <= 0) {
if (countdownRef.current) {
clearInterval(countdownRef.current);
countdownRef.current = null;
// Calculate delay based on attempt - 2 (so second attempt gets initialDelay)
const delay = calculateDelay(Math.max(0, attempt - 2));
setCurrentDelay(delay);
setTimeUntilNextRetry(delay);
startTimeRef.current = Date.now();

// Start countdown timer
countdownRef.current = setInterval(() => {
if (startTimeRef.current) {
const elapsed = Date.now() - startTimeRef.current;
const remaining = Math.max(0, delay - elapsed);
setTimeUntilNextRetry(remaining);

if (remaining <= 0) {
if (countdownRef.current) {
clearInterval(countdownRef.current);
countdownRef.current = null;
}
}
}
}
}, 100); // Update every 100ms for smooth countdown
}, 100); // Update every 100ms for smooth countdown

// Schedule the actual retry
timeoutRef.current = setTimeout(() => {
performRetry();
}, delay);
}, [calculateDelay, maxAttempts, performRetry]);
// Schedule the actual retry
timeoutRef.current = setTimeout(() => {
performRetry();
}, delay);
},
[calculateDelay, maxAttempts, performRetry],
);

// Effect to schedule next retry after a failed attempt
useEffect(() => {
if (!isRetrying && !isManualRetry && attemptCount > 0 && attemptCount < maxAttempts) {
if (
!isRetrying &&
!isManualRetry &&
attemptCount > 1 &&
attemptCount <= maxAttempts
) {
scheduleNextRetry(attemptCount);
}
}, [attemptCount, isRetrying, isManualRetry, maxAttempts, scheduleNextRetry]);
Expand All @@ -157,8 +171,10 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
}, [clearTimers, performRetry]);

const startRetrying = useCallback(() => {
setAttemptCount(1); // This will trigger the first retry attempt
}, []);
// Immediately perform the first retry attempt
setAttemptCount(1);
performRetry();
}, [performRetry]);

const stopRetrying = useCallback(() => {
clearTimers();
Expand Down
Loading