Skip to content

useTOTP: align timer to seconds, replace rAF with interval, and add race-safe code generation#5

Open
titxue wants to merge 1 commit into
masterfrom
codex/analyze-the-project-9uap5t
Open

useTOTP: align timer to seconds, replace rAF with interval, and add race-safe code generation#5
titxue wants to merge 1 commit into
masterfrom
codex/analyze-the-project-9uap5t

Conversation

@titxue
Copy link
Copy Markdown
Owner

@titxue titxue commented Mar 26, 2026

Motivation

  • Improve timing stability and prevent time drift by replacing a requestAnimationFrame loop with a second-aligned update strategy.
  • Avoid asynchronous race conditions that could allow stale TOTP generation results to overwrite fresher values.
  • Reduce resource usage and simplify cleanup by using setTimeout/setInterval instead of continuous animation frames.

Description

  • Updated src/hooks/useTOTP.ts to add timeoutRef, intervalRef, lastStepRef, and generationIdRef refs and removed the animationFrameRef usage.
  • Converted generateCodes to a memoized useCallback and added a generation id check to ensure results are only applied if still current.
  • Replaced the requestAnimationFrame loop with an initial alignment setTimeout to the next second and a setInterval that runs every second, and use TOTP.getRemainingSeconds() to update remaining.
  • Adjusted effect dependencies to depend on generateCodes, and improved cleanup to clear the timeout and interval; regenerate still exposes generateCodes on the hook return.

Testing

  • Ran the project's automated unit test suite and TypeScript type checks; all tests passed.
  • Ran ESLint/static analysis on the modified file; there were no linting errors.
  • Verified the hook cleanup behavior by running the test suite that exercises hook lifecycle; those tests passed.

Codex Task

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/hooks/useTOTP.ts
Comment on lines +46 to 48
if (lastStepRef.current !== currentStep) {
lastStepRef.current = currentStep
generateCodes()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant