Unverified Commit 191465ac authored by joshualitt's avatar joshualitt Committed by GitHub

[web] Migrate the bulk of JS interop to JS types. (#123286)

parent 1a98273a
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:js_interop';
import 'package:js/js.dart'; import 'package:js/js.dart';
// This value is set by the engine. It is used to determine if the application is // This value is set by the engine. It is used to determine if the application is
// using canvaskit. // using canvaskit.
@JS('window.flutterCanvasKit') @JS('window.flutterCanvasKit')
external Object? get _windowFlutterCanvasKit; external JSAny? get _windowFlutterCanvasKit;
/// The web implementation of [isCanvasKit] /// The web implementation of [isCanvasKit]
bool get isCanvasKit => _windowFlutterCanvasKit != null; bool get isCanvasKit => _windowFlutterCanvasKit != null;
...@@ -3,11 +3,10 @@ ...@@ -3,11 +3,10 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:typed_data'; import 'dart:js_interop';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:js/js.dart';
import '../services/dom.dart'; import '../services/dom.dart';
import 'image_provider.dart' as image_provider; import 'image_provider.dart' as image_provider;
...@@ -18,7 +17,7 @@ typedef HttpRequestFactory = DomXMLHttpRequest Function(); ...@@ -18,7 +17,7 @@ typedef HttpRequestFactory = DomXMLHttpRequest Function();
/// Default HTTP client. /// Default HTTP client.
DomXMLHttpRequest _httpClient() { DomXMLHttpRequest _httpClient() {
return createDomXMLHttpRequest(); return DomXMLHttpRequest();
} }
/// Creates an overridable factory function. /// Creates an overridable factory function.
...@@ -148,7 +147,7 @@ class NetworkImage ...@@ -148,7 +147,7 @@ class NetworkImage
}); });
} }
request.addEventListener('load', allowInterop((DomEvent e) { request.addEventListener('load', createDomEventListener((DomEvent e) {
final int? status = request.status; final int? status = request.status;
final bool accepted = status! >= 200 && status < 300; final bool accepted = status! >= 200 && status < 300;
final bool fileUri = status == 0; // file:// URIs have status of 0. final bool fileUri = status == 0; // file:// URIs have status of 0.
...@@ -166,13 +165,14 @@ class NetworkImage ...@@ -166,13 +165,14 @@ class NetworkImage
} }
})); }));
request.addEventListener('error', allowInterop(completer.completeError)); request.addEventListener('error',
createDomEventListener(completer.completeError));
request.send(); request.send();
await completer.future; await completer.future;
final Uint8List bytes = (request.response as ByteBuffer).asUint8List(); final Uint8List bytes = (request.response! as JSArrayBuffer).toDart.asUint8List();
if (bytes.lengthInBytes == 0) { if (bytes.lengthInBytes == 0) {
throw image_provider.NetworkImageLoadException( throw image_provider.NetworkImageLoadException(
......
This diff is collapsed.
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:js/js.dart';
import '../services/dom.dart'; import '../services/dom.dart';
import 'basic.dart'; import 'basic.dart';
...@@ -115,7 +114,7 @@ class PlatformSelectableRegionContextMenu extends StatelessWidget { ...@@ -115,7 +114,7 @@ class PlatformSelectableRegionContextMenu extends StatelessWidget {
sheet.insertRule(_kClassRule, 0); sheet.insertRule(_kClassRule, 0);
sheet.insertRule(_kClassSelectionRule, 1); sheet.insertRule(_kClassSelectionRule, 1);
htmlElement.addEventListener('mousedown', allowInterop((DomEvent event) { htmlElement.addEventListener('mousedown', createDomEventListener((DomEvent event) {
final DomMouseEvent mouseEvent = event as DomMouseEvent; final DomMouseEvent mouseEvent = event as DomMouseEvent;
if (mouseEvent.button != _kRightClickButton) { if (mouseEvent.button != _kRightClickButton) {
return; return;
......
...@@ -2,19 +2,25 @@ ...@@ -2,19 +2,25 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:js_interop';
import 'package:flutter/src/services/dom.dart'; import 'package:flutter/src/services/dom.dart';
import 'package:js/js.dart'; import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util; import 'package:js/js_util.dart' as js_util;
void createGetter<T>(Object mock, String key, T Function() get) { /// Defines a new property on an Object.
@JS('Object.defineProperty')
external JSVoid objectDefineProperty(JSAny o, JSString symbol, JSAny desc);
void createGetter(JSAny mock, String key, JSAny? Function() get) {
objectDefineProperty( objectDefineProperty(
mock, mock,
key, key.toJS,
js_util.jsify( js_util.jsify(
<dynamic, dynamic>{ <String, Object>{
'get': allowInterop(() => get()) 'get': () { return get(); }.toJS
} }
)); ) as JSAny);
} }
@JS() @JS()
...@@ -22,46 +28,47 @@ void createGetter<T>(Object mock, String key, T Function() get) { ...@@ -22,46 +28,47 @@ void createGetter<T>(Object mock, String key, T Function() get) {
@anonymous @anonymous
class DomXMLHttpRequestMock { class DomXMLHttpRequestMock {
external factory DomXMLHttpRequestMock({ external factory DomXMLHttpRequestMock({
void Function(String method, String url, bool async)? open, JSFunction? open,
String responseType = 'invalid', JSString responseType,
int timeout = 10, JSNumber timeout,
bool withCredentials = false, JSBoolean withCredentials,
void Function()? send, JSFunction? send,
void Function(String name, String value)? setRequestHeader, JSFunction? setRequestHeader,
void Function(String type, DomEventListener listener) addEventListener, JSFunction addEventListener,
}); });
} }
class TestHttpRequest { class TestHttpRequest {
TestHttpRequest() { TestHttpRequest() {
_mock = DomXMLHttpRequestMock( _mock = DomXMLHttpRequestMock(
open: allowInterop(open), open: open.toJS,
send: allowInterop(send), send: send.toJS,
setRequestHeader: allowInterop(setRequestHeader), setRequestHeader: setRequestHeader.toJS,
addEventListener: allowInterop(addEventListener), addEventListener: addEventListener.toJS,
); );
createGetter(_mock, 'headers', () => headers); createGetter(_mock, 'headers', () => js_util.jsify(headers) as JSAny);
createGetter(_mock, 'responseHeaders', () => responseHeaders); createGetter(_mock,
createGetter(_mock, 'status', () => status); 'responseHeaders', () => js_util.jsify(responseHeaders) as JSAny);
createGetter(_mock, 'response', () => response); createGetter(_mock, 'status', () => status.toJS);
createGetter(_mock, 'response', () => js_util.jsify(response) as JSAny);
} }
late DomXMLHttpRequestMock _mock; late DomXMLHttpRequestMock _mock;
MockEvent? mockEvent; MockEvent? mockEvent;
Map<String, String> headers = <String, String>{}; Map<String, String> headers = <String, String>{};
int status = -1; int status = -1;
dynamic response; Object? response;
Map<String, String> get responseHeaders => headers; Map<String, String> get responseHeaders => headers;
void open(String method, String url, bool async) {} JSVoid open(JSString method, JSString url, JSBoolean async) {}
void send() {} JSVoid send() {}
void setRequestHeader(String name, String value) { JSVoid setRequestHeader(JSString name, JSString value) {
headers[name] = value; headers[name.toDart] = value.toDart;
} }
void addEventListener(String type, DomEventListener listener) { JSVoid addEventListener(JSString type, DomEventListener listener) {
if (type == mockEvent?.type) { if (type.toDart == mockEvent?.type) {
listener(mockEvent!.event); (listener.toDart as DartDomEventListener)(mockEvent!.event);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment