MpscSender<T> class final

Multi-producer sender for MPSC channels with cloning capability.

Allows multiple producers to send concurrently to a single consumer. Senders can be cloned to create additional producer handles without affecting the underlying channel.

Key Features

  • Concurrent sends: Multiple senders can send simultaneously
  • Clone support: Create additional sender handles with clone
  • Coordinated shutdown: Channel disconnects when ALL senders close
  • Individual state: Each sender tracks its own closed state

Lifecycle Management

  • Reference counting: Channel stays open while any sender exists
  • Graceful shutdown: Consumer receives RecvErrorDisconnected after last sender closes
  • Resource cleanup: Automatic cleanup when all senders are dropped

Example - Multi-threaded producer pool:

final (baseTx, rx) = Mpsc.unbounded<WorkItem>();

// Create producer pool
final producers = <MpscSender<WorkItem>>[];
for (int i = 0; i < threadCount; i++) {
  producers.add(baseTx.clone());
}
baseTx.close(); // Close base sender

// Each thread gets its own sender
for (int i = 0; i < threadCount; i++) {
  final tx = producers[i];
  threads[i] = Thread(() async {
    while (hasWork) {
      await tx.send(generateWork());
    }
    tx.close(); // Each thread closes its sender
  });
}

// Consumer receives from all producers
await for (final work in rx.stream()) {
  await processWork(work);
} // Stream ends when all senders close
Available extensions

Properties

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

Methods

clone() MpscSender<T>
close() → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send(T v) Future<SendResult>
sendAll(Iterable<T> it) Future<void>

Available on Sender<T>, provided by the SenderBatchX extension

Send all items with backpressure handling.
sendTimeout(T v, Duration d) Future<SendResult>

Available on Sender<T>, provided by the SenderTimeoutX extension

Send a value with a timeout to prevent indefinite blocking.
toString() String
A string representation of this object.
inherited
trySend(T v) SendResult
trySendAll(Iterable<T> it) Future<void>

Available on Sender<T>, provided by the SenderBatchX extension

Send all items using trySend without waiting (best-effort).

Operators

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