[web] Hide JS types from dart:ui_web (flutter/engine#42252)

In order to use the exported `PlatformLocation` cross-platform, we need to remove any JS types from the interface. Namely, `DomEventListener`.

Required by https://github.com/flutter/flutter/pull/126851
This commit is contained in:
Mouad Debbar
2023-05-24 16:26:23 -04:00
committed by GitHub
parent ce619437f2
commit 04afaaf25e
4 changed files with 67 additions and 23 deletions

View File

@@ -2119,7 +2119,7 @@ extension DomHistoryExtension on DomHistory {
external JSVoid _go1();
@JS('go')
external JSVoid _go2(JSNumber delta);
void go([double? delta]) {
void go([int? delta]) {
if (delta == null) {
_go1();
} else {

View File

@@ -2,10 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:meta/meta.dart';
import 'package:ui/src/engine.dart';
import 'url_strategy.dart';
/// Function type that handles pop state events.
typedef EventListener = dynamic Function(Object event);
/// Encapsulates all calls to DOM apis, which allows the [UrlStrategy] classes
/// to be platform agnostic and testable.
///
@@ -15,13 +19,13 @@ abstract interface class PlatformLocation {
/// Registers an event listener for the `popstate` event.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
void addPopStateListener(DomEventListener fn);
void addPopStateListener(EventListener fn);
/// Unregisters the given listener (added by [addPopStateListener]) from the
/// `popstate` event.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
void removePopStateListener(DomEventListener fn);
void removePopStateListener(EventListener fn);
/// The `pathname` part of the URL in the browser address bar.
///
@@ -64,7 +68,7 @@ abstract interface class PlatformLocation {
/// * `go(3)` moves forward 3 steps in hisotry.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/go
void go(double count);
void go(int count);
/// The base href where the Flutter app is being served.
///
@@ -72,6 +76,9 @@ abstract interface class PlatformLocation {
String? getBaseHref();
}
final Map<EventListener, DomEventListener> _popStateListenersCache =
<EventListener, DomEventListener>{};
/// Delegates to real browser APIs to provide platform location functionality.
class BrowserPlatformLocation implements PlatformLocation {
/// Default constructor for [BrowserPlatformLocation].
@@ -80,14 +87,24 @@ class BrowserPlatformLocation implements PlatformLocation {
DomLocation get _location => domWindow.location;
DomHistory get _history => domWindow.history;
@override
void addPopStateListener(DomEventListener fn) {
domWindow.addEventListener('popstate', fn);
@visibleForTesting
DomEventListener getOrCreateDomEventListener(EventListener fn) {
return _popStateListenersCache.putIfAbsent(fn, () => createDomEventListener(fn));
}
@override
void removePopStateListener(DomEventListener fn) {
domWindow.removeEventListener('popstate', fn);
void addPopStateListener(EventListener fn) {
domWindow.addEventListener('popstate', getOrCreateDomEventListener(fn));
}
@override
void removePopStateListener(EventListener fn) {
assert(
_popStateListenersCache.containsKey(fn),
'Removing a listener that was never added or was removed already.',
);
domWindow.removeEventListener('popstate', getOrCreateDomEventListener(fn));
_popStateListenersCache.remove(fn);
}
@override
@@ -113,7 +130,7 @@ class BrowserPlatformLocation implements PlatformLocation {
}
@override
void go(double count) {
void go(int count) {
_history.go(count);
}

View File

@@ -151,10 +151,10 @@ class HashUrlStrategy extends UrlStrategy {
@override
ui.VoidCallback addPopStateListener(PopStateListener fn) {
final DomEventListener wrappedFn = createDomEventListener((DomEvent event) {
void wrappedFn(Object event) {
// `fn` expects `event.state`, not a `DomEvent`.
fn((event as DomPopStateEvent).state);
});
}
_platformLocation.addPopStateListener(wrappedFn);
return () => _platformLocation.removePopStateListener(wrappedFn);
}
@@ -199,7 +199,7 @@ class HashUrlStrategy extends UrlStrategy {
@override
Future<void> go(int count) {
_platformLocation.go(count.toDouble());
_platformLocation.go(count);
return _waitForPopState();
}

View File

@@ -3,20 +3,19 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:js_interop'
show JSExportedDartFunction, JSExportedDartFunctionToFunction;
import 'package:quiver/testing/async.dart';
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart' show window;
import 'package:ui/src/engine/dom.dart'
show DomEvent, DomEventListener, createDomPopStateEvent;
show DomEvent, createDomPopStateEvent;
import 'package:ui/src/engine/navigation.dart';
import 'package:ui/src/engine/services.dart';
import 'package:ui/src/engine/test_embedding.dart';
import 'package:ui/ui_web/src/ui_web.dart';
import '../common/matchers.dart';
import '../common/spy.dart';
Map<String, dynamic> _wrapOriginState(dynamic state) {
@@ -690,6 +689,35 @@ void testMain() {
expect(state, expected);
});
});
group('$BrowserPlatformLocation', () {
test('getOrCreateDomEventListener caches funcions', () {
const BrowserPlatformLocation location = BrowserPlatformLocation();
void myListener(Object event) {}
expect(
identical(
location.getOrCreateDomEventListener(myListener),
location.getOrCreateDomEventListener(myListener),
),
isTrue,
);
});
test('throws if removing an invalid listener', () {
const BrowserPlatformLocation location = BrowserPlatformLocation();
void myAddedListener(Object event) {}
void myNonAddedListener(Object event) {}
location.addPopStateListener(myAddedListener);
expect(() => location.removePopStateListener(myAddedListener), returnsNormally);
// Removing the same listener twice should throw.
expect(() => location.removePopStateListener(myAddedListener), throwsAssertionError);
// A listener that was never added.
expect(() => location.removePopStateListener(myNonAddedListener), throwsAssertionError);
});
});
}
Future<void> routeUpdated(String routeName) {
@@ -736,7 +764,7 @@ class TestPlatformLocation implements PlatformLocation {
@override
dynamic state;
List<DomEventListener> popStateListeners = <DomEventListener>[];
List<EventListener> popStateListeners = <EventListener>[];
@override
String pathname = '';
@@ -753,19 +781,18 @@ class TestPlatformLocation implements PlatformLocation {
if (state != null) 'state': state,
},
);
for (final DomEventListener listener in popStateListeners) {
final Function fn = (listener as JSExportedDartFunction).toDart;
fn(event);
for (final EventListener listener in popStateListeners) {
listener(event);
}
}
@override
void addPopStateListener(DomEventListener fn) {
void addPopStateListener(EventListener fn) {
popStateListeners.add(fn);
}
@override
void removePopStateListener(DomEventListener fn) {
void removePopStateListener(EventListener fn) {
throw UnimplementedError();
}
@@ -780,7 +807,7 @@ class TestPlatformLocation implements PlatformLocation {
}
@override
void go(double count) {
void go(int count) {
throw UnimplementedError();
}