Index: lib/src/stream_channel_controller.dart |
diff --git a/lib/src/stream_channel_controller.dart b/lib/src/stream_channel_controller.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8a69a849935f182018abd3be2903f7dd0f3f833b |
--- /dev/null |
+++ b/lib/src/stream_channel_controller.dart |
@@ -0,0 +1,58 @@ |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import 'dart:async'; |
+ |
+import '../stream_channel.dart'; |
+ |
+/// A controller for exposing a new [StreamChannel]. |
+/// |
+/// This exposes two connected [StreamChannel]s, [local] and [foreign]. The |
+/// user's code should use [local] to emit and receive events. Then [foreign] |
+/// can be returned for others to use. For example, here's a simplified version |
+/// of the implementation of [new IsolateChannel]: |
+/// |
+/// ```dart |
+/// StreamChannel isolateChannel(ReceivePort receivePort, SendPort sendPort) { |
nweiz
2016/02/04 00:03:07
I do actually plan to switch over the IsolateChann
|
+/// var controller = new StreamChannelController(); |
+/// |
+/// // Pipe all events from the receive port into the local sink... |
+/// receivePort.pipe(controller.local.sink); |
+/// |
+/// // ...and all events from the local stream into the send port. |
+/// controller.local.listen(sendPort.add, onDone: receivePort.close); |
+/// |
+/// // Then return the foreign controller for your users to use. |
+/// return controller.foreign; |
+/// } |
+/// ``` |
+class StreamChannelController<T> { |
+ /// The local channel. |
+ /// |
+ /// This channel should be used directly by the creator of this |
+ /// [StreamChannelController] to send and receive events. |
+ StreamChannel<T> get local => _local; |
nweiz
2016/02/04 00:03:07
I decided to go with local/foreign here, in part b
|
+ StreamChannel<T> _local; |
tjblasi
2016/02/04 00:33:47
Consider making these `final` & avoiding the need
nweiz
2016/02/04 01:35:04
I could do it here, but both channels' constructor
|
+ |
+ /// The foreign channel. |
+ /// |
+ /// This channel should be returned to external users so they can communicate |
+ /// with [local]. |
+ StreamChannel<T> get foreign => _foreign; |
+ StreamChannel<T> _foreign; |
+ |
+ /// Creates a [StreamChannelController]. |
+ /// |
+ /// If [sync] is true, events added to either channel's sink are synchronously |
+ /// dispatched to the other channel's stream. This should only be done if the |
+ /// source of those events is already asynchronous. |
+ StreamChannelController({bool sync: false}) { |
+ var _localToForeignController = new StreamController<T>(sync: sync); |
+ var _foreignToLocalController = new StreamController<T>(sync: sync); |
+ _local = new StreamChannel<T>.withGuarantees( |
+ _foreignToLocalController.stream, _localToForeignController.sink); |
+ _foreign = new StreamChannel<T>.withGuarantees( |
+ _localToForeignController.stream, _foreignToLocalController.sink); |
+ } |
+} |