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