when<T> function

FutureOr<T> when<T>(
  1. String description,
  2. FutureOr<T> body()
)

Implementation

FutureOr<T> when<T>(String description, FutureOr<T> Function() body) {
  final whens = Zone.current[whenKey] as List<String>?;

  if (whens == null) throw Exception('Use gwt method');

  final state = Zone.current[stateKey] as State;
  state.key = whenKey;
  whens.add(description);

  try {
    final result = body();
    if (result is Future) {
      return Future.sync(() async => await result);
    } else {
      return result;
    }
  } catch (e) {
    state.throwCount += 1;
    return Future(() {
      state.throwCount -= 1;
      // ignore: use_rethrow_when_possible
      throw e;
    });
  }
}