From 5cbb9c2a80686efa391bc65acb5c5d7bb2040e98 Mon Sep 17 00:00:00 2001 From: Harsh Dev Pathak Date: Wed, 15 Oct 2025 17:47:19 +0530 Subject: [PATCH] fix: Prevent watch function to spam api --- lib/index.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 651dad3..87d5d15 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -98,9 +98,25 @@ export async function* watch( while (!abortController.signal.aborted) { const now = Date.now() - await sleep(roundTime(info, currentRound) - now) - const beacon = await retryOnError(async () => client.get(currentRound), options.retriesOnFailure) + // Computing which round should be next based on real time + const expectedRound = roundAt(now, info) + + // If we've fallen behind just skip the old rounds + if (expectedRound > currentRound) { + currentRound = expectedRound + } + + const waitTime = roundTime(info, currentRound) - now + if (waitTime > 0) { + await sleep(waitTime) + } + + const beacon = await retryOnError( + async () => client.get(currentRound), + options.retriesOnFailure, + ) + yield validatedBeacon(client, beacon, currentRound) currentRound = currentRound + 1 }