Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Unified Diff: lib/src/stream_channel_controller.dart

Issue 1662773003: Add StreamChannel.withGuarantees and StreamChannelController. (Closed) Base URL: git@github.com:dart-lang/stream_channel.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698