Skip to content

fix(hooks): update useSmartPolling to handle enabled toggle and reset isPolling (#570) - #578

Open
PINYOPATTANAWASANPORN wants to merge 2 commits into
MergeFi:mainfrom
PINYOPATTANAWASANPORN:fix/use-smart-polling-enabled-toggle-570
Open

PINYOPATTANAWASANPORN wants to merge 2 commits into
MergeFi:mainfrom
PINYOPATTANAWASANPORN:fix/use-smart-polling-enabled-toggle-570

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown

Summary of Changes

Fixed a bug in useSmartPolling where toggling enabled between true and false left isPolling in a stale true state when disabled, and failed to resume polling when re-enabled (#570).

Root Cause / Technical Context

  1. Previously, when enabled transitioned to false, the cleanup cleared the interval timer but omitted dispatch({ type: 'STOP_POLLING' }). Consequently, isPolling remained true even though polling had halted.
  2. Additionally, when enabled transitioned back to true, hasInitialized.current was already set to true from initial mount, causing the initialization branch to be skipped and preventing interval re-creation.
  3. Fix details:
    • Separate unmount cleanup (isMountedRef.current = false) from the enabled lifecycle effect.
    • Dispatch STOP_POLLING and clear the active interval timer when enabled=false.
    • Dispatch START_POLLING and start interval timer when enabled=true.
    • Stabilized option/callback references with useRef to prevent infinite re-render loops.
    • Added unit test suite in useSmartPolling.test.ts using fake timers validating enabled=true -> false -> true toggling behavior.

Verification & Testing

Ran npx jest src/hooks/useSmartPolling.test.ts --coverage=false:

PASS src/hooks/useSmartPolling.test.ts
  useSmartPolling (#570)
    √ starts polling when enabled=true (22 ms)
    √ sets isPolling=false and stops timer when toggled to enabled=false (#570) (8 ms)
    √ re-starts polling immediately when toggled from false back to true (#570) (4 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.478 s

… isPolling (MergeFi#570)

Ensure enabled=false dispatches STOP_POLLING and clears interval timer, and enabled=true correctly re-initializes active polling.
… isPolling (MergeFi#570)

Ensure enabled=false dispatches STOP_POLLING and clears interval timer, and enabled=true correctly re-initializes active polling.
@vercel

vercel Bot commented Oct 4, 2026

Copy link
Copy Markdown

@PINYOPATTANAWASANPORN is attempting to deploy a commit to the chonilius' projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant