nc-photos/app/lib/use_case/resync_album.dart
Ming Ming 8ce0125879 Limit number of async queries to be fired at the same time
While the plugin can handle this no problem, the dart side will get stuck when all the results come in over a tiny period of time
2022-05-28 14:38:11 +08:00

65 lines
2.2 KiB
Dart

import 'package:idb_shim/idb_client.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_db.dart';
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
/// Resync files inside an album with the file db
class ResyncAlbum {
const ResyncAlbum(this.appDb);
Future<List<AlbumItem>> call(Account account, Album album) async {
_log.info("[call] Resync album: ${album.name}");
if (album.provider is! AlbumStaticProvider) {
throw ArgumentError(
"Resync only make sense for static albums: ${album.name}");
}
final items = AlbumStaticProvider.of(album).items;
final fileIds =
items.whereType<AlbumFileItem>().map((i) => i.file.fileId!).toList();
final dbItems = Map.fromEntries(await appDb.use(
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
(transaction) async {
final store = transaction.objectStore(AppDb.file2StoreName);
return await fileIds
.mapStream(
(id) async => MapEntry(
id,
await store.getObject(
AppDbFile2Entry.toPrimaryKey(account, id)) as Map?,
),
k.simultaneousQuery)
.toList();
},
));
return items.map((i) {
if (i is AlbumFileItem) {
try {
final dbItem = dbItems[i.file.fileId]!;
final dbEntry =
AppDbFile2Entry.fromJson(dbItem.cast<String, dynamic>());
return i.copyWith(
file: dbEntry.file,
);
} catch (e, stackTrace) {
_log.shout(
"[call] Failed syncing file in album: ${logFilename(i.file.path)}",
e,
stackTrace);
return i;
}
} else {
return i;
}
}).toList();
}
final AppDb appDb;
static final _log = Logger("use_case.resync_album.ResyncAlbum");
}