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