Repository navigation
Reduce requests number - #464
Conversation
🔍 Arkana PR Review —
|
WalkthroughDebounce service-worker VTXO/UTXO messages to schedule a single reload after 1000ms, switch service-worker listener management to store/remove the exact handler function, and remove the startup Changes
Sequence Diagram(s)sequenceDiagram
participant SW as ServiceWorker
participant Provider as WalletProvider
participant Timer as ReloadTimer
participant Wallet as reloadWallet / renewCoins
rect rgba(173,216,230,0.5)
SW->>Provider: postMessage(VTXO_UPDATE / UTXO_UPDATE)
end
Provider->>Provider: clear existing reloadTimerRef
Provider->>Timer: set timeout (1000ms)
Timer--)Provider: timeout triggers
Provider->>Wallet: reloadWallet(svcWallet)
alt delegation disabled
Provider->>Wallet: renewCoins(svcWallet, ...)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/providers/wallet.tsx (1)
312-319: Debounce implementation looks correct, but consider timer cleanup on reinitialization.The debounce pattern works well for the normal flow. However, if
initSvcWorkerWalletis called again (viarestartWalletor retry), any pending timer from the previous closure will still fire with a stalesvcWalletreference. Consider promotingreloadTimerto auseRefor clearing it at the start ofinitSvcWorkerWallet.♻️ Optional: Use a ref to ensure cleanup across reinitializations
+ const reloadTimerRef = useRef<ReturnType<typeof setTimeout>>() + const initSvcWorkerWallet = async ({ ... }) => { try { + // Clear any pending reload from previous initialization + clearTimeout(reloadTimerRef.current) ... - let reloadTimer: ReturnType<typeof setTimeout> | undefined const handleServiceWorkerMessages = (event: MessageEvent) => { if (event.data && ['VTXO_UPDATE', 'UTXO_UPDATE'].includes(event.data.type)) { - clearTimeout(reloadTimer) - reloadTimer = setTimeout(() => reloadWallet(svcWallet), 1000) + clearTimeout(reloadTimerRef.current) + reloadTimerRef.current = setTimeout(() => reloadWallet(svcWallet), 1000) } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/providers/wallet.tsx` around lines 312 - 319, The debounce timer reloadTimer in handleServiceWorkerMessages can survive across re-initializations of initSvcWorkerWallet and call reloadWallet with a stale svcWallet; fix by promoting reloadTimer to a React ref (e.g., useRef<ReturnType<typeof setTimeout> | undefined>) or ensure you clearTimeout(reloadTimer) at the very start of initSvcWorkerWallet (and when restartWallet runs) so any pending timer is cancelled before re-registering the service worker handlers; update references in handleServiceWorkerMessages and any cleanup logic to use the ref or cleared variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/providers/wallet.tsx`:
- Around line 312-319: The debounce timer reloadTimer in
handleServiceWorkerMessages can survive across re-initializations of
initSvcWorkerWallet and call reloadWallet with a stale svcWallet; fix by
promoting reloadTimer to a React ref (e.g., useRef<ReturnType<typeof setTimeout>
| undefined>) or ensure you clearTimeout(reloadTimer) at the very start of
initSvcWorkerWallet (and when restartWallet runs) so any pending timer is
cancelled before re-registering the service worker handlers; update references
in handleServiceWorkerMessages and any cleanup logic to use the ref or cleared
variable.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8a05f480-0493-4e6a-83ed-627801d24a1e
📒 Files selected for processing (1)
src/providers/wallet.tsx
Deploying wallet-mutinynet with
|
| Latest commit: |
5b7da51
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://b40b3824.arkade-wallet.pages.dev |
| Branch Preview URL: | https://reduce-requests-number.arkade-wallet.pages.dev |
Deploying wallet-bitcoin with
|
| Latest commit: |
5b7da51
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://76e87c34.wallet-bitcoin.pages.dev |
| Branch Preview URL: | https://reduce-requests-number.wallet-bitcoin.pages.dev |
🔍 Arkana Review — Incremental (commit
|
Previously, each UTXO_UPDATE or VTXO_UPDATE message from the service worker triggered reloadWallet() twice: once immediately and once after a 5s delay. The immediate call fetches stale data (the indexer hasn't updated yet), wasting a full round of API requests every refresh cycle. Replace with a single debounced 5s-delayed reload. If multiple update messages arrive in quick succession, only the last one triggers a reload. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The SDK's VtxoManager already auto-delegates via onContractEvent. The wallet-side call ran concurrently, doubling info/delegator requests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
75186c6 to
5b7da51
Compare
🔍 Review — New commits since last reviewCommits reviewed: Changes
Notes
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/providers/wallet.tsx`:
- Around line 331-351: In lockWallet, also clear the debounced reload timer and
unregister the service worker message handler: call
clearTimeout(reloadTimerRef.current) and set reloadTimerRef.current = null,
remove the listener via navigator.serviceWorker.removeEventListener('message',
swMessageHandlerRef.current) and set swMessageHandlerRef.current = null, and
ensure you still clear statusPingInterval as before; this prevents reloadWallet
from being invoked after wallet is cleared and avoids leaving the message
listener registered.
- Around line 368-373: The comment above the renewCoins call is inaccurate about
how delegation works; update the comment near renewCoins(svcWallet,
aspInfo.dust, wallet.thresholdMs) to state that when delegation is enabled the
SDK auto-delegates internally via the delegatorUrl passed during wallet
initialization (see initSvcWorkerWallet and delegatorUrl), so wallet-side
renewCoins is skipped in delegate mode; keep the existing best-effort error
handling (.catch(() => {})) unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 43bab0de-da10-4ba5-9c5c-dbe009945865
📒 Files selected for processing (1)
src/providers/wallet.tsx
* Debounce wallet reload on UTXO/VTXO service worker updates Previously, each UTXO_UPDATE or VTXO_UPDATE message from the service worker triggered reloadWallet() twice: once immediately and once after a 5s delay. The immediate call fetches stale data (the indexer hasn't updated yet), wasting a full round of API requests every refresh cycle. Replace with a single debounced 5s-delayed reload. If multiple update messages arrive in quick succession, only the last one triggers a reload. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Remove redundant delegateVtxos call on startup The SDK's VtxoManager already auto-delegates via onContractEvent. The wallet-side call ran concurrently, doubling info/delegator requests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Reduce delay to 1s * Addresss stale timer and listener accumulation --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
reloadWalletSummary by CodeRabbit
Improvements
Changes