From b1b09dff0921e61c2a41676a5610f7c5373649f7 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 22 Nov 2024 20:54:18 +0100 Subject: [PATCH] Wait for poll to finish with setTimeout This makes sure that we wait for the pending poll to finish before we poll again. This prevents running multiple polls at the same time on slow connections. I noticed we don't need to queue a new poll ourselves since a poll updates effect dependencies so we will cleanup and run the effect again anyway. --- components/wallet-logger.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/wallet-logger.js b/components/wallet-logger.js index 3d405f424..4dc25a976 100644 --- a/components/wallet-logger.js +++ b/components/wallet-logger.js @@ -319,10 +319,11 @@ export function useWalletLogs (wallet, initialPage = 1, logsPerPage = 10) { // only fetch new logs if we are on a page that uses logs const needLogs = router.asPath.startsWith('/settings/wallets') || router.asPath.startsWith('/wallet/logs') if (me && needLogs) { - const interval = setInterval(() => { - loadNew().catch(console.error) - }, 1_000) - return () => clearInterval(interval) + // this will poll the logs _every_ second instead of just once since a poll + // changes `loadNew` so we will run the effect again with a new timeout after each poll + const poll = async () => { await loadNew().catch(console.error) } + const timeout = setTimeout(poll, 1_000) + return () => { clearTimeout(timeout) } } }, [me?.id, router.pathname, loadNew])