-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate usage of dart:html
to package:web
to support WASM
#801
Labels
type-enhancement
A request for a change that isn't a bug
Comments
There is one issue with moving from We need to remove There are a couple of options:
Option 1.1 (no timeout)
class HttpRequestDataReader implements LocaleDataReader {
/// The base url from which we read the data.
String url;
HttpRequestDataReader(this.url);
@override
Future<String> read(String locale) {
return HttpRequest.getString('$url$locale.json');
}
}
Option 1.2 (artificial timeout):
class HttpRequestDataReader implements LocaleDataReader {
/// The base url from which we read the data.
String url;
HttpRequestDataReader(this.url);
@override
Future<String> read(String locale) {
return Future.any([
Future.delayed(Duration(seconds: 5)).then((_) {
throw TimeoutException('Timeout while reading $locale');
}),
HttpRequest.getString('$url$locale.json')
]);
}
}
Option 2:
class HttpRequestDataReader implements LocaleDataReader {
/// The base url from which we read the data.
String url;
HttpRequestDataReader(this.url);
@override
Future<String> read(String locale) {
final controller = AbortController();
Future.delayed(Duration(seconds: 5)).then((_) {
controller.abort();
});
return window
.fetch(
'$url$locale.json'.toJS,
RequestInit(
method: 'GET',
signal: controller.signal,
),
)
.toDart
.then((response) {
// Note: file:// URIs have status of 0.
if ((response.status >= 200 && response.status < 300) ||
response.status == 0 ||
response.status == 304) {
return response.text().toDart.then((text) => text.toDart);
} else {
throw Exception('Failed to load locale data for $locale');
}
});
}
}
Option 3
class HttpRequestDataReader implements LocaleDataReader {
/// The base url from which we read the data.
String url;
HttpRequestDataReader(this.url);
@override
Future<String> read(String locale) {
final Client client = Client();
return client.get(Uri.parse('$url$locale.json')).then((response) {
if ((response.statusCode >= 200 && response.statusCode < 300) ||
response.statusCode == 0 ||
response.statusCode == 304) {
return response.body;
} else {
throw Exception('Failed to load $locale');
}
});
}
}
Option 4
// Copyright (c) 2012, 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.
/// This contains a reader that accesses data using the HttpRequest
/// facility, and thus works only in the web browser.
library http_request_data_reader;
import 'dart:async';
import 'dart:js_interop';
// todo: Rework this to use the http package once https://github.com/dart-lang/http/issues/424 is resolved.
import 'package:http/http.dart';
import 'intl_helpers.dart';
class HttpRequestDataReader implements LocaleDataReader {
/// The base url from which we read the data.
String url;
HttpRequestDataReader(this.url);
@override
Future<String> read(String locale) {
final Client client = Client();
return Future.any([
Future.delayed(Duration(seconds: 5)).then((_) {
client.close();
throw TimeoutException('Timeout while reading $locale');
}),
client.get(Uri.parse('$url$locale.json')).then((response) {
if ((response.statusCode >= 200 && response.statusCode < 300) ||
response.statusCode == 0 ||
response.statusCode == 304) {
return response.body;
} else {
throw Exception('Failed to load $locale');
}
}),
]);
}
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To support using this fundamental library in WASM it can't use
dart:html
which only works when compiled to JavaScript not WASM.Enable working in a WASM environment usages of
dart:html
need to be migrated topackage:web
.Usages of
dart:html
:https://github.com/search?q=repo%3Adart-lang%2Fi18n+%22dart%3Ahtml%22+path%3A%2F%5Epkgs%5C%2Fintl%5C%2Flib%5C%2F%2F&type=code
Migration guide:
https://dart.dev/interop/js-interop/package-web
The text was updated successfully, but these errors were encountered: