Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 | |
| 7 import '../stream_channel.dart'; | |
| 8 | |
| 9 /// A controller for exposing a new [StreamChannel]. | |
| 10 /// | |
| 11 /// This exposes two connected [StreamChannel]s, [local] and [foreign]. The | |
| 12 /// user's code should use [local] to emit and receive events. Then [foreign] | |
| 13 /// can be returned for others to use. For example, here's a simplified version | |
| 14 /// of the implementation of [new IsolateChannel]: | |
| 15 /// | |
| 16 /// ```dart | |
| 17 /// StreamChannel isolateChannel(ReceivePort receivePort, SendPort sendPort) { | |
|
nweiz
2016/02/04 00:03:07
I do actually plan to switch over the IsolateChann
| |
| 18 /// var controller = new StreamChannelController(); | |
| 19 /// | |
| 20 /// // Pipe all events from the receive port into the local sink... | |
| 21 /// receivePort.pipe(controller.local.sink); | |
| 22 /// | |
| 23 /// // ...and all events from the local stream into the send port. | |
| 24 /// controller.local.listen(sendPort.add, onDone: receivePort.close); | |
| 25 /// | |
| 26 /// // Then return the foreign controller for your users to use. | |
| 27 /// return controller.foreign; | |
| 28 /// } | |
| 29 /// ``` | |
| 30 class StreamChannelController<T> { | |
| 31 /// The local channel. | |
| 32 /// | |
| 33 /// This channel should be used directly by the creator of this | |
| 34 /// [StreamChannelController] to send and receive events. | |
| 35 StreamChannel<T> get local => _local; | |
|
nweiz
2016/02/04 00:03:07
I decided to go with local/foreign here, in part b
| |
| 36 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
| |
| 37 | |
| 38 /// The foreign channel. | |
| 39 /// | |
| 40 /// This channel should be returned to external users so they can communicate | |
| 41 /// with [local]. | |
| 42 StreamChannel<T> get foreign => _foreign; | |
| 43 StreamChannel<T> _foreign; | |
| 44 | |
| 45 /// Creates a [StreamChannelController]. | |
| 46 /// | |
| 47 /// If [sync] is true, events added to either channel's sink are synchronously | |
| 48 /// dispatched to the other channel's stream. This should only be done if the | |
| 49 /// source of those events is already asynchronous. | |
| 50 StreamChannelController({bool sync: false}) { | |
| 51 var _localToForeignController = new StreamController<T>(sync: sync); | |
| 52 var _foreignToLocalController = new StreamController<T>(sync: sync); | |
| 53 _local = new StreamChannel<T>.withGuarantees( | |
| 54 _foreignToLocalController.stream, _localToForeignController.sink); | |
| 55 _foreign = new StreamChannel<T>.withGuarantees( | |
| 56 _localToForeignController.stream, _foreignToLocalController.sink); | |
| 57 } | |
| 58 } | |
| OLD | NEW |