perform<T> method

FutureOr<void> perform<T>({
  1. void beforeAction()?,
  2. FutureOr<T> action()?,
  3. void onResult(
    1. T result
    )?,
  4. void onError(
    1. Object e
    )?,
  5. void afterAction()?,
  6. Function? methodSignature,
})

Runs the given action and updates the state of the presenter. It can handle optional beforeAction, afterAction, and onError callbacks. Optionally, it can also provide an exactNotification callback to notify only a specific listener.

Returns a FutureOr that completes when the action has been performed and the state has been updated. Runs an action and notifies the relevant listeners of any changes that occur

Implementation

FutureOr<void> perform<T>({
  void Function()? beforeAction,
  FutureOr<T> Function()? action,
  void Function(T result)? onResult,
  void Function(Object e)? onError,
  void Function()? afterAction,
  Function? methodSignature,
}) async {
  if (beforeAction != null) {
    _runAndShakeIfNotDisposed(beforeAction);
  }
  if (action == null) return;
  _setProcessingState(methodSignature, true);
  try {
    late T result;
    if (action is Future<T> Function()) {
      result = await action();
    } else if (action is T Function()) {
      result = action();
    }
    if (onResult != null) {
      _runAndShakeIfNotDisposed(() => onResult(result));
    }
  } catch (e) {
    if (onError == null) throw e;
    _runAndShakeIfNotDisposed(() => onError(e));
  }
  _setProcessingState(methodSignature, false);
  if (afterAction != null) {
    _runAndShakeIfNotDisposed(afterAction);
  }
}