Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Unified Diff: lib/src/browser/resource.dart

Issue 1612003002: Add browser-compatible version. (Closed) Base URL: https://github.com/dart-lang/resource.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/browser/resource.dart
diff --git a/lib/src/resource.dart b/lib/src/browser/resource.dart
similarity index 63%
copy from lib/src/resource.dart
copy to lib/src/browser/resource.dart
index 924ec051b364e38c6fa90bd3867704bb2100f69f..5d0e737f7b971442dd83a6e013546eebc7d95730 100644
--- a/lib/src/resource.dart
+++ b/lib/src/browser/resource.dart
@@ -1,87 +1,82 @@
-// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
-// 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.
-
-import "dart:async" show Future, Stream;
-import "dart:convert" show Encoding;
-import "dart:isolate" show Isolate;
-import "loader.dart";
-
-/// A resource that can be read into the program.
-///
-/// A resource is data that can be located using a URI and read into
-/// the program at runtime.
-/// The URI may use the `package` scheme to read resources provided
-/// along with package sources.
-abstract class 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.
- /// If the string is not a valid URI, using any of the functions on
- /// the resource object will fail.
- ///
- /// The URI may be relative, in which case it will be resolved
- /// against [Uri.base] before being used.
- ///
- /// The URI may use the `package` scheme, which is always supported.
- /// Other schemes may also be supported where possible.
- ///
- /// If [loader] is provided, it is used to load absolute non-package URIs.
- /// Package: URIs are resolved to a non-package URI before being loaded, so
- /// the loader doesn't have to support package: URIs, nor does it need to
- /// support relative URI references.
- /// 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;
-
- /// The location URI of this resource.
- ///
- /// This is a [Uri] of the `uri` parameter given to the constructor.
- /// If the parameter was a string that did not contain a valid URI,
- /// reading `uri` will fail.
- Uri get uri;
-
- /// Reads the resource content as a stream of bytes.
- Stream<List<int>> openRead();
-
- /// Reads the resource content as a single list of bytes.
- Future<List<int>> readAsBytes();
-
- /// Reads the resource content as a string.
- ///
- /// The content is decoded into a string using an [Encoding].
- /// 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);
-}
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// 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.
floitsch 2016/01/21 12:53:57 "or string"?
Lasse Reichstein Nielsen 2016/01/25 10:48:37 Done.
+/// 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:html` to do the reading,
+/// so it only works in the browser.
+library resource;
+
+import "dart:async" show Future, Stream;
+
+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.
+///
+/// A resource is data that can be located using a URI and read into
+/// the program at runtime.
+/// The URI may use the `package` scheme to read resources provided
+/// along with package sources.
+class Resource extends impl.Resource {
+ /// Creates a resource object with the given [uri] as location.
floitsch 2016/01/21 12:53:57 I would inherit these comments for both Resource-c
Lasse Reichstein Nielsen 2016/01/25 10:48:37 The constructor comment can't be inherited, but I'
+ ///
+ /// The [uri] must be either a [Uri] or a string containing a valid URI.
+ /// If the string is not a valid URI, using any of the functions on
+ /// the resource object will fail.
+ ///
+ /// The URI may be relative, in which case it will be resolved
+ /// against [Uri.base] before being used.
+ ///
+ /// The URI may use the `package` scheme, which is always supported.
+ /// Other schemes may also be supported where possible.
+ ///
+ /// If [loader] is provided, it is used to load absolute non-package URIs.
+ /// Package: URIs are resolved to a non-package URI before being loaded, so
+ /// the loader doesn't have to support package: URIs, nor does it need to
+ /// support relative URI references.
+ /// 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 Resource(uri, {ResourceLoader loader})
+ : super(uri, (loader != null) ? loader : const DefaultLoader());
+
+ /// The location URI of this resource.
+ ///
+ /// This is a [Uri] of the `uri` parameter given to the constructor.
+ /// If the parameter was a string that did not contain a valid URI,
+ /// reading `uri` will fail.
+ Uri get uri;
+
+ /// Reads the resource content as a stream of bytes.
+ Stream<List<int>> openRead();
+
+ /// Reads the resource content as a single list of bytes.
+ Future<List<int>> readAsBytes();
+
+ /// Reads the resource content as a string.
+ ///
+ /// The content is decoded into a string using an [Encoding].
+ /// If no other encoding is provided, it defaults to UTF-8.
+ Future<String> readAsString({Encoding encoding});
+}

Powered by Google App Engine
This is Rietveld 408576698