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.transformer.base_transform; | 5 library barback.base_transform; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import '../asset/asset_id.dart'; | 9 import 'log.dart'; |
10 import '../graph/transform_node.dart'; | |
11 import '../log.dart'; | |
12 import 'transform_logger.dart'; | 10 import 'transform_logger.dart'; |
| 11 import 'transform_node.dart'; |
13 | 12 |
14 /// The base class for the ephemeral transform objects that are passed to | 13 /// The base class for the ephemeral transform objects that are passed to |
15 /// transformers. | 14 /// transformers. |
16 /// | 15 /// |
17 /// This class provides the transformers with inputs, but its up to the | 16 /// This class provides the transformers with inputs, but its up to the |
18 /// subclasses to provide a means of emitting outputs. | 17 /// subclasses to provide a means of emitting outputs. |
19 abstract class BaseTransform { | 18 abstract class BaseTransform { |
20 final TransformNode _node; | 19 final TransformNode _node; |
21 | 20 |
22 /// The ids of primary inputs that should be consumed. | 21 /// Whether the primary input should be consumed. |
23 /// | 22 /// |
24 /// This is exposed by [BaseTransformController]. | 23 /// This is exposed via [BaseTransformController]. |
25 final _consumedPrimaries = new Set<AssetId>(); | 24 bool _consumePrimary = false; |
26 | 25 |
27 /// Whether the transformer logged an error. | 26 /// Whether the transformer logged an error. |
28 /// | 27 /// |
29 /// This is exposed via [BaseTransformController]. | 28 /// This is exposed via [BaseTransformController]. |
30 bool _loggedError = false; | 29 bool _loggedError = false; |
31 | 30 |
32 /// The controller for the stream of log entries emitted by the transformer. | 31 /// The controller for the stream of log entries emitted by the transformer. |
33 /// | 32 /// |
34 /// This is exposed via [BaseTransformController]. | 33 /// This is exposed via [BaseTransformController]. |
35 /// | 34 /// |
36 /// This is synchronous because error logs can cause the transform to fail, so | 35 /// This is synchronous because error logs can cause the transform to fail, so |
37 /// we need to ensure that their processing isn't delayed until after the | 36 /// we need to ensure that their processing isn't delayed until after the |
38 /// transform or build has finished. | 37 /// transform or build has finished. |
39 final _onLogController = new StreamController<LogEntry>.broadcast(sync: true); | 38 final _onLogController = new StreamController<LogEntry>.broadcast(sync: true); |
40 | 39 |
41 /// A logger so that the [Transformer] can report build details. | 40 /// A logger so that the [Transformer] can report build details. |
42 TransformLogger get logger => _logger; | 41 TransformLogger get logger => _logger; |
43 TransformLogger _logger; | 42 TransformLogger _logger; |
44 | 43 |
45 BaseTransform(this._node) { | 44 BaseTransform(this._node) { |
46 _logger = new TransformLogger((asset, level, message, span) { | 45 _logger = new TransformLogger((asset, level, message, span) { |
47 if (level == LogLevel.ERROR) _loggedError = true; | 46 if (level == LogLevel.ERROR) _loggedError = true; |
48 | 47 |
49 // If the log isn't already associated with an asset, use the primary. | 48 // If the log isn't already associated with an asset, use the primary. |
50 if (asset == null) asset = _node.info.primaryId; | 49 if (asset == null) asset = _node.primary.id; |
51 var entry = new LogEntry(_node.info, asset, level, message, span); | 50 var entry = new LogEntry(_node.info, asset, level, message, span); |
52 _onLogController.add(entry); | 51 _onLogController.add(entry); |
53 }); | 52 }); |
54 } | 53 } |
55 | 54 |
56 /// Consume a primary input so that it doesn't get processed by future | 55 /// Consume the primary input so that it doesn't get processed by future |
57 /// phases or emitted once processing has finished. | 56 /// phases or emitted once processing has finished. |
58 /// | 57 /// |
59 /// Normally each primary input will automatically be forwarded unless the | 58 /// Normally the primary input will automatically be forwarded unless the |
60 /// transformer overwrites it by emitting an input with the same id. This | 59 /// transformer overwrites it by emitting an input with the same id. This |
61 /// allows the transformer to tell barback not to forward a primary input | 60 /// allows the transformer to tell barback not to forward the primary input |
62 /// even if it's not overwritten. | 61 /// even if it's not overwritten. |
63 void consumePrimary(AssetId id) { | 62 void consumePrimary() { |
64 // TODO(nweiz): throw an error if an id is consumed that wasn't listed as a | 63 _consumePrimary = true; |
65 // primary input. | |
66 _consumedPrimaries.add(id); | |
67 } | 64 } |
68 } | 65 } |
69 | 66 |
70 /// The base class for controllers of subclasses of [BaseTransform]. | 67 /// The base class for controllers of subclasses of [BaseTransform]. |
71 /// | 68 /// |
72 /// Controllers are used so that [TransformNode]s can get values from a | 69 /// Controllers are used so that [TransformNode]s can get values from a |
73 /// [BaseTransform] without exposing getters in the public API. | 70 /// [BaseTransform] without exposing getters in the public API. |
74 abstract class BaseTransformController { | 71 abstract class BaseTransformController { |
75 /// The [BaseTransform] controlled by this controller. | 72 /// The [BaseTransform] controlled by this controller. |
76 final BaseTransform transform; | 73 final BaseTransform transform; |
77 | 74 |
78 /// The ids of primary inputs that should be consumed. | 75 /// Whether the primary input should be consumed. |
79 Set<AssetId> get consumedPrimaries => transform._consumedPrimaries; | 76 bool get consumePrimary => transform._consumePrimary; |
80 | 77 |
81 /// Whether the transform logged an error. | 78 /// Whether the transform logged an error. |
82 bool get loggedError => transform._loggedError; | 79 bool get loggedError => transform._loggedError; |
83 | 80 |
84 /// The stream of log entries emitted by the transformer during a run. | 81 /// The stream of log entries emitted by the transformer during a run. |
85 Stream<LogEntry> get onLog => transform._onLogController.stream; | 82 Stream<LogEntry> get onLog => transform._onLogController.stream; |
86 | 83 |
87 /// Whether the transform's input or id stream has been closed. | |
88 /// | |
89 /// See also [done]. | |
90 bool get isDone; | |
91 | |
92 BaseTransformController(this.transform); | 84 BaseTransformController(this.transform); |
93 | 85 |
94 /// Mark this transform as finished emitting new inputs or input ids. | 86 /// Notifies the [BaseTransform] that the transformation has finished being |
95 /// | 87 /// applied. |
96 /// This is distinct from [cancel] in that it *doesn't* indicate that the | |
97 /// transform is finished being used entirely. The transformer may still log | |
98 /// messages and load secondary inputs. This just indicates that all the | |
99 /// primary inputs are accounted for. | |
100 void done(); | |
101 | |
102 /// Mark this transform as canceled. | |
103 /// | 88 /// |
104 /// This will close any streams and release any resources that were allocated | 89 /// This will close any streams and release any resources that were allocated |
105 /// for the duration of the transformation. Unlike [done], this indicates that | 90 /// for the duration of the transformation. |
106 /// the transformation is no longer relevant; either it has returned, or | 91 void close() { |
107 /// something external has preemptively invalidated its results. | |
108 void cancel() { | |
109 done(); | |
110 transform._onLogController.close(); | 92 transform._onLogController.close(); |
111 } | 93 } |
112 } | 94 } |
OLD | NEW |