perf(app): the dashboard works itself out once a burst of changes ends

The home tab stays open below a study session, and every rating changed
what it shows; it now waits until changes pause for 300 ms, so a large
library isn't read anew after each rating.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-01 22:15:27 +02:00
commit a52e96b27c
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -57,8 +57,24 @@ class HomeBloc(
/// out never overwrites a newer one. /// out never overwrites a newer one.
var _refreshes = 0; var _refreshes = 0;
/// Waits for a burst of changes to end, e.g. ratings in a study session
/// while this tab stays open below it, so the dashboard is worked out once
/// rather than after every one.
Timer? _settling;
/// How long changes must pause before the dashboard is worked out anew.
static const settleTime = Duration(milliseconds: 300);
void _onStarted(HomeEventStarted event, Emitter<HomeState> emit) { void _onStarted(HomeEventStarted event, Emitter<HomeState> emit) {
void changed(Object? _) => add(const HomeEventChanged()); void changed(Object? _) {
_settling?.cancel();
_settling = Timer(settleTime, () {
if (!isClosed) {
add(const HomeEventChanged());
}
});
}
_subscriptions.addAll([ _subscriptions.addAll([
_reviews.changes.listen(changed), _reviews.changes.listen(changed),
_cards.changes.listen(changed), _cards.changes.listen(changed),
@ -125,6 +141,7 @@ class HomeBloc(
@override @override
Future<void> close() async { Future<void> close() async {
_settling?.cancel();
for (final subscription in _subscriptions) { for (final subscription in _subscriptions) {
await subscription.cancel(); await subscription.cancel();
} }