fix: queue drain continues in background tabs and across chat switches#55
Open
samueljklee wants to merge 1 commit intomainfrom
Open
fix: queue drain continues in background tabs and across chat switches#55samueljklee wants to merge 1 commit intomainfrom
samueljklee wants to merge 1 commit intomainfrom
Conversation
c41f142 to
e26ecbf
Compare
(includes debug logging for background drain - remove before merge) 🤖 Generated with [Amplifier](https://github.com/microsoft/amplifier) Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
e26ecbf to
58870aa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the browser tab is not in focus, queued chat messages don't get sent. Two scenarios:
Tab not visible (switched to another app): The queue countdown uses recursive
setTimeout(tick, 1000). Browsers aggressively throttlesetTimeoutin background tabs — after ~5 minutes, Chrome throttles to 1 per minute, and with Tab Freezing, JS can be completely paused. A 5-second countdown could take minutes or never fire.Switched to another chat (same browser tab): When creating a new chat, the previous session's queue was actively cleared (
clearSessionQueue(prevKey)), deleting all queued messages.Changes
Wall-clock countdown —
startCountdownnow usesDate.now()vs a fixed deadline instead of tick-decrement. Even if the browser throttlessetTimeoutfrom 1s to 60s, the next tick sees real wall time has elapsed and fires the callback immediately.Resume drain on tab focus —
visibilitychangehandler now callstryDrainQueue()when the tab regains focus, catching any timers that were frozen while backgrounded.Preserve queue across sessions — Removed
clearSessionQueue(prevKey)when creating a new chat. Queue items are preserved and drain when the user switches back.Clearer drain guards — Split the combined guard into two explicit checks: session-switched-away (preserves queue for later) vs execution-started (backs off).
Testing