1
0
mirror of https://github.com/immich-app/immich.git synced 2025-07-09 06:25:41 +02:00
Files
immich/mobile/lib/domain/utils/background_sync.dart

38 lines
938 B
Dart
Raw Normal View History

// ignore_for_file: avoid-passing-async-when-sync-expected
import 'dart:async';
import 'package:immich_mobile/providers/infrastructure/sync_stream.provider.dart';
import 'package:immich_mobile/utils/isolate.dart';
import 'package:worker_manager/worker_manager.dart';
class BackgroundSyncManager {
Cancelable<void>? _userSyncTask;
BackgroundSyncManager();
Future<void> cancel() {
final futures = <Future>[];
if (_userSyncTask != null) {
futures.add(_userSyncTask!.future);
}
_userSyncTask?.cancel();
_userSyncTask = null;
return Future.wait(futures);
}
Future<void> syncUsers() {
if (_userSyncTask != null) {
return _userSyncTask!.future;
}
_userSyncTask = runInIsolateGentle(
computation: (ref) => ref.read(syncStreamServiceProvider).syncUsers(),
);
_userSyncTask!.whenComplete(() {
_userSyncTask = null;
});
return _userSyncTask!.future;
}
}