[web] Migrate Flutter Web DOM usage to JS static interop - 26. (flutter/engine#33354)

This commit is contained in:
joshualitt
2022-06-10 14:30:24 -07:00
committed by GitHub
parent c091f2b3db
commit 03055d09f7
3 changed files with 59 additions and 16 deletions

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
import 'dom.dart';
@@ -58,26 +57,34 @@ class AssetManager {
return Uri.encodeFull((_baseUrl ?? '') + '$assetsDir/$asset');
}
/// Loads an asset using an [html.HttpRequest] and returns data as [ByteData].
/// Loads an asset using an [DomXMLHttpRequest] and returns data as [ByteData].
Future<ByteData> load(String asset) async {
final String url = getAssetUrl(asset);
try {
final html.HttpRequest request =
await html.HttpRequest.request(url, responseType: 'arraybuffer');
final DomXMLHttpRequest request =
await domHttpRequest(url, responseType: 'arraybuffer');
final ByteBuffer response = request.response as ByteBuffer;
return response.asByteData();
} on html.ProgressEvent catch (e) {
final html.EventTarget? target = e.target;
if (target is html.HttpRequest) {
if (target.status == 404 && asset == 'AssetManifest.json') {
} catch (e) {
if (!domInstanceOfString(e, 'ProgressEvent')){
rethrow;
}
final DomProgressEvent p = e as DomProgressEvent;
final DomEventTarget? target = p.target;
if (domInstanceOfString(target,'XMLHttpRequest')) {
final DomXMLHttpRequest request = target! as DomXMLHttpRequest;
if (request.status == 404 && asset == 'AssetManifest.json') {
printWarning('Asset manifest does not exist at `$url` – ignoring.');
return Uint8List.fromList(utf8.encode('{}')).buffer.asByteData();
}
throw AssetManagerException(url, target.status!);
throw AssetManagerException(url, request.status!);
}
printWarning('Caught ProgressEvent with target: $target');
final String? constructorName = target == null ? 'null' :
domGetConstructorName(target);
printWarning('Caught ProgressEvent with unknown target: '
'$constructorName');
rethrow;
}
}

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:typed_data';
import 'package:js/js.dart';
@@ -562,6 +563,33 @@ extension DomXMLHttpRequestExtension on DomXMLHttpRequest {
external void send();
}
Future<DomXMLHttpRequest> domHttpRequest(String url, {String? responseType}) {
final Completer<DomXMLHttpRequest> completer = Completer<DomXMLHttpRequest>();
final DomXMLHttpRequest xhr = createDomXMLHttpRequest();
const String method = 'GET';
xhr.open(method, url, /* async */ true);
if (responseType != null) {
xhr.responseType = responseType;
}
xhr.addEventListener('load', allowInterop((DomEvent e) {
final int status = xhr.status!;
final bool accepted = status >= 200 && status < 300;
final bool fileUri = status == 0;
final bool notModified = status == 304;
final bool unknownRedirect = status > 307 && status < 400;
if (accepted || fileUri || notModified || unknownRedirect) {
completer.complete(xhr);
} else {
completer.completeError(e);
}
}));
xhr.addEventListener('error', allowInterop(completer.completeError));
xhr.send();
return completer.future;
}
@JS()
@staticInterop
class DomResponse {}
@@ -822,6 +850,14 @@ Object? domCallConstructorString(String constructorName, List<Object?> args) {
return js_util.callConstructor(constructor, args);
}
String? domGetConstructorName(Object o) {
final Object? constructor = js_util.getProperty(o, 'constructor');
if (constructor == null) {
return '';
}
return js_util.getProperty(constructor, 'name')?.toString();
}
bool domInstanceOfString(Object? element, String objectType) =>
js_util.instanceof(element, domGetConstructor(objectType)!);

View File

@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:html' as html;
import 'dom.dart';
html.Element? _logElement;
late html.Element _logContainer;
DomElement? _logElement;
late DomElement _logContainer;
List<_LogMessage> _logBuffer = <_LogMessage>[];
class _LogMessage {
@@ -51,7 +51,7 @@ void printOnScreen(Object object) {
}
void _initialize() {
_logElement = html.Element.tag('flt-onscreen-log');
_logElement = createDomElement('flt-onscreen-log');
_logElement!.setAttribute('aria-hidden', 'true');
_logElement!.style
..position = 'fixed'
@@ -66,14 +66,14 @@ void _initialize() {
..overflow = 'hidden'
..zIndex = '1000';
_logContainer = html.Element.tag('flt-log-container');
_logContainer = createDomElement('flt-log-container');
_logContainer.setAttribute('aria-hidden', 'true');
_logContainer.style
..position = 'absolute'
..bottom = '0';
_logElement!.append(_logContainer);
html.document.body!.append(_logElement!);
domDocument.body!.append(_logElement!);
}
/// Dump the current stack to the console using [print] and