ofArgFunc<Msg, Result, Arg> static method

Cmd<Msg> ofArgFunc<Msg, Result, Arg>(
  1. FutureOr<Result?> task(
    1. Arg
    ),
  2. Arg arg, {
  3. Msg onSuccess(
    1. Result
    )?,
  4. Msg? onMissing,
  5. Msg onException(
    1. Exception
    )?,
  6. Msg onError(
    1. Error
    )?,
})

Call a function that after returning a result, can be used to create and dispatch a new message.

task: Function that takes an input and returns a result that might be turned into a message. arg: Argument to be used when calling the function. onSuccess: Function that returns a message when the function terminated successfully using the returned non-null result from the task. onMissing: Message used when the function terminated successfully and returned a null value. onException: Function that returns a message when the function raised an Exception, using the exception as input. onError: Function that returns a message when the function raised an Error, using the error as input.

Implementation

static Cmd<Msg> ofArgFunc<Msg, Result, Arg>(
    FutureOr<Result?> Function(Arg) task, Arg arg,
    {Msg Function(Result)? onSuccess,
    Msg? onMissing,
    Msg Function(Exception)? onException,
    Msg Function(Error)? onError}) {
  void bind(Dispatch<Msg> dispatch) async {
    try {
      final result = await task(arg);
      if (result != null) {
        if (onSuccess != null) {
          dispatch(onSuccess(result));
        }
      } else {
        if (onMissing != null) {
          dispatch(onMissing);
        }
      }
    } on Exception catch (ex) {
      if (onException != null) {
        dispatch(onException(ex));
      }
    } on Error catch (err) {
      if (onError != null) {
        dispatch(onError(err));
      }
    }
  }

  return Cmd([bind]);
}