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

Unified Diff: sdk/lib/_internal/js_runtime/lib/core_patch.dart

Issue 1308143002: dart2js: fetch http Resources (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 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
« no previous file with comments | « pkg/compiler/lib/src/core_types.dart ('k') | sdk/lib/_internal/js_runtime/lib/js_helper.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/js_runtime/lib/core_patch.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/core_patch.dart b/sdk/lib/_internal/js_runtime/lib/core_patch.dart
index 971b37f96e36270c2b6726a7f0ba5f2b83033563..b7842f190344d6fa16cda4130b5e830e2e3e1b80 100644
--- a/sdk/lib/_internal/js_runtime/lib/core_patch.dart
+++ b/sdk/lib/_internal/js_runtime/lib/core_patch.dart
@@ -17,10 +17,13 @@ import 'dart:_js_helper' show patch,
ConstantMap,
stringJoinUnchecked,
objectHashCode,
- Closure;
+ Closure,
+ readHttp;
import 'dart:_native_typed_data' show NativeUint8List;
+import 'dart:async' show StreamController;
+
String _symbolToString(Symbol symbol) => _symbol_dev.Symbol.getName(symbol);
_symbolMapToStringMap(Map<Symbol, dynamic> map) {
@@ -570,20 +573,53 @@ class _Resource implements Resource {
uri = _resolvePackageUri(uri);
}
if (uri.scheme == "http" || uri.scheme == "https") {
- return _readAsString(uri);
+ return _readAsString(uri, encoding);
}
throw new StateError("Unable to find resource, unknown scheme: $_location");
}
+ // TODO(het): Use a streaming XHR request instead of returning the entire
+ // payload in one event.
Stream<List<int>> _readAsStream(Uri uri) {
- throw new UnimplementedError("Streaming bytes via HTTP");
+ var controller = new StreamController.broadcast();
+ // We only need to implement the listener as there is no way to provide
+ // back pressure into the channel.
+ controller.onListen = () {
+ // Once there is a listener, we kick off the loading of the resource.
+ _readAsBytes(uri).then((value) {
+ // The resource loading implementation sends all of the data in a
+ // single message. So the stream will only get a single value posted.
+ controller.add(value);
+ controller.close();
+ },
+ onError: (e, s) {
+ // In case the future terminates with an error we propagate it to the
+ // stream.
+ controller.addError(e, s);
+ controller.close();
+ });
+ };
+
+ return controller.stream;
}
Future<List<int>> _readAsBytes(Uri uri) {
- throw new UnimplementedError("Reading bytes via HTTP");
+ return readHttp('$uri').then((data) {
+ if (data is NativeUint8List) return data;
+ if (data is String) return data.codeUnits;
+ throw new StateError(
+ "Unable to read Resource, data could not be decoded");
+ });
}
- Future<String> _readAsString(Uri uri) {
- throw new UnimplementedError("Reading string via HTTP");
+ Future<String> _readAsString(Uri uri, Encoding encoding) {
+ return readHttp('$uri').then((data) {
+ if (data is String) return data;
+ if (data is NativeUint8List) {
+ return encoding.decode(data);
+ };
+ throw new StateError(
+ "Unable to read Resource, data could not be decoded");
+ });
}
}
« no previous file with comments | « pkg/compiler/lib/src/core_types.dart ('k') | sdk/lib/_internal/js_runtime/lib/js_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698