execute method
Execute a Future.
This function is a sugar code used to run a Future in a simple way, executing setLoading and adding to setError if an error occurs in Future
Implementation
Future<void> execute(Future<State> Function() func, {Duration delay = const Duration(milliseconds: 50)}) async {
final localTime = DateTime.now();
_mutableObjects.lastExecution = localTime;
await Future.delayed(delay);
if (localTime != _mutableObjects.lastExecution) {
return;
}
setLoading(true);
await _mutableObjects.completerExecution?.cancel();
_mutableObjects.completerExecution = CancelableOperation.fromFuture(func());
await _mutableObjects.completerExecution!.then(
(value) {
if (value is State) {
update(value, force: true);
setLoading(false);
}
},
onError: (error, __) {
if (error is Error) {
setError(error, force: true);
setLoading(false);
} else {
throw Exception('is expected a ${Error.toString()} type, and receipt ${error.runtimeType}');
}
},
).valueOrCancellation();
}