From ed9e99e946b5458946f341bbee9a004049c1a233 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Sun, 28 Apr 2024 16:51:36 +0100 Subject: [PATCH] fix: fluid streaming was "pausing" when tab was not visible --- src/lib/apis/streaming/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/apis/streaming/index.ts b/src/lib/apis/streaming/index.ts index 19e4d8fc7..aad42b2b6 100644 --- a/src/lib/apis/streaming/index.ts +++ b/src/lib/apis/streaming/index.ts @@ -73,7 +73,11 @@ async function* streamLargeDeltasAsRandomChunks( const chunkSize = Math.min(Math.floor(Math.random() * 3) + 1, content.length); const chunk = content.slice(0, chunkSize); yield { done: false, value: chunk }; - await sleep(5); + // Do not sleep if the tab is hidden + // Timers are throttled to 1s in hidden tabs + if (document?.visibilityState !== 'hidden') { + await sleep(5); + } content = content.slice(chunkSize); } }