Chromium Code Reviews| Index: lib/src/resource.dart |
| diff --git a/lib/src/resource.dart b/lib/src/resource.dart |
| index 924ec051b364e38c6fa90bd3867704bb2100f69f..24cbd513387ac29efcb98be574094514e73f1b6d 100644 |
| --- a/lib/src/resource.dart |
| +++ b/lib/src/resource.dart |
| @@ -2,10 +2,35 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| +/// A [Resource] is data that can be read into a Dart program. |
| +/// |
| +/// A resource is identified by a URI. It can be loaded as bytes or data. |
| +/// The resource URI may be a `package:` URI. |
| +/// |
| +/// Example: |
| +/// |
| +/// var resource = new Resource("package:foo/foo_data.txt"); |
| +/// var string = await resource.readAsString(UTF8); |
| +/// print(string); |
| +/// |
| +/// Example: |
| +/// |
| +/// var resource = new Resource("http://example.com/data.json"); |
| +/// var obj = await resource.openRead() // Reads as stream of bytes. |
| +/// .transform(UTF8.fuse(JSON).decoder) |
| +/// .first; |
| +/// |
| +/// |
| +/// Notice: Currently this package requires `dart:io` to do the reading, |
| +/// so it doesn't work in the browser. |
|
floitsch
2016/01/21 12:53:57
Mention the browser version.
Lasse Reichstein Nielsen
2016/01/25 10:48:37
Done.
|
| +library resource; |
| + |
| import "dart:async" show Future, Stream; |
| -import "dart:convert" show Encoding; |
| -import "dart:isolate" show Isolate; |
| -import "loader.dart"; |
| + |
| +import "loader.dart" show ResourceLoader; |
| +// TODO(lrn): Merge with implementation when configured imports removes |
| +// the need to share code. |
| +import "resource_impl.dart" as impl show Resource; |
| /// A resource that can be read into the program. |
| /// |
| @@ -13,7 +38,7 @@ import "loader.dart"; |
| /// the program at runtime. |
| /// The URI may use the `package` scheme to read resources provided |
| /// along with package sources. |
| -abstract class Resource { |
| +class Resource extends impl.Resource { |
| /// Creates a resource object with the given [uri] as location. |
| /// |
| /// The [uri] must be either a [Uri] or a string containing a valid URI. |
| @@ -33,7 +58,8 @@ abstract class Resource { |
| /// If [loader] is omitted, a default implementation is used which supports |
| /// as many of `http`, `https`, `file` and `data` as are available on the |
| /// current platform. |
| - const factory Resource(uri, {ResourceLoader loader}) = _Resource; |
| + const Resource(uri, {ResourceLoader loader}) |
| + : super(uri, (loader != null) ? loader : const DefaultLoader()); |
| /// The location URI of this resource. |
| /// |
| @@ -54,34 +80,3 @@ abstract class Resource { |
| /// If no other encoding is provided, it defaults to UTF-8. |
| Future<String> readAsString({Encoding encoding}); |
| } |
| - |
| -class _Resource implements Resource { |
| - /// Loading strategy for the resource. |
| - final ResourceLoader _loader; |
| - |
| - /// The URI of the resource. |
| - final _uri; |
| - |
| - const _Resource(uri, {ResourceLoader loader}) |
| - : _uri = uri, _loader = (loader != null) ? loader : const DefaultLoader(); |
| - // TODO: Make this `loader ?? const DefaultLoader()` when ?? is const. |
| - |
| - Uri get uri => (_uri is String) ? Uri.parse(_uri) : (_uri as Uri); |
| - |
| - Stream<List<int>> openRead() async* { |
| - Uri uri = await _resolvedUri; |
| - yield* _loader.openRead(uri); |
| - } |
| - |
| - Future<List<int>> readAsBytes() async { |
| - Uri uri = await _resolvedUri; |
| - return _loader.readAsBytes(uri); |
| - } |
| - |
| - Future<String> readAsString({Encoding encoding}) async { |
| - Uri uri = await _resolvedUri; |
| - return _loader.readAsString(uri, encoding: encoding); |
| - } |
| - |
| - Future<Uri> get _resolvedUri => resolveUri(uri); |
| -} |