toChannel<T> method
(dynamic, dynamic)
toChannel<T>(
- ChannelType type, {
- int? capacity,
- DropPolicy policy = DropPolicy.block,
- OnDrop<
T> ? onDrop, - bool chunked = true,
- bool strict = true,
- String? metricsId,
Convert port messages to a channel with unified API.
Modern, unified approach that replaces toMpsc and toMpmc.
When strict == true
, the port is closed on unexpected message types.
Parameters:
type
: Channel type (mpsc or mpmc)capacity
: Buffer size (null = unbounded, 0 = rendezvous)policy
: Drop policy for bounded channelsonDrop
: Optional callback for dropped itemschunked
: Use optimized chunked bufferstrict
: Close port on type mismatches
Example:
// Worker isolate event processing
final rp = ReceivePort();
final (tx, rx) = rp.toChannel<WorkerCommand>(
type: ChannelType.mpsc,
capacity: 1000,
);
// Structured command processing
await for (final cmd in rx.stream()) {
switch (cmd.type) {
case 'process': await handleProcess(cmd);
case 'shutdown': return;
}
}
Implementation
(dynamic, dynamic) toChannel<T>(
ChannelType type, {
int? capacity,
DropPolicy policy = DropPolicy.block,
OnDrop<T>? onDrop,
bool chunked = true,
bool strict = true,
String? metricsId,
}) {
switch (type) {
case ChannelType.mpsc:
return toMpsc<T>(
capacity: capacity,
policy: policy,
onDrop: onDrop,
chunked: chunked,
strict: strict,
metricsId: metricsId);
case ChannelType.mpmc:
return toMpmc<T>(
capacity: capacity,
policy: policy,
onDrop: onDrop,
chunked: chunked,
strict: strict,
metricsId: metricsId);
}
}