From 7121e0a75ca5db5e14b4780e0ef9bfa7002a4d47 Mon Sep 17 00:00:00 2001 From: Santo Shakil Date: Tue, 4 Aug 2026 20:59:19 +0600 Subject: [PATCH] fix(mobile): run one more sync round when a request arrives mid sync (#30478) * fix(mobile): run one more sync round when a request arrives mid sync * fix(mobile): only queue the extra sync round when the caller asks for it * fix(mobile): skip the queued sync round when the sync fails * clear the sync task in whenComplete and gate the queued round on the result * simplify the queued flag gate --- mobile/lib/domain/utils/background_sync.dart | 12 ++++++++++-- mobile/lib/providers/websocket.provider.dart | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/mobile/lib/domain/utils/background_sync.dart b/mobile/lib/domain/utils/background_sync.dart index 82f397d9b6..db0d93c92f 100644 --- a/mobile/lib/domain/utils/background_sync.dart +++ b/mobile/lib/domain/utils/background_sync.dart @@ -28,6 +28,7 @@ class BackgroundSyncManager { final SyncErrorCallback? onCloudIdSyncError; Cancelable? _syncTask; + bool _syncQueued = false; Cancelable? _syncWebsocketTask; Cancelable? _cloudIdSyncTask; Cancelable? _deviceAlbumSyncTask; @@ -50,6 +51,7 @@ class BackgroundSyncManager { }); Future cancel() async { + _syncQueued = false; final tasks = [ _syncTask, _syncWebsocketTask, @@ -133,8 +135,9 @@ class BackgroundSyncManager { }); } - Future syncRemote() { + Future syncRemote({bool enqueue = false}) { if (_syncTask != null) { + _syncQueued |= enqueue; return _syncTask!.future.then((result) => result ?? false).catchError((_) => false); } @@ -148,15 +151,20 @@ class BackgroundSyncManager { .then((result) { final success = result ?? false; onRemoteSyncComplete?.call(success); + _syncQueued &= success; return success; }) .catchError((error) { onRemoteSyncError?.call(error.toString()); - _syncTask = null; + _syncQueued = false; return false; }) .whenComplete(() { _syncTask = null; + if (_syncQueued) { + _syncQueued = false; + unawaited(syncRemote()); + } }); } diff --git a/mobile/lib/providers/websocket.provider.dart b/mobile/lib/providers/websocket.provider.dart index c6ed09360a..05d1f3aac1 100644 --- a/mobile/lib/providers/websocket.provider.dart +++ b/mobile/lib/providers/websocket.provider.dart @@ -187,7 +187,7 @@ class WebsocketNotifier extends StateNotifier { } void _handleRemoteChange(dynamic _) { - unawaited(_ref.read(backgroundSyncProvider).syncRemote()); + unawaited(_ref.read(backgroundSyncProvider).syncRemote(enqueue: true)); } void _handleSyncAssetEditReadyV2(dynamic data) {