Skip to content

Fix stale time deltas after browser sleep/tab suspension - #3149

Open
jakobbg wants to merge 2 commits into
Novik:masterfrom
jakobbg:fix-deltatime
Open

Fix stale time deltas after browser sleep/tab suspension#3149
jakobbg wants to merge 2 commits into
Novik:masterfrom
jakobbg:fix-deltatime

Conversation

@jakobbg

@jakobbg jakobbg commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

deltaTime and serverDeltaTime are computed once on the first AJAX response and cached for the lifetime of the page. When the browser sleeps or the tab is suspended, the cached values become stale, causing all displayed timestamps to be offset by the sleep duration.

Reset both values to 0 on visibilitychange so Ajax_UpdateTime recalculates them from the next fresh response.

Co-authored-by: AI (Gemini via Antigravity IDE)

deltaTime and serverDeltaTime are computed once on the first AJAX
response and cached for the lifetime of the page. When the browser
sleeps or the tab is suspended, the cached values become stale,
causing all displayed timestamps to be offset by the sleep duration.

Reset both values to 0 on visibilitychange so Ajax_UpdateTime
recalculates them from the next fresh response.

Co-authored-by: AI (Gemini via Antigravity IDE)
Copilot AI review requested due to automatic review settings August 3, 2026 21:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses incorrect UI timestamps after browser sleep/tab suspension by forcing a recalculation of cached client/server time deltas when the page becomes visible again.

Changes:

  • Add a visibilitychange listener to reset theWebUI.deltaTime and theWebUI.serverDeltaTime when the tab becomes visible.
  • Ensure subsequent AJAX responses recompute time deltas via the existing Ajax_UpdateTime logic.
Suppressed comments (1)

js/rtorrent.js:1386

  • js/rtorrent.js is loaded before js/webui.js (see index.html preload order), so visibilitychange can fire while theWebUI is still undefined (e.g., page opened in a background tab). That would throw a ReferenceError and potentially break further initialization. Guard access to theWebUI (or use window.theWebUI) before resetting the deltas.
	if(!document.hidden)
	{
		theWebUI.deltaTime = 0;
		theWebUI.serverDeltaTime = 0;
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread js/rtorrent.js
Add tests covering the visibilitychange event handler in rtorrent.js
that resets deltaTime and serverDeltaTime when a tab becomes visible
after browser sleep or tab suspension.

- Verify both deltas are reset to 0 when document becomes visible,
  ensuring Ajax_UpdateTime will recompute them on the next request
- Verify deltas are preserved when the tab is hidden (negative case)

Generated with agentic AI (Google Antigravity)
@xirvik

xirvik commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

One issue: the test leaks its mock.

beforeEach saves the descriptor from Document.prototype, but the tests define hidden on the instance (document), and afterEach restores only the prototype — the own property is never removed. The second test happens to redefine it, which is why nothing fails today, but a third test that expects real document.hidden would inherit the stub. Adding delete document.hidden; to afterEach fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants