execute method

Future<void> execute(
  1. Future<S> func(), {
  2. Duration delay = const Duration(milliseconds: 50),
})
inherited

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, force: true);
      }
    },
    onError: (error, __) {
      setError(error, force: true);
      setLoading(false, force: true);
    },
  ).valueOrCancellation();
}