perf: async cache reads in content repository
This commit is contained in:
@@ -26,12 +26,12 @@ class ContentRepository {
|
||||
return SeasonPack.fromJson(jsonDecode(raw) as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
SeasonPack? _loadCachedSeason(Directory dir) {
|
||||
Future<SeasonPack?> _loadCachedSeason(Directory dir) async {
|
||||
try {
|
||||
final file = File('${dir.path}/pack.json');
|
||||
if (!file.existsSync()) return null;
|
||||
if (!await file.exists()) return null;
|
||||
return SeasonPack.fromJson(
|
||||
jsonDecode(file.readAsStringSync()) as Map<String, dynamic>);
|
||||
jsonDecode(await file.readAsString()) as Map<String, dynamic>);
|
||||
} catch (_) {
|
||||
return null; // Corrupt or future-schema pack: ignore.
|
||||
}
|
||||
@@ -51,9 +51,10 @@ class ContentRepository {
|
||||
|
||||
final seasonsDir =
|
||||
cacheDir == null ? null : Directory('${cacheDir!.path}/seasons');
|
||||
if (seasonsDir != null && seasonsDir.existsSync()) {
|
||||
for (final entry in seasonsDir.listSync().whereType<Directory>()) {
|
||||
final pack = _loadCachedSeason(entry);
|
||||
if (seasonsDir != null && await seasonsDir.exists()) {
|
||||
await for (final entry in seasonsDir.list()) {
|
||||
if (entry is! Directory) continue;
|
||||
final pack = await _loadCachedSeason(entry);
|
||||
if (pack != null) byId[pack.seasonId] = pack;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user