perf: avoid duplicate bootstrap work on join
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:io' show File;
|
||||
import 'dart:io' show Directory, File;
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:flutter/foundation.dart' show visibleForTesting;
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
@@ -9,7 +10,19 @@ import '../src/rust/api.dart' as rust;
|
||||
|
||||
const String appSemverBaseline = 'v0.1.0';
|
||||
const String _sileroVadAsset = 'assets/models/silero_vad.onnx';
|
||||
const String _tenVadAsset = 'assets/models/ten_vad.onnx';
|
||||
|
||||
typedef StorageDirectoryProvider = Future<Directory> Function();
|
||||
typedef StorageInitializer = Future<void> Function(String dir);
|
||||
|
||||
Future<void>? _storageInitFuture;
|
||||
Future<void>? _vadBootstrapFuture;
|
||||
StorageDirectoryProvider _storageDirectoryProvider =
|
||||
getApplicationSupportDirectory;
|
||||
StorageInitializer _storageInitializer = _defaultStorageInitializer;
|
||||
|
||||
Future<void> _defaultStorageInitializer(String dir) {
|
||||
return rust.initStorage(dir: dir);
|
||||
}
|
||||
|
||||
Future<File> _copyBundledAssetToDocuments({
|
||||
required String assetPath,
|
||||
@@ -29,16 +42,28 @@ Future<File> _copyBundledAssetToDocuments({
|
||||
}
|
||||
|
||||
Future<void> configureBundledVadModels() async {
|
||||
final existing = _vadBootstrapFuture;
|
||||
if (existing != null) {
|
||||
await existing;
|
||||
return;
|
||||
}
|
||||
|
||||
final bootstrap = _configureBundledVadModelsImpl();
|
||||
_vadBootstrapFuture = bootstrap;
|
||||
try {
|
||||
await bootstrap;
|
||||
} catch (_) {
|
||||
_vadBootstrapFuture = null;
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _configureBundledVadModelsImpl() async {
|
||||
final silero = await _copyBundledAssetToDocuments(
|
||||
assetPath: _sileroVadAsset,
|
||||
fileName: 'silero_vad.onnx',
|
||||
);
|
||||
final ten = await _copyBundledAssetToDocuments(
|
||||
assetPath: _tenVadAsset,
|
||||
fileName: 'ten_vad.onnx',
|
||||
);
|
||||
await rust.setVadModelPath(path: silero.path);
|
||||
await rust.setTenVadModelPath(path: ten.path);
|
||||
}
|
||||
|
||||
/// Resolve the human-readable app version displayed in About.
|
||||
@@ -69,15 +94,45 @@ String appVersionFromBuildNumber({
|
||||
}
|
||||
|
||||
Future<void> wireStorage() async {
|
||||
final existing = _storageInitFuture;
|
||||
if (existing != null) {
|
||||
await existing;
|
||||
return;
|
||||
}
|
||||
|
||||
final initFuture = _wireStorageImpl();
|
||||
_storageInitFuture = initFuture;
|
||||
await initFuture;
|
||||
}
|
||||
|
||||
Future<void> _wireStorageImpl() async {
|
||||
var initialized = false;
|
||||
try {
|
||||
final dir = await getApplicationSupportDirectory();
|
||||
await rust.initStorage(dir: dir.path);
|
||||
final dir = await _storageDirectoryProvider();
|
||||
await _storageInitializer(dir.path);
|
||||
initialized = true;
|
||||
} catch (_) {
|
||||
// Best-effort; missing storage just means no identity persistence
|
||||
// and no bookmark list this session.
|
||||
} finally {
|
||||
if (!initialized) {
|
||||
_storageInitFuture = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
void debugResetStorageBootstrap({
|
||||
StorageDirectoryProvider? storageDirectoryProvider,
|
||||
StorageInitializer? storageInitializer,
|
||||
}) {
|
||||
_storageInitFuture = null;
|
||||
_vadBootstrapFuture = null;
|
||||
_storageDirectoryProvider =
|
||||
storageDirectoryProvider ?? getApplicationSupportDirectory;
|
||||
_storageInitializer = storageInitializer ?? _defaultStorageInitializer;
|
||||
}
|
||||
|
||||
rust.BridgeNetworkState _mapConnectivity(List<ConnectivityResult> results) {
|
||||
if (results.isEmpty) return rust.BridgeNetworkState.unknown;
|
||||
final allNone = results.every((r) => r == ConnectivityResult.none);
|
||||
|
||||
Reference in New Issue
Block a user