onDelay method

SelectBuilder<R> onDelay(
  1. Duration d,
  2. FutureOr<R> body(), {
  3. Object? tag,
  4. bool if_()?,
})

Add a fixed delay branch to the selection.

Creates a timer that fires once after the specified duration. Use this for timeouts, periodic actions, or adding delays to processing.

Parameters:

  • d: Duration to wait before firing
  • body: Function to call when the timer fires
  • tag: Optional tag for debugging
  • if_: Optional guard condition

Example:

await XSelect.run<String>((s) => s
  ..onStream(fastStream, (data) => 'Fast: $data')
  ..delay(Duration(milliseconds: 100), () => 'Throttled')
  ..delay(Duration(seconds: 1), () => 'Periodic check')
  ..delay(Duration(seconds: 30), () => 'Timeout reached')
);

See also:

  • onTimeout - Global timeout for the entire selection
  • onTick - For recurring timer events

Implementation

SelectBuilder<R> onDelay(Duration d, FutureOr<R> Function() body,
    {Object? tag, bool Function()? if_}) {
  _branches.add((TimerBranch<R>.once(d, body, tag: tag), if_));
  return this;
}