runWithCallbacks<A, E, R> method

Future<void> runWithCallbacks<A, E, R>(
  1. Effect<A, E, R> effect, {
  2. void onSuccess(
    1. A
    )?,
  3. void onFailure(
    1. E
    )?,
  4. void onDefect(
    1. Object
    )?,
  5. Context<R>? context,
})

Runs an effect and handles success/failure with callbacks

Implementation

Future<void> runWithCallbacks<A, E, R>(
  Effect<A, E, R> effect, {
  void Function(A)? onSuccess,
  void Function(E)? onFailure,
  void Function(Object)? onDefect,
  Context<R>? context,
}) async {
  final exit = await effect.runToExit(context);

  switch (exit) {
    case Success(:final value):
      onSuccess?.call(value);
    case Failure(:final cause):
      switch (cause) {
        case Fail(:final error):
          onFailure?.call(error);
        case Die(:final throwable):
          onDefect?.call(throwable);
      }
  }
}