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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 | 7 |
8 import 'package:async/async.dart'; | 8 import 'package:async/async.dart'; |
9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 | 10 |
11 /// A mixin for transforms that support [getInput] and the associated suite of | 11 /// A mixin for transforms that support [getInput] and the associated suite of |
12 /// methods. | 12 /// methods. |
13 abstract class GetInputTransform { | 13 abstract class GetInputTransform { |
14 Future<Asset> getInput(AssetId id); | 14 Future<Asset> getInput(AssetId id); |
15 | 15 |
16 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 16 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
17 if (encoding == null) encoding = UTF8; | 17 if (encoding == null) encoding = UTF8; |
18 return getInput(id).then((input) => | 18 return getInput(id).then((input) => |
19 input.readAsString(encoding: encoding)); | 19 input.readAsString(encoding: encoding)); |
20 } | 20 } |
21 | 21 |
22 Stream<List<int>> readInput(AssetId id) => | 22 Stream<List<int>> readInput(AssetId id) => |
23 StreamCompleter.fromFuture(getInput(id).then((input) => input.read())); | 23 StreamCompleter.fromFuture(getInput(id).then((input) => input.read())); |
24 | 24 |
25 Future<bool> hasInput(AssetId id) { | 25 Future<bool> hasInput(AssetId id) async { |
26 return getInput(id).then((_) => true).catchError((error) { | 26 try { |
27 if (error is AssetNotFoundException && error.id == id) return false; | 27 await getInput(id); |
28 throw error; | 28 return true; |
29 }); | 29 } on AssetNotFoundException catch (error) { |
| 30 if (error.id == id) return false; |
| 31 rethrow; |
| 32 } |
30 } | 33 } |
31 } | 34 } |
OLD | NEW |