perf(home): 冷启动提前拉取首页数据

This commit is contained in:
Zhou Weidong committed 2026-10-20 19:51:41 +08:00
1 parent 1199db760f
commit 9b31d83e39
7 files changed
+255 -113

No files matched your search

+15 -4
View File
@@ -28,6 +28,12 @@ class AppConfig {
static String _runtimeBaseUrl = '';
static Future<void>? _ready;
/// 启动地址探测完成信号;默认已完成,避免影响既有测试与调用方。
static Future<void> get ready => _ready ?? Future<void>.value();
static set ready(Future<void> value) => _ready = value;
static const Duration _probeTimeout = Duration(seconds: 2);
static const List<String> _probeCandidates = [
lanBaseUrl,
@@ -85,10 +91,15 @@ class AppConfig {
for (final candidate in _probeCandidates)
if (candidate != _runtimeBaseUrl) candidate,
];
for (final candidate in candidates) {
final result = probe == null
? await _probeBaseUrl(candidate)
: await _runInjectedProbe(candidate, probe);
final results = await Future.wait([
for (final candidate in candidates)
probe == null
? _probeBaseUrl(candidate)
: _runInjectedProbe(candidate, probe),
]);
for (var index = 0; index < candidates.length; index++) {
final candidate = candidates[index];
final result = results[index];
if (kDebugMode) {
final status = result.ok
? 'ok'
+11 -2
View File
@@ -21,7 +21,7 @@ class ApiClient {
this.tokenRefresher,
this.onUnauthorized,
this.deviceIdReader,
}) : baseUrl = baseUrl ?? AppConfig.apiBaseUrl {
}) : _fixedBaseUrl = baseUrl {
_dio = Dio(
BaseOptions(
baseUrl: this.baseUrl,
@@ -45,9 +45,12 @@ class ApiClient {
}
}
final String baseUrl;
final String? _fixedBaseUrl;
late final Dio _dio;
/// 当前请求使用的服务地址;未显式指定时跟随启动探测结果。
String get baseUrl => _fixedBaseUrl ?? AppConfig.apiBaseUrl;
/// 取当前 access token(未登录返回 null)
final String? Function()? accessTokenReader;
@@ -110,6 +113,7 @@ class ApiClient {
bool retried = false,
bool skipAuthRetry = false,
}) async {
_syncBaseUrl();
final disableAuthRetry = skipAuthRetry || tokenRefresher == null;
try {
final response = await _dio.request<Object?>(
@@ -150,5 +154,10 @@ class ApiClient {
}
}
void _syncBaseUrl() {
if (_fixedBaseUrl != null) return;
_dio.options.baseUrl = AppConfig.apiBaseUrl;
}
void close() => _dio.close(force: true);
}
@@ -35,6 +35,7 @@ class CatalogController extends ChangeNotifier {
debugError = null;
notifyListeners();
try {
await AppConfig.ready;
feed = await repository.fetchHomeFeed();
} on ApiException catch (exception) {
error = exception.message;
+1 -1
View File
@@ -13,6 +13,6 @@ Future<void> main() async {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]),
ServerEndpoint.load(),
]);
await AppConfig.probeAndApplyBaseUrl();
AppConfig.ready = AppConfig.probeAndApplyBaseUrl();
runApp(const LumaDramaApp());
}