refactor: optimize application startup sequence and migration handling
- Removed the immediate initialization of SelfSignedCertificateManager to improve the app's first paint performance. - Introduced lazy initialization of the certificate manager after the first frame, enhancing startup efficiency. - Implemented a fast path for migration checks in PersistenceMigrator to skip unnecessary operations if already completed in the current session, improving overall performance during app startup. - Updated comments for clarity on the changes made to the startup sequence and migration process.
This commit is contained in:
@@ -11,13 +11,20 @@ class PersistenceMigrator {
|
||||
PersistenceMigrator({required HiveBoxes hiveBoxes}) : _boxes = hiveBoxes;
|
||||
|
||||
static const int _targetVersion = 1;
|
||||
static bool _migrationComplete = false;
|
||||
|
||||
final HiveBoxes _boxes;
|
||||
|
||||
Future<void> migrateIfNeeded() async {
|
||||
// Fast path: if we already checked migration in this app session, skip
|
||||
if (_migrationComplete) {
|
||||
return;
|
||||
}
|
||||
|
||||
final currentVersion =
|
||||
_boxes.metadata.get(HiveStoreKeys.migrationVersion) as int?;
|
||||
if (currentVersion != null && currentVersion >= _targetVersion) {
|
||||
_migrationComplete = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -34,6 +41,7 @@ class PersistenceMigrator {
|
||||
await _migrateTaskQueue(prefs);
|
||||
|
||||
await _boxes.metadata.put(HiveStoreKeys.migrationVersion, _targetVersion);
|
||||
_migrationComplete = true;
|
||||
|
||||
await _cleanupLegacyKeys(prefs);
|
||||
DebugLogger.log('Migration completed', scope: 'persistence/migration');
|
||||
|
||||
@@ -29,8 +29,6 @@ void main() {
|
||||
() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
SelfSignedCertificateManager.instance.ensureInitialized();
|
||||
|
||||
// Global error handlers
|
||||
FlutterError.onError = (FlutterErrorDetails details) {
|
||||
DebugLogger.error(
|
||||
@@ -59,11 +57,16 @@ void main() {
|
||||
_startupTimeline!.start('app_startup');
|
||||
_startupTimeline!.instant('bindings_initialized');
|
||||
|
||||
// Defer edge-to-edge mode to post-frame to avoid impacting first paint
|
||||
// Defer edge-to-edge mode and certificate manager to post-frame to avoid
|
||||
// impacting first paint
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
// ignore: discarded_futures
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||||
_startupTimeline?.instant('edge_to_edge_enabled');
|
||||
|
||||
// Initialize certificate manager lazily after first frame
|
||||
SelfSignedCertificateManager.instance.ensureInitialized();
|
||||
_startupTimeline?.instant('cert_manager_ready');
|
||||
});
|
||||
|
||||
const secureStorage = FlutterSecureStorage(
|
||||
@@ -80,9 +83,11 @@ void main() {
|
||||
);
|
||||
_startupTimeline!.instant('secure_storage_ready');
|
||||
|
||||
// Initialize Hive (now optimized with migration state caching)
|
||||
final hiveBoxes = await HiveBootstrap.instance.ensureInitialized();
|
||||
_startupTimeline!.instant('hive_ready');
|
||||
|
||||
// Run migration check (now fast-pathed after first run)
|
||||
final migrator = PersistenceMigrator(hiveBoxes: hiveBoxes);
|
||||
await migrator.migrateIfNeeded();
|
||||
_startupTimeline!.instant('migration_complete');
|
||||
|
||||
Submodule openwebui-src updated: 46ae3f4f5d...43a2881074
Reference in New Issue
Block a user