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'; | 10 import 'package:async/async.dart'; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 /// | 74 /// |
75 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. | 75 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
76 /// | 76 /// |
77 /// 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 |
78 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 78 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
79 /// asset is decoded using [encoding], which defaults to [UTF8]. | 79 /// asset is decoded using [encoding], which defaults to [UTF8]. |
80 /// | 80 /// |
81 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 81 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
82 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 82 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
83 if (encoding == null) encoding = UTF8; | 83 if (encoding == null) encoding = UTF8; |
84 return getInput(id).then/*<Future<String>>*/( | 84 return getInput(id).then<Future<String>>( |
85 (input) => input.readAsString(encoding: encoding)); | 85 (input) => input.readAsString(encoding: encoding)); |
86 } | 86 } |
87 | 87 |
88 /// A convenience method to the contents of the input with [id]. | 88 /// A convenience method to the contents of the input with [id]. |
89 /// | 89 /// |
90 /// This is equivalent to calling [getInput] followed by [Asset.read]. | 90 /// This is equivalent to calling [getInput] followed by [Asset.read]. |
91 /// | 91 /// |
92 /// 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. |
93 /// | 93 /// |
94 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 94 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
95 Stream<List<int>> readInput(AssetId id) => | 95 Stream<List<int>> readInput(AssetId id) => |
96 futureStream(getInput(id).then((input) => input.read())); | 96 futureStream(getInput(id).then((input) => input.read())); |
97 | 97 |
98 /// A convenience method to return whether or not an asset exists. | 98 /// A convenience method to return whether or not an asset exists. |
99 /// | 99 /// |
100 /// This is equivalent to calling [getInput] and catching an | 100 /// This is equivalent to calling [getInput] and catching an |
101 /// [AssetNotFoundException]. | 101 /// [AssetNotFoundException]. |
102 Future<bool> hasInput(AssetId id) { | 102 Future<bool> hasInput(AssetId id) { |
103 return DelegatingFuture.typed( | 103 return DelegatingFuture |
104 getInput(id).then((_) => true).catchError((error) { | 104 .typed(getInput(id).then((_) => true).catchError((error) { |
105 if (error is AssetNotFoundException && error.id == id) return false; | 105 if (error is AssetNotFoundException && error.id == id) return false; |
106 throw error; | 106 throw error; |
107 })); | 107 })); |
108 } | 108 } |
109 | 109 |
110 /// Stores [output] as an output created by this transformation. | 110 /// Stores [output] as an output created by this transformation. |
111 /// | 111 /// |
112 /// A transformation can output as many assets as it wants. | 112 /// A transformation can output as many assets as it wants. |
113 void addOutput(Asset output) { | 113 void addOutput(Asset output) { |
114 // 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 |
(...skipping 24 matching lines...) Expand all Loading... |
139 : transform = new AggregateTransform._(node); | 139 : transform = new AggregateTransform._(node); |
140 | 140 |
141 /// Adds a primary input asset to the [AggregateTransform.primaryInputs] | 141 /// Adds a primary input asset to the [AggregateTransform.primaryInputs] |
142 /// stream. | 142 /// stream. |
143 void addInput(Asset input) { | 143 void addInput(Asset input) { |
144 transform._emittedPrimaryInputs.add(input); | 144 transform._emittedPrimaryInputs.add(input); |
145 transform._inputController.add(input); | 145 transform._inputController.add(input); |
146 } | 146 } |
147 | 147 |
148 /// 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]. |
149 bool addedId(AssetId id) => | 149 bool addedId(AssetId id) => transform._emittedPrimaryInputs.containsId(id); |
150 transform._emittedPrimaryInputs.containsId(id); | |
151 | 150 |
152 void done() { | 151 void done() { |
153 transform._inputController.close(); | 152 transform._inputController.close(); |
154 } | 153 } |
155 } | 154 } |
OLD | NEW |