fix: suppress position sync when app backgrounded

This commit is contained in:
edde746
2026-04-16 15:43:08 +02:00
parent 34815f22ee
commit f055759c7a
3 changed files with 25 additions and 1 deletions
@@ -79,6 +79,9 @@ class WatchTogetherSyncManager {
// Whether we've announced our player as ready (first buffering: false)
bool _hasAnnouncedReady = false;
// Whether the app is backgrounded (suppress heartbeats to avoid stale positions)
bool _backgrounded = false;
// Callbacks
SessionConfigCallback? onSessionConfigReceived;
SyncStateCallback? onSyncStateChanged;
@@ -204,6 +207,7 @@ class WatchTogetherSyncManager {
}
_positionSyncTimer?.cancel();
_positionSyncTimer = null;
_backgrounded = false;
appLogger.d('WatchTogether: Player detached');
}
@@ -313,7 +317,7 @@ class WatchTogetherSyncManager {
void _startPositionSync() {
_positionSyncTimer?.cancel();
_positionSyncTimer = Timer.periodic(positionSyncInterval, (_) {
if (_player != null && _session.isHost) {
if (_player != null && _session.isHost && !_backgrounded) {
_peerService.broadcast(
SyncMessage.positionSync(
_player!.state.position,
@@ -956,6 +960,16 @@ class WatchTogetherSyncManager {
}
}
/// Suppress heartbeats while the app is backgrounded.
///
/// macOS App Nap can throttle the event loop, causing stale position reads.
/// Guests would drift-correct to the stale position every heartbeat, making
/// playback loop. Pausing heartbeats avoids this; drift correction catches
/// up when the app returns to the foreground.
void setBackgrounded(bool value) {
_backgrounded = value;
}
/// Re-announce player readiness after reconnect.
///
/// During reconnect the host resets our _peerReady entry to false via