| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import '../stream_channel.dart'; | 7 import '../stream_channel.dart'; |
| 8 | 8 |
| 9 /// A controller for exposing a new [StreamChannel]. | 9 /// A controller for exposing a new [StreamChannel]. |
| 10 /// | 10 /// |
| 11 /// This exposes two connected [StreamChannel]s, [local] and [foreign]. The | 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] | 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 | 13 /// can be returned for others to use. For example, here's a simplified version |
| 14 /// of the implementation of [new IsolateChannel]: | 14 /// of the implementation of [new IsolateChannel]: |
| 15 /// | 15 /// |
| 16 /// ```dart | 16 /// ```dart |
| 17 /// StreamChannel isolateChannel(ReceivePort receivePort, SendPort sendPort) { | 17 /// StreamChannel isolateChannel(ReceivePort receivePort, SendPort sendPort) { |
| 18 /// var controller = new StreamChannelController(); | 18 /// var controller = new StreamChannelController(allowForeignErrors: false); |
| 19 /// | 19 /// |
| 20 /// // Pipe all events from the receive port into the local sink... | 20 /// // Pipe all events from the receive port into the local sink... |
| 21 /// receivePort.pipe(controller.local.sink); | 21 /// receivePort.pipe(controller.local.sink); |
| 22 /// | 22 /// |
| 23 /// // ...and all events from the local stream into the send port. | 23 /// // ...and all events from the local stream into the send port. |
| 24 /// controller.local.listen(sendPort.add, onDone: receivePort.close); | 24 /// controller.local.stream.listen(sendPort.send, onDone: receivePort.close); |
| 25 /// | 25 /// |
| 26 /// // Then return the foreign controller for your users to use. | 26 /// // Then return the foreign controller for your users to use. |
| 27 /// return controller.foreign; | 27 /// return controller.foreign; |
| 28 /// } | 28 /// } |
| 29 /// ``` | 29 /// ``` |
| 30 class StreamChannelController<T> { | 30 class StreamChannelController<T> { |
| 31 /// The local channel. | 31 /// The local channel. |
| 32 /// | 32 /// |
| 33 /// This channel should be used directly by the creator of this | 33 /// This channel should be used directly by the creator of this |
| 34 /// [StreamChannelController] to send and receive events. | 34 /// [StreamChannelController] to send and receive events. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 55 StreamChannelController({bool allowForeignErrors: true, bool sync: false}) { | 55 StreamChannelController({bool allowForeignErrors: true, bool sync: false}) { |
| 56 var localToForeignController = new StreamController<T>(sync: sync); | 56 var localToForeignController = new StreamController<T>(sync: sync); |
| 57 var foreignToLocalController = new StreamController<T>(sync: sync); | 57 var foreignToLocalController = new StreamController<T>(sync: sync); |
| 58 _local = new StreamChannel<T>.withGuarantees( | 58 _local = new StreamChannel<T>.withGuarantees( |
| 59 foreignToLocalController.stream, localToForeignController.sink); | 59 foreignToLocalController.stream, localToForeignController.sink); |
| 60 _foreign = new StreamChannel<T>.withGuarantees( | 60 _foreign = new StreamChannel<T>.withGuarantees( |
| 61 localToForeignController.stream, foreignToLocalController.sink, | 61 localToForeignController.stream, foreignToLocalController.sink, |
| 62 allowSinkErrors: allowForeignErrors); | 62 allowSinkErrors: allowForeignErrors); |
| 63 } | 63 } |
| 64 } | 64 } |
| OLD | NEW |