channel<T> static method

(OneShotSender<T>, OneShotReceiver<T>) channel<T>({
  1. bool consumeOnce = false,
  2. String? metricsId,
})

Creates a one-shot channel for single-value delivery.

Parameters:

  • consumeOnce: If true, first receiver consumes and disconnects channel. If false, all receivers get the same value (broadcast).

Returns: Tuple of (sender, receiver) for promise-like communication

Consumption behaviors:

  • consumeOnce: false (default): All receivers get the value
  • consumeOnce: true: Only first receiver gets the value, others disconnected

Example - Request-response:

final (responseTx, responseRx) = OneShot.channel<String>(consumeOnce: true);

// Send single response
await responseTx.send('Operation completed');

// Only one receiver gets it
final result = await responseRx.recv();
print(result.valueOrNull); // 'Operation completed'

Example - Shared resource:

final (configTx, configRx) = OneShot.channel<Config>(consumeOnce: false);

// Initialize once
await configTx.send(loadedConfig);

// Multiple consumers can access
final config1 = (await configRx.recv()).valueOrNull;
final config2 = (await configRx.recv()).valueOrNull; // Same config

Implementation

static (OneShotSender<T>, OneShotReceiver<T>) channel<T>(
    {bool consumeOnce = false, String? metricsId}) {
  final core =
      _OneShotCore<T>(consumeOnce: consumeOnce, metricsId: metricsId);
  final tx = core.attachSender((c) => OneShotSender<T>._(c));
  final rx = core.attachReceiver((c) => OneShotReceiver<T>._(c));
  return (tx, rx);
}