perf(collections): coalesce duplicate refresh triggers during in-flight loads
This commit is contained in:
parent
7e798cd7e6
commit
ff9aa57548
1 changed files with 22 additions and 5 deletions
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue