perf(collections): coalesce duplicate refresh triggers during in-flight loads

This commit is contained in:
Lukas Müllner 2026-03-05 14:47:02 +01:00
parent 7e798cd7e6
commit ff9aa57548

View file

@ -21,6 +21,8 @@ class CollectionsScreenState extends State<CollectionsScreen>
with WidgetsBindingObserver {
List<Collection> _collections = [];
bool _isLoading = true;
bool _isLoadInFlight = false;
bool _reloadQueued = false;
String? _error;
String? _activeCollectionId;
DateTime _lastLoadedAt = DateTime.fromMillisecondsSinceEpoch(0);
@ -61,11 +63,20 @@ class CollectionsScreenState extends State<CollectionsScreen>
}
}
Future<void> _load() async {
setState(() {
_isLoading = true;
_error = null;
});
Future<void> _load({bool showLoading = true}) async {
if (_isLoadInFlight) {
_reloadQueued = true;
return;
}
_isLoadInFlight = true;
if (showLoading) {
setState(() {
_isLoading = true;
_error = null;
});
}
try {
var list = await CollectionService.getMyCollections();
@ -119,6 +130,12 @@ class CollectionsScreenState extends State<CollectionsScreen>
_isLoading = false;
});
logError('collections.load', e);
} finally {
_isLoadInFlight = false;
if (_reloadQueued) {
_reloadQueued = false;
Future<void>.microtask(() => _load(showLoading: false));
}
}
}