| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library barback.node_streams; | 5 library barback.node_streams; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset_node.dart'; | 9 import 'asset_node.dart'; |
| 10 import 'log.dart'; | 10 import 'log.dart'; |
| 11 import 'node_status.dart'; |
| 11 import 'stream_pool.dart'; | 12 import 'stream_pool.dart'; |
| 12 | 13 |
| 13 /// A collection of streams that are common to nodes in barback's package graph. | 14 /// A collection of streams that are common to nodes in barback's package graph. |
| 14 class NodeStreams { | 15 class NodeStreams { |
| 15 /// A stream that emits an event whenever the node is no longer dirty. | 16 /// A stream that emits an event every time the node's status changes. |
| 16 /// | 17 /// |
| 17 /// This is synchronous in order to guarantee that it will emit an event as | 18 /// This will emit the new status. It's guaranteed to emit an event only when |
| 18 /// soon as [isDirty] flips from `true` to `false`. | 19 /// the status changes from the previous value. To ensure this, callers should |
| 19 Stream get onDone => onDoneController.stream; | 20 /// emit status changes using [changeStatus]. The initial status is assumed to |
| 20 final onDoneController = new StreamController.broadcast(sync: true); | 21 /// be [NodeStatus.RUNNING]. |
| 22 Stream<NodeStatus> get onStatusChange => _onStatusChangeController.stream; |
| 23 final _onStatusChangeController = |
| 24 new StreamController<NodeStatus>.broadcast(sync: true); |
| 21 | 25 |
| 22 /// A stream that emits any new assets produced by the node. | 26 /// A stream that emits any new assets produced by the node. |
| 23 /// | 27 /// |
| 24 /// Assets are emitted synchronously to ensure that any changes are thoroughly | 28 /// Assets are emitted synchronously to ensure that any changes are thoroughly |
| 25 /// propagated as soon as they occur. | 29 /// propagated as soon as they occur. |
| 26 Stream<AssetNode> get onAsset => onAssetPool.stream; | 30 Stream<AssetNode> get onAsset => onAssetPool.stream; |
| 27 final onAssetPool = new StreamPool<AssetNode>.broadcast(); | 31 final onAssetPool = new StreamPool<AssetNode>.broadcast(); |
| 28 final onAssetController = | 32 final onAssetController = |
| 29 new StreamController<AssetNode>.broadcast(sync: true); | 33 new StreamController<AssetNode>.broadcast(sync: true); |
| 30 | 34 |
| 31 /// A stream that emits an event whenever any the node logs an entry. | 35 /// A stream that emits an event whenever any the node logs an entry. |
| 32 Stream<LogEntry> get onLog => onLogPool.stream; | 36 Stream<LogEntry> get onLog => onLogPool.stream; |
| 33 final onLogPool = new StreamPool<LogEntry>.broadcast(); | 37 final onLogPool = new StreamPool<LogEntry>.broadcast(); |
| 34 final onLogController = new StreamController<LogEntry>.broadcast(sync: true); | 38 final onLogController = new StreamController<LogEntry>.broadcast(sync: true); |
| 35 | 39 |
| 40 var _previousStatus = NodeStatus.RUNNING; |
| 41 |
| 36 NodeStreams() { | 42 NodeStreams() { |
| 37 onAssetPool.add(onAssetController.stream); | 43 onAssetPool.add(onAssetController.stream); |
| 38 onLogPool.add(onLogController.stream); | 44 onLogPool.add(onLogController.stream); |
| 39 } | 45 } |
| 40 | 46 |
| 47 /// Emits a status change notification via [onStatusChange]. |
| 48 /// |
| 49 /// This guarantees that a change notification won't be emitted if the status |
| 50 /// didn't actually change. |
| 51 void changeStatus(NodeStatus status) { |
| 52 if (_previousStatus != status) _onStatusChangeController.add(status); |
| 53 } |
| 54 |
| 41 /// Closes all the streams. | 55 /// Closes all the streams. |
| 42 void close() { | 56 void close() { |
| 43 onDoneController.close(); | 57 _onStatusChangeController.close(); |
| 44 onAssetController.close(); | 58 onAssetController.close(); |
| 45 onAssetPool.close(); | 59 onAssetPool.close(); |
| 46 onLogController.close(); | 60 onLogController.close(); |
| 47 onLogPool.close(); | 61 onLogPool.close(); |
| 48 } | 62 } |
| 49 } | 63 } |
| OLD | NEW |