Refactor usePolledData hook and add comprehensive tests

- Renamed usePolledIntervalCheck to usePolledData for clarity
- Updated hook to properly manage interval cleanup on unmount
- Added comprehensive test suite covering normal operation, error handling, and cleanup
- Updated documentation to reflect new hook name
- Updated Tasks.md to track progress

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-30 20:24:47 +02:00
parent 69d32bfbe9
commit 3bd2a71367
4 changed files with 280 additions and 61 deletions

View File

@@ -29,3 +29,27 @@ Hooks that only fetch once inside `useEffect` and do not expose a manual refresh
### 3. Do not use boolean cancelled flags for network requests
A boolean `cancelled` flag is not sufficient because it does not stop the underlying fetch. Abort signals are the correct cancellation mechanism for fetch-based hooks.
## Polling with Drift Correction
The `usePolledData` hook implements drift-corrected polling to maintain accurate polling intervals despite variable fetch durations.
### How it works
- Uses self-scheduling timeouts instead of fixed `setInterval`
- Tracks elapsed time from poll start to completion with `performance.now()`
- Calculates next delay as `Math.max(0, pollInterval - elapsed)`
- Schedules the next poll with drift compensation in the `onSuccess` callback
- If a fetch takes longer than `pollInterval`, the next poll starts immediately (delay = 0)
### Why this matters
With fixed `setInterval`:
- If a fetch takes 2 seconds and `pollInterval` is 5 seconds
- Actual polling interval becomes ~7 seconds (2s fetch + 5s interval)
- Effective polling rate drifts and wastes bandwidth
With drift correction:
- Total time from poll start to next poll start is always ~5 seconds
- Fetch duration doesn't affect the long-term polling rate
- Bandwidth and CPU usage remain consistent