SelectBuilder<R> class

Builder for composing async selections with type-safe operations.

Provides a fluent DSL for racing multiple async operations. Each operation type has specialized methods with automatic resource cleanup and cancellation.

Available Operations

Channel Operations:

  • onRecv - Wait for any channel message (Success/Failure)
  • onRecvValue - Wait only for successful channel messages
  • onRecvError - Wait only for channel errors

Future Operations:

Stream Operations:

Timer Operations:

  • onTick - Wait for timer tick
  • onDelay - Add fixed delay branch
  • onTimeout - Add timeout to the entire selection

Example - Multi-source data processing:

final processor = DataProcessor();

while (processor.isRunning) {
  final action = await XSelect.run<ProcessorAction>((s) => s
    // High-priority user commands
    ..onRecvValue(userCommands, (cmd) => ProcessorAction.userCommand(cmd))

    // Background data processing
    ..onStream(dataStream, (data) => ProcessorAction.processData(data))

    // Periodic maintenance
    ..onTick(Duration(days: 5), () => ProcessorAction.maintenance())

    // API health checks
    ..onFuture(healthCheck(), (status) => ProcessorAction.healthUpdate(status))

    // Graceful shutdown on idle
    ..onTimeout(Duration(minutes: 10), () => ProcessorAction.shutdown())
  );

  await processor.handleAction(action);
}

Constructors

SelectBuilder.new()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDelay(Duration d, FutureOr<R> body(), {Object? tag, bool if_()?}) SelectBuilder<R>
Add a fixed delay branch to the selection.
onFuture<T>(Future<T> fut, FutureOr<R> body(T), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a Future - handles both success and error cases.
onFutureError<T>(Future<T> fut, FutureOr<R> body(Object error), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a Future, but only handle errors.
onFutureValue<T>(Future<T> fut, FutureOr<R> body(T), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a Future, but only handle successful completion.
onNotify(Notify notify, FutureOr<R> action(), {Object? tag, bool if_()?}) SelectBuilder<R>
Adds a branch that becomes ready when the given Notify is triggered.
onNotifyOnce(Notify notify, FutureOr<R> action(), {Object? tag, bool if_()?}) SelectBuilder<R>
Adds a branch that listens to Notify but only once in the lifetime of this builder instance (useful for one-shot shutdown signals).
onRecv<T>(Receiver<T> rx, FutureOr<R> body(RecvResult<T>), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a channel receiver - handles both success and error cases.
onRecvError<T>(Receiver<T> rx, FutureOr<R> body(RecvError error), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a channel receiver - only handle error messages.
onRecvValue<T>(Receiver<T> rx, FutureOr<R> body(T), {Object? tag, FutureOr<R> onError(RecvError e)?, FutureOr<R> onDisconnected()?, bool if_()?}) SelectBuilder<R>
Race a channel receiver - only handle successful messages.
onSend<T>(Sender<T> sender, T value, FutureOr<R> body(), {Object? tag, bool if_()?}) SelectBuilder<R>
Wait for a channel send operation to complete.
onStream<T>(Stream<T> stream, FutureOr<R> body(T), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a Stream - waits for the next event.
onStreamDone<T>(Stream<T> stream, FutureOr<R> body(), {Object? tag, bool if_()?}) SelectBuilder<R>
Race a Stream - waits for completion (done event).
onTick(Duration d, FutureOr<R> body(), {Object? tag, bool if_()?}) SelectBuilder<R>
Wait for a ticker to fire.
onTimeout(Duration d, FutureOr<R> body()) SelectBuilder<R>
Add a global timeout to the entire selection.
ordered() SelectBuilder<R>
Preserve declaration order instead of applying fairness rotation.
run() Future<R>
syncRun() → R?
Synchronous fast-path over attachSync only.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited