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