executeEither method

Future<void> executeEither(
  1. Future<EitherAdapter<Error, State>> func(), {
  2. Duration delay = const Duration(milliseconds: 50),
})

Execute a Future Either dartz.

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 Either

Implementation

Future<void> executeEither(Future<EitherAdapter<Error, 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 EitherAdapter<Error, State>) {
        value.fold((e) => setError(e, force: true), (s) => update(s, force: true));
        setLoading(false);
      }
    },
  ).valueOrCancellation();
}