latest<T> static method

(MpscSender<T>, MpscReceiver<T>) latest<T>({
  1. String? metricsId,
})

Creates a latest-only MPSC channel for real-time state updates.

Only retains the most recent message - each send overwrites the previous. Perfect for state updates, progress indicators, and real-time data where only the current value matters.

Characteristics:

  • Zero memory growth: Buffer size is always 0 or 1
  • No blocking: Sends never block regardless of consumer speed
  • Always current: Consumer gets the latest state, never stale data
  • Coalescing: Rapid updates are automatically merged

Example - UI state updates:

final (statusTx, statusRx) = Mpsc.latest<AppState>();

// Multiple components update state rapidly
statusTx.send(AppState(loading: true));
statusTx.send(AppState(loading: false, data: result)); // Overwrites previous
statusTx.send(AppState(loading: false, data: newResult)); // Overwrites again

// UI always gets current state, never outdated
await for (final state in statusRx.stream()) {
  rebuildUI(state); // Only called with latest state
}

Example - Progress monitoring:

final (progressTx, progressRx) = Mpsc.latest<Progress>();

// Background task reports progress frequently
for (int i = 0; i <= 100; i++) {
  await doWork();
  progressTx.send(Progress(percent: i)); // Overwrites automatically
}

// UI shows smooth progress without lag
await for (final progress in progressRx.stream()) {
  updateProgressBar(progress.percent);
}

Implementation

static (MpscSender<T>, MpscReceiver<T>) latest<T>({String? metricsId}) {
  final buf = LatestOnlyBuffer<T>();
  final core = _MpscCore<T>(buf, metricsId: metricsId);
  final tx = core.attachSender((c) => MpscSender<T>._(c));
  final rx = core.attachReceiver((c) => MpscReceiver<T>._(c));
  return (tx, rx);
}