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