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.aggregate_transform; | 5 library barback.transformer.aggregate_transform; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
| 10 import 'package:async/async.dart'; |
| 11 |
10 import '../asset/asset.dart'; | 12 import '../asset/asset.dart'; |
11 import '../asset/asset_id.dart'; | 13 import '../asset/asset_id.dart'; |
12 import '../asset/asset_set.dart'; | 14 import '../asset/asset_set.dart'; |
13 import '../errors.dart'; | 15 import '../errors.dart'; |
14 import '../graph/transform_node.dart'; | 16 import '../graph/transform_node.dart'; |
15 import '../utils.dart'; | 17 import '../utils.dart'; |
16 import 'base_transform.dart'; | 18 import 'base_transform.dart'; |
17 | 19 |
18 /// A transform for [AggregateTransformer]s that provides access to all of their | 20 /// A transform for [AggregateTransformer]s that provides access to all of their |
19 /// primary inputs. | 21 /// primary inputs. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 57 |
56 AggregateTransform._(TransformNode node) | 58 AggregateTransform._(TransformNode node) |
57 : _node = node, | 59 : _node = node, |
58 super(node); | 60 super(node); |
59 | 61 |
60 /// Gets the asset for an input [id]. | 62 /// Gets the asset for an input [id]. |
61 /// | 63 /// |
62 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 64 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
63 Future<Asset> getInput(AssetId id) { | 65 Future<Asset> getInput(AssetId id) { |
64 if (_emittedPrimaryInputs.containsId(id)) { | 66 if (_emittedPrimaryInputs.containsId(id)) { |
65 return syncFuture(() => _emittedPrimaryInputs[id]); | 67 return new Future.sync(() => _emittedPrimaryInputs[id]); |
66 } else { | 68 } else { |
67 return _node.getInput(id); | 69 return _node.getInput(id); |
68 } | 70 } |
69 } | 71 } |
70 | 72 |
71 /// A convenience method to the contents of the input with [id] as a string. | 73 /// A convenience method to the contents of the input with [id] as a string. |
72 /// | 74 /// |
73 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. | 75 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
74 /// | 76 /// |
75 /// If the asset was created from a [String] the original string is always | 77 /// If the asset was created from a [String] the original string is always |
76 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 78 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
77 /// asset is decoded using [encoding], which defaults to [UTF8]. | 79 /// asset is decoded using [encoding], which defaults to [UTF8]. |
78 /// | 80 /// |
79 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 81 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
80 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 82 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
81 if (encoding == null) encoding = UTF8; | 83 if (encoding == null) encoding = UTF8; |
82 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | 84 return getInput(id).then/*<Future<String>>*/( |
| 85 (input) => input.readAsString(encoding: encoding)); |
83 } | 86 } |
84 | 87 |
85 /// A convenience method to the contents of the input with [id]. | 88 /// A convenience method to the contents of the input with [id]. |
86 /// | 89 /// |
87 /// This is equivalent to calling [getInput] followed by [Asset.read]. | 90 /// This is equivalent to calling [getInput] followed by [Asset.read]. |
88 /// | 91 /// |
89 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 92 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
90 /// | 93 /// |
91 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 94 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
92 Stream<List<int>> readInput(AssetId id) => | 95 Stream<List<int>> readInput(AssetId id) => |
93 futureStream(getInput(id).then((input) => input.read())); | 96 futureStream(getInput(id).then((input) => input.read())); |
94 | 97 |
95 /// A convenience method to return whether or not an asset exists. | 98 /// A convenience method to return whether or not an asset exists. |
96 /// | 99 /// |
97 /// This is equivalent to calling [getInput] and catching an | 100 /// This is equivalent to calling [getInput] and catching an |
98 /// [AssetNotFoundException]. | 101 /// [AssetNotFoundException]. |
99 Future<bool> hasInput(AssetId id) { | 102 Future<bool> hasInput(AssetId id) { |
100 return getInput(id).then((_) => true).catchError((error) { | 103 return DelegatingFuture.typed( |
| 104 getInput(id).then((_) => true).catchError((error) { |
101 if (error is AssetNotFoundException && error.id == id) return false; | 105 if (error is AssetNotFoundException && error.id == id) return false; |
102 throw error; | 106 throw error; |
103 }); | 107 })); |
104 } | 108 } |
105 | 109 |
106 /// Stores [output] as an output created by this transformation. | 110 /// Stores [output] as an output created by this transformation. |
107 /// | 111 /// |
108 /// A transformation can output as many assets as it wants. | 112 /// A transformation can output as many assets as it wants. |
109 void addOutput(Asset output) { | 113 void addOutput(Asset output) { |
110 // TODO(rnystrom): This should immediately throw if an output with that ID | 114 // TODO(rnystrom): This should immediately throw if an output with that ID |
111 // has already been created by this transformer. | 115 // has already been created by this transformer. |
112 _outputs.add(output); | 116 _outputs.add(output); |
113 } | 117 } |
114 | 118 |
115 void consumePrimary(AssetId id) { | 119 void consumePrimary(AssetId id) { |
116 if (!_emittedPrimaryInputs.containsId(id)) { | 120 if (!_emittedPrimaryInputs.containsId(id)) { |
117 throw new StateError( | 121 throw new StateError( |
118 "$id can't be consumed because it's not a primary input."); | 122 "$id can't be consumed because it's not a primary input."); |
119 } | 123 } |
120 | 124 |
121 super.consumePrimary(id); | 125 super.consumePrimary(id); |
122 } | 126 } |
123 } | 127 } |
124 | 128 |
125 /// The controller for [AggregateTransform]. | 129 /// The controller for [AggregateTransform]. |
126 class AggregateTransformController extends BaseTransformController { | 130 class AggregateTransformController extends BaseTransformController { |
127 AggregateTransform get transform => super.transform; | 131 final AggregateTransform transform; |
128 | 132 |
129 /// The set of assets that the transformer has emitted. | 133 /// The set of assets that the transformer has emitted. |
130 AssetSet get outputs => transform._outputs; | 134 AssetSet get outputs => transform._outputs; |
131 | 135 |
132 bool get isDone => transform._inputController.isClosed; | 136 bool get isDone => transform._inputController.isClosed; |
133 | 137 |
134 AggregateTransformController(TransformNode node) | 138 AggregateTransformController(TransformNode node) |
135 : super(new AggregateTransform._(node)); | 139 : transform = new AggregateTransform._(node); |
136 | 140 |
137 /// Adds a primary input asset to the [AggregateTransform.primaryInputs] | 141 /// Adds a primary input asset to the [AggregateTransform.primaryInputs] |
138 /// stream. | 142 /// stream. |
139 void addInput(Asset input) { | 143 void addInput(Asset input) { |
140 transform._emittedPrimaryInputs.add(input); | 144 transform._emittedPrimaryInputs.add(input); |
141 transform._inputController.add(input); | 145 transform._inputController.add(input); |
142 } | 146 } |
143 | 147 |
144 /// Returns whether an input with the given [id] was added via [addInput]. | 148 /// Returns whether an input with the given [id] was added via [addInput]. |
145 bool addedId(AssetId id) => | 149 bool addedId(AssetId id) => |
146 transform._emittedPrimaryInputs.containsId(id); | 150 transform._emittedPrimaryInputs.containsId(id); |
147 | 151 |
148 void done() { | 152 void done() { |
149 transform._inputController.close(); | 153 transform._inputController.close(); |
150 } | 154 } |
151 } | 155 } |
OLD | NEW |