onRecv<T> method

SelectBuilder<R> onRecv<T>(
  1. Receiver<T> rx,
  2. FutureOr<R> body(
    1. RecvResult<T>
    ), {
  3. Object? tag,
  4. bool if_()?,
})

Race a channel receiver - handles both success and error cases.

Waits for any message from the channel (successful value or error). Use this when you need to handle both normal messages and channel errors.

Parameters:

  • rx: Receiver to wait on
  • body: Function to call with the RecvResult
  • tag: Optional tag for debugging
  • if_: Optional guard condition

Example:

await XSelect.run<String>((s) => s
  ..onRecv(taskResults, (result) => {
      result.valueOrNull != null
        ? 'Task completed: ${result.valueOrNull}'
        : 'Task failed: ${result.errorOrNull}'
    })
  ..onTimeout(Duration(minutes: 1), () => 'Task timeout')
);

See also:

Implementation

SelectBuilder<R> onRecv<T>(
  Receiver<T> rx,
  FutureOr<R> Function(RecvResult<T>) body, {
  Object? tag,
  bool Function()? if_,
}) {
  _branches.add((RecvBranch<T, R>(rx, body, tag: tag), if_));
  return this;
}