diff --git a/lib/bloc/list_album.dart b/lib/bloc/list_album.dart index e31b2d74..2dac3150 100644 --- a/lib/bloc/list_album.dart +++ b/lib/bloc/list_album.dart @@ -5,6 +5,7 @@ import 'package:nc_photos/entity/album.dart'; import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/entity/file/data_source.dart'; import 'package:nc_photos/event/event.dart'; +import 'package:nc_photos/throttler.dart'; import 'package:nc_photos/use_case/list_album.dart'; import 'package:tuple/tuple.dart'; @@ -99,6 +100,13 @@ class ListAlbumBloc extends Bloc { _albumUpdatedListener.begin(); _fileRemovedListener.begin(); _albumCreatedListener.begin(); + + _refreshThrottler = Throttler( + onTriggered: (_) { + add(_ListAlbumBlocExternalEvent()); + }, + logTag: "ListAlbumBloc.refresh", + ); } @override @@ -116,6 +124,7 @@ class ListAlbumBloc extends Bloc { _albumUpdatedListener.end(); _fileRemovedListener.end(); _albumCreatedListener.end(); + _refreshThrottler.clear(); return super.close(); } @@ -151,7 +160,10 @@ class ListAlbumBloc extends Bloc { // no data in this bloc, ignore return; } - add(_ListAlbumBlocExternalEvent()); + _refreshThrottler.trigger( + maxResponceTime: const Duration(seconds: 3), + maxPendingCount: 10, + ); } void _onFileRemovedEvent(FileRemovedEvent ev) { @@ -160,7 +172,10 @@ class ListAlbumBloc extends Bloc { return; } if (isAlbumFile(ev.account, ev.file)) { - add(_ListAlbumBlocExternalEvent()); + _refreshThrottler.trigger( + maxResponceTime: const Duration(seconds: 3), + maxPendingCount: 10, + ); } } @@ -210,5 +225,7 @@ class ListAlbumBloc extends Bloc { late AppEventListener _fileRemovedListener; late AppEventListener _albumCreatedListener; + late Throttler _refreshThrottler; + static final _log = Logger("bloc.list_album.ListAlbumBloc"); }