useTOTP: align timer to seconds, replace rAF with interval, and add race-safe code generation#5
useTOTP: align timer to seconds, replace rAF with interval, and add race-safe code generation#5titxue wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3c3f147ba3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (lastStepRef.current !== currentStep) { | ||
| lastStepRef.current = currentStep | ||
| generateCodes() |
There was a problem hiding this comment.
Regenerate codes immediately on account changes
This gate makes code generation dependent only on a 30-second step transition, so when accounts changes within the same step (add/remove/edit account), updateTimer() skips generateCodes() and the hook continues returning stale entries until the next boundary. In this commit, the previous unconditional generation-on-effect-run behavior was removed, so account updates can show wrong or missing codes for up to 30 seconds.
Useful? React with 👍 / 👎.
Motivation
requestAnimationFrameloop with a second-aligned update strategy.setTimeout/setIntervalinstead of continuous animation frames.Description
src/hooks/useTOTP.tsto addtimeoutRef,intervalRef,lastStepRef, andgenerationIdRefrefs and removed theanimationFrameRefusage.generateCodesto a memoizeduseCallbackand added a generation id check to ensure results are only applied if still current.requestAnimationFrameloop with an initial alignmentsetTimeoutto the next second and asetIntervalthat runs every second, and useTOTP.getRemainingSeconds()to updateremaining.generateCodes, and improved cleanup to clear the timeout and interval;regeneratestill exposesgenerateCodeson the hook return.Testing
Codex Task