immich/mobile/lib/domain/utils/background_sync.dart
shenlong-tanwen 5fc1a63810 Merge branch 'main' into feat/sqlite-device-assets
# Conflicts:
#	mobile/lib/domain/utils/background_sync.dart
2025-04-20 23:17:14 +05:30

56 lines
1.3 KiB
Dart

// ignore_for_file: avoid-passing-async-when-sync-expected
import 'dart:async';
import 'package:immich_mobile/providers/infrastructure/sync.provider.dart';
import 'package:immich_mobile/utils/isolate.dart';
import 'package:worker_manager/worker_manager.dart';
class BackgroundSyncManager {
Cancelable<void>? _syncTask;
Cancelable<void>? _deviceAlbumSyncTask;
BackgroundSyncManager();
Future<void> cancel() {
final futures = <Future>[];
if (_syncTask != null) {
futures.add(_syncTask!.future);
}
_syncTask?.cancel();
_syncTask = null;
return Future.wait(futures);
}
// No need to cancel the task, as it can also be run when the user logs out
Future<void> syncLocal() {
if (_deviceAlbumSyncTask != null) {
return _deviceAlbumSyncTask!.future;
}
_deviceAlbumSyncTask = runInIsolateGentle(
computation: (ref) => ref.read(deviceSyncServiceProvider).sync(),
);
return _deviceAlbumSyncTask!.whenComplete(() {
_deviceAlbumSyncTask = null;
});
}
Future<void> syncRemote() {
if (_syncTask != null) {
return _syncTask!.future;
}
_syncTask = runInIsolateGentle(
computation: (ref) => ref.read(syncStreamServiceProvider).sync(),
);
return _syncTask!.whenComplete(() {
_syncTask = null;
});
}
}