handle method

  1. @override
Future<R> handle(
  1. A args
)

This method iterates through the chained nodes in the precise order they had been chained and calls the IResponsibilityNodes with the argument arg.

If a node returns a successful result, the method stops iterating and returns the result. Otherwise, if the result is not successful or throws an exception is thrown, the method proceeds to the next chained node.

If no chained node succeeded to return a successful result, the result of orElse function is returned.

Implementation

@override
Future<R> handle(A args) async {
  ChainResult<R>? lastResult;

  for (final node in nodes) {
    try {
      lastResult = await node(args);
    } on Exception {
      continue;
    }

    if (lastResult.isSuccessful) return lastResult.value;
  }

  return orElse.call(args);
}