toMpmc<T> method

(MpmcSender<T>, MpmcReceiver<T>) toMpmc<T>({
  1. int? capacity,
  2. DropPolicy policy = DropPolicy.block,
  3. OnDrop<T>? onDrop,
  4. bool chunked = true,
  5. bool strict = true,
})

Convert port messages to MPMC channel (legacy method).

Deprecated: Use toChannel with type: ChannelType.mpmc for new code. When strict == true, the port is closed on unexpected message types.

Implementation

(MpmcSender<T>, MpmcReceiver<T>) toMpmc<T>({
  int? capacity,
  DropPolicy policy = DropPolicy.block,
  OnDrop<T>? onDrop,
  bool chunked = true,
  bool strict = true,
}) {
  final (tx, rx) = Mpmc.channel<T>(
    capacity: capacity,
    policy: policy,
    onDrop: onDrop,
    chunked: chunked,
  );
  onmessage = ((MessageEvent msg) {
    final data = msg.data.dartify();
    if (data is T) {
      final r = tx.trySend(data);
      if (r.isDisconnected) close();
    } else if (strict) {
      close();
    }
  }).toJS;
  return (tx, rx);
}