mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-04 03:27:09 -05:00 
			
		
		
		
	* refactor: user entity * chore: rebase fixes * refactor: remove int user Id * refactor: migrate store userId from int to string * refactor: rename uid to id * feat: drift * pr feedback * refactor: move common overrides to mixin * refactor: remove int user Id * refactor: migrate store userId from int to string * refactor: rename uid to id * feat: user & partner sync stream * pr changes * refactor: sync service and add tests * chore: remove generated change * chore: move sync model * rebase: convert string ids to byte uuids * rebase * add processing logs * batch db calls * rewrite isolate manager * rewrite with worker_manager * misc fixes * add sync order test --------- Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Co-authored-by: Alex <alex.tran1502@gmail.com>
		
			
				
	
	
		
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
import 'dart:async';
 | 
						|
import 'dart:ui';
 | 
						|
 | 
						|
import 'package:flutter/services.dart';
 | 
						|
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
						|
import 'package:immich_mobile/providers/db.provider.dart';
 | 
						|
import 'package:immich_mobile/providers/infrastructure/cancel.provider.dart';
 | 
						|
import 'package:immich_mobile/providers/infrastructure/db.provider.dart';
 | 
						|
import 'package:immich_mobile/utils/bootstrap.dart';
 | 
						|
import 'package:logging/logging.dart';
 | 
						|
import 'package:worker_manager/worker_manager.dart';
 | 
						|
 | 
						|
class InvalidIsolateUsageException implements Exception {
 | 
						|
  const InvalidIsolateUsageException();
 | 
						|
 | 
						|
  @override
 | 
						|
  String toString() =>
 | 
						|
      "IsolateHelper should only be used from the root isolate";
 | 
						|
}
 | 
						|
 | 
						|
// !! Should be used only from the root isolate
 | 
						|
Cancelable<T?> runInIsolateGentle<T>({
 | 
						|
  required Future<T> Function(ProviderContainer ref) computation,
 | 
						|
  String? debugLabel,
 | 
						|
}) {
 | 
						|
  final token = RootIsolateToken.instance;
 | 
						|
  if (token == null) {
 | 
						|
    throw const InvalidIsolateUsageException();
 | 
						|
  }
 | 
						|
 | 
						|
  return workerManager.executeGentle((cancelledChecker) async {
 | 
						|
    BackgroundIsolateBinaryMessenger.ensureInitialized(token);
 | 
						|
    DartPluginRegistrant.ensureInitialized();
 | 
						|
 | 
						|
    final db = await Bootstrap.initIsar();
 | 
						|
    await Bootstrap.initDomain(db, shouldBufferLogs: false);
 | 
						|
    final ref = ProviderContainer(
 | 
						|
      overrides: [
 | 
						|
        // TODO: Remove once isar is removed
 | 
						|
        dbProvider.overrideWithValue(db),
 | 
						|
        isarProvider.overrideWithValue(db),
 | 
						|
        cancellationProvider.overrideWithValue(cancelledChecker),
 | 
						|
      ],
 | 
						|
    );
 | 
						|
 | 
						|
    Logger log = Logger("IsolateLogger");
 | 
						|
 | 
						|
    try {
 | 
						|
      return await computation(ref);
 | 
						|
    } on CanceledError {
 | 
						|
      log.warning(
 | 
						|
        "Computation cancelled ${debugLabel == null ? '' : ' for $debugLabel'}",
 | 
						|
      );
 | 
						|
    } catch (error, stack) {
 | 
						|
      log.severe(
 | 
						|
        "Error in runInIsolateGentle ${debugLabel == null ? '' : ' for $debugLabel'}",
 | 
						|
        error,
 | 
						|
        stack,
 | 
						|
      );
 | 
						|
    } finally {
 | 
						|
      // Wait for the logs to flush
 | 
						|
      await Future.delayed(const Duration(seconds: 2));
 | 
						|
      // Always close the new db connection on Isolate end
 | 
						|
      ref.read(driftProvider).close();
 | 
						|
      ref.read(isarProvider).close();
 | 
						|
    }
 | 
						|
    return null;
 | 
						|
  });
 | 
						|
}
 |