Rescan only after multiple metadata updates

This commit is contained in:
Ming Ming 2021-04-28 17:49:55 +08:00
parent 63772e6e17
commit 4bb837875c

View file

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:bloc/bloc.dart';
@ -165,6 +166,7 @@ class ScanDirBloc extends Bloc<ScanDirBlocEvent, ScanDirBlocState> {
close() {
_fileRemovedEventListener.end();
_fileMetadataUpdatedEventListener.end();
_metadataUpdatedSubscription?.cancel();
return super.close();
}
@ -218,7 +220,19 @@ class ScanDirBloc extends Bloc<ScanDirBlocEvent, ScanDirBlocState> {
// no data in this bloc, ignore
return;
}
add(_ScanDirBlocExternalEvent());
_successiveMetadataUpdatedCount += 1;
_metadataUpdatedSubscription?.cancel();
// only trigger the event on the 10th update or 10s after the last update
if (_successiveMetadataUpdatedCount % 10 == 0) {
add(_ScanDirBlocExternalEvent());
} else {
_metadataUpdatedSubscription =
Future.delayed(const Duration(seconds: 10)).asStream().listen((_) {
add(_ScanDirBlocExternalEvent());
_successiveMetadataUpdatedCount = 0;
});
}
}
Stream<ScanDirBlocState> _queryOffline(
@ -251,5 +265,8 @@ class ScanDirBloc extends Bloc<ScanDirBlocEvent, ScanDirBlocState> {
AppEventListener<FileRemovedEvent> _fileRemovedEventListener;
AppEventListener<FileMetadataUpdatedEvent> _fileMetadataUpdatedEventListener;
int _successiveMetadataUpdatedCount = 0;
StreamSubscription<void> _metadataUpdatedSubscription;
static final _log = Logger("bloc.scan_dir.ScanDirBloc");
}