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