Skip to content

Commit 7760774

Browse files
authored
Fix MultiChannel inner channel type constraint documentation (#2491)
Closes #2401 Addresses a runtime type mismatch issue when using `MultiChannel` with strictly typed channels. - **`example.dart`**: Change the underlying `StreamChannelController` type to `<Object>`, as `MultiChannel` requires a transport channel capable of receiving `List` envelopes. - **`multi_channel.dart`**: Clarifies the `MultiChannel` documentation and updates the code snippet to correctly transmit the channel ID without causing type conflicts.
1 parent 889787a commit 7760774

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎pkgs/stream_channel/example/example.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Future<void> main() async {
6262
//
6363
// A MultiChannel<T> splits events into numbered channels, which are
6464
// instances of VirtualChannel<T>.
65-
var dummyCtrl1 = StreamChannelController<String>();
65+
var dummyCtrl1 = StreamChannelController<Object>();
6666
var multiChannel = MultiChannel<String>(dummyCtrl1.foreign);
6767
var channel1 = multiChannel.virtualChannel();
6868
await multiChannel.sink.close();
@@ -71,7 +71,7 @@ Future<void> main() async {
7171
// the underlying transport, use the corresponding ID's to handle events in
7272
// their respective channels. It is up to you how to communicate channel ID's
7373
// across different endpoints.
74-
var dummyCtrl2 = StreamChannelController<String>();
74+
var dummyCtrl2 = StreamChannelController<Object>();
7575
var multiChannel2 = MultiChannel<String>(dummyCtrl2.foreign);
7676
var channel2 = multiChannel2.virtualChannel(channel1.id);
7777
await channel2.sink.close();

‎pkgs/stream_channel/lib/src/multi_channel.dart‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ import '../stream_channel.dart';
2424
/// ```dart
2525
/// // First endpoint
2626
/// var virtual = multiChannel.virtualChannel();
27-
/// multiChannel.sink.add({
28-
/// "channel": virtual.id
29-
/// });
27+
/// multiChannel.sink.add(virtual.id);
3028
///
3129
/// // Second endpoint
3230
/// multiChannel.stream.listen((message) {
33-
/// var virtual = multiChannel.virtualChannel(message["channel"]);
31+
/// var virtual = multiChannel.virtualChannel(message as int);
3432
/// // ...
3533
/// });
3634
/// ```
@@ -59,7 +57,10 @@ abstract class MultiChannel<T> implements StreamChannel<T> {
5957
/// Creates a new [MultiChannel] that sends and receives messages over
6058
/// [inner].
6159
///
62-
/// The inner channel must take JSON-like objects.
60+
/// The [inner] channel must be capable of transmitting `List`
61+
/// envelopes. Even if you intend to use a strictly typed [MultiChannel]
62+
/// (for example `MultiChannel<String>`), the [inner] channel MUST be typed to
63+
/// accept `List` or a supertype, otherwise a runtime `TypeError` will occur.
6364
factory MultiChannel(StreamChannel<dynamic> inner) => _MultiChannel<T>(inner);
6465

6566
/// Creates a new virtual channel.

0 commit comments

Comments
 (0)