wait property

Future<T> get wait

Returns a Future that completes when the operation reaches a terminal state.

If the current state is LxSuccess or LxError, the Future completes immediately. Otherwise, it waits for the next transition to a terminal state.

Implementation

Future<T> get wait async {
  final s = value;
  if (s is LxSuccess<T>) return Future.value(s.value);
  if (s is LxError<T>) return Future.error(s.error, s.stackTrace);

  final terminalStatus = await stream.firstWhere(
      (status) => status is LxSuccess<T> || status is LxError<T>,
      orElse: () =>
          throw StateError('Async operation stream closed unexpectedly.'));

  if (terminalStatus is LxSuccess<T>) return terminalStatus.value;
  throw (terminalStatus as LxError<T>).error;
}