Skip to content

perf(oracle): throttle visibilitychange refetches in useOracle hooks (#519) - #626

Merged
nonsobethel0-dev merged 2 commits into
Parashield-Protocol:mainfrom
oluwarantimini:fix/519-throttle-oracle-visibility-refetch
Sep 23, 2026
Merged

nonsobethel0-dev merged 2 commits into
Parashield-Protocol:mainfrom
oluwarantimini:fix/519-throttle-oracle-visibility-refetch

Conversation

@oluwarantimini

@oluwarantimini oluwarantimini commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

src/hooks/useOracle.ts refetched every time the tab became visible. Switching tabs quickly (10 times in 5 seconds, for example) sent one oracle API request per switch, even though the hook already polls every ORACLE_REFRESH_INTERVAL_MS (60s).

Both hooks had this problem: useOracleReading and useAllOracleReadings, which backs the oracle page.

Fix

This follows the approach suggested in the issue:

  • Added VISIBILITY_REFETCH_MIN_MS = ORACLE_REFRESH_INTERVAL_MS / 2 (30s by default). It is exported so tests can use it.
  • Each hook stores the time of its last fetch in a ref (lastFetchAtRef). The ref is updated at the start of every load(), whether the fetch came from the first load, the polling interval, a visibility change or a manual refetch.
  • The visibilitychange handler now refetches only if the tab is visible and the last fetch was at least VISIBILITY_REFETCH_MIN_MS ago.

Unchanged:

Tests

The new tests are in src/hooks/__tests__/useOracle.test.ts. They stub Date.now and dispatch real visibilitychange events:

  • useOracleReading: 10 rapid visibility changes trigger no extra fetch. After moving the clock past VISIBILITY_REFETCH_MIN_MS, one more change triggers exactly one refetch.
  • useAllOracleReadings: the same check.

Results:

npx vitest run src/hooks/__tests__/useOracle.test.ts
 Test Files  1 passed (1)
      Tests  9 passed (9)

Both useOracleReading and useAllOracleReadings refetched on every
visibilitychange, so rapid tab switching fired a request per switch.
Track the last fetch time and only refetch on visibility if at least
VISIBILITY_REFETCH_MIN_MS (half of ORACLE_REFRESH_INTERVAL_MS) has
passed. Interval polling and manual refetch are unchanged.

Closes Parashield-Protocol#519
Simulate 10 rapid tab switches and assert no extra fetch, then advance
past VISIBILITY_REFETCH_MIN_MS and assert exactly one refetch, for both
useOracleReading and useAllOracleReadings.

Refs Parashield-Protocol#519
@drips-wave

drips-wave Bot commented Sep 23, 2026

Copy link
Copy Markdown

@oluwarantimini Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@netlify

netlify Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for boisterous-sunshine-dd4c4c failed.

Name Link
🔨 Latest commit 4806c9e
🔍 Latest deploy log https://app.netlify.com/projects/boisterous-sunshine-dd4c4c/deploys/6ab41e911bf6df0008cd9b57

@nonsobethel0-dev
nonsobethel0-dev merged commit f63f2cb into Parashield-Protocol:main Sep 23, 2026
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants