firstEvent<T> method

Future<T?> firstEvent<T>(
  1. dynamic responseHandler(
    1. T
    ), {
  2. Duration? timeout = const Duration(seconds: 5),
})

Implementation

Future<T?> firstEvent<T>(Function(T) responseHandler, {Duration? timeout = const Duration(seconds: 5)}) {
  if (timeout != null) {
    return firstWhere((element) => element is T).timeout(
      timeout,
      onTimeout: () {
        return Future<T>.error("Timeout waiting for event of type $T");
      },
    ).then((e) => responseHandler(e));
  } else {
    return firstWhere((element) => element is T).then((e) => responseHandler(e));
  }
}