From 4bb837875c04f8cc28acb14cf2d362e033b697ee Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Wed, 28 Apr 2021 17:49:55 +0800 Subject: [PATCH] Rescan only after multiple metadata updates --- lib/bloc/scan_dir.dart | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/bloc/scan_dir.dart b/lib/bloc/scan_dir.dart index 189e6ebb..9ff989be 100644 --- a/lib/bloc/scan_dir.dart +++ b/lib/bloc/scan_dir.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:bloc/bloc.dart'; @@ -165,6 +166,7 @@ class ScanDirBloc extends Bloc { close() { _fileRemovedEventListener.end(); _fileMetadataUpdatedEventListener.end(); + _metadataUpdatedSubscription?.cancel(); return super.close(); } @@ -218,7 +220,19 @@ class ScanDirBloc extends Bloc { // 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 _queryOffline( @@ -251,5 +265,8 @@ class ScanDirBloc extends Bloc { AppEventListener _fileRemovedEventListener; AppEventListener _fileMetadataUpdatedEventListener; + int _successiveMetadataUpdatedCount = 0; + StreamSubscription _metadataUpdatedSubscription; + static final _log = Logger("bloc.scan_dir.ScanDirBloc"); }