sendTimeout method

Future<SendResult> sendTimeout(
  1. T v,
  2. Duration d
)

Send a value with a timeout to prevent indefinite blocking.

Returns SendErrorTimeout if the operation doesn't complete within the specified duration. Useful for implementing deadlock-free systems.

Parameters:

  • v: The value to send
  • d: Maximum time to wait for the send to complete

Example:

// Robust producer with timeout
for (final item in workItems) {
  final result = await tx.sendTimeout(item, Duration(seconds: 10));
  switch (result) {
    case SendOk():
      continue; // Success
    case SendErrorTimeout():
      print('Send timed out, skipping item');
    case SendErrorDisconnected():
      return; // No consumers
  }
}

Implementation

Future<SendResult> sendTimeout(T v, Duration d) async {
  try {
    return await send(v).timeout(
      d,
      onTimeout: () => SendErrorTimeout(d),
    );
  } catch (e) {
    return SendErrorFailed(e);
  }
}