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:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
9 | 10 |
10 import '../utils.dart'; | |
11 | |
12 /// 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 |
13 /// methods. | 12 /// methods. |
14 abstract class GetInputTransform { | 13 abstract class GetInputTransform { |
15 Future<Asset> getInput(AssetId id); | 14 Future<Asset> getInput(AssetId id); |
16 | 15 |
17 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 16 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
18 if (encoding == null) encoding = UTF8; | 17 if (encoding == null) encoding = UTF8; |
19 return getInput(id).then((input) => | 18 return getInput(id).then((input) => |
20 input.readAsString(encoding: encoding)); | 19 input.readAsString(encoding: encoding)); |
21 } | 20 } |
22 | 21 |
23 Stream<List<int>> readInput(AssetId id) => | 22 Stream<List<int>> readInput(AssetId id) => |
24 futureStream(getInput(id).then((input) => input.read())); | 23 StreamCompleter.fromFuture(getInput(id).then((input) => input.read())); |
25 | 24 |
26 Future<bool> hasInput(AssetId id) { | 25 Future<bool> hasInput(AssetId id) { |
27 return getInput(id).then((_) => true).catchError((error) { | 26 return getInput(id).then((_) => true).catchError((error) { |
28 if (error is AssetNotFoundException && error.id == id) return false; | 27 if (error is AssetNotFoundException && error.id == id) return false; |
29 throw error; | 28 throw error; |
30 }); | 29 }); |
31 } | 30 } |
32 } | 31 } |
OLD | NEW |