diff --git a/lib/index.ts b/lib/index.ts index faeae63..aab4820 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -94,15 +94,21 @@ export async function* watch( options: WatchOptions = defaultWatchOptions, ): AsyncGenerator { const info = await client.chain().info() - let currentRound = roundAt(Date.now(), info) - + while (!abortController.signal.aborted) { const now = Date.now() - await sleep(roundTime(info, currentRound) - now) - - const beacon = await retryOnError(async () => client.get(currentRound), options.retriesOnFailure) - yield validatedBeacon(client, beacon, currentRound) - currentRound = currentRound + 1 + const targetRound = roundAt(now, info) + + const nextRoundTime = roundTime(info, targetRound) + const waitTime = nextRoundTime - now + + await sleep(Math.max(0, waitTime)) + + const beacon = await retryOnError( + async () => client.get(targetRound), + options.retriesOnFailure + ) + yield validatedBeacon(client, beacon, targetRound) } }