[web] make addView/removeView functions sync (flutter/engine#51103)

Related to https://github.com/flutter/flutter/issues/138930
This commit is contained in:
Pavel Mazhnik
2024-03-13 01:29:27 +03:00
committed by GitHub
parent 514a3895ce
commit a9c40d24b7
3 changed files with 44 additions and 8 deletions

View File

@@ -68,11 +68,11 @@ class AppBootstrap {
/// Represents the App that was just started, and its JS API.
FlutterApp _prepareFlutterApp() {
return FlutterApp(
addView: (JsFlutterViewOptions options) async {
addView: (JsFlutterViewOptions options) {
assert(configuration.multiViewEnabled, 'Cannot addView when multiView is not enabled');
return viewManager.createAndRegisterView(options).viewId;
},
removeView: (int viewId) async {
removeView: (int viewId) {
assert(configuration.multiViewEnabled, 'Cannot removeView when multiView is not enabled');
return viewManager.disposeAndUnregisterView(viewId);
}

View File

@@ -67,10 +67,8 @@ abstract class FlutterApp {
required RemoveFlutterViewFn removeView,
}) =>
FlutterApp._(
addView: ((JsFlutterViewOptions options) =>
futureToPromise(addView(options) as Future<JSAny>)).toJS,
removeView: ((int id) =>
futureToPromise(removeView(id) as Future<JSObject?>)).toJS,
addView: addView.toJS,
removeView: ((JSNumber id) => removeView(id.toDartInt)).toJS,
);
external factory FlutterApp._({
required JSFunction addView,
@@ -81,9 +79,9 @@ abstract class FlutterApp {
/// Typedef for the function that adds a new view to the app.
///
/// Returns the ID of the newly created view.
typedef AddFlutterViewFn = Future<int> Function(JsFlutterViewOptions);
typedef AddFlutterViewFn = int Function(JsFlutterViewOptions);
/// Typedef for the function that removes a view from the app.
///
/// Returns the configuration used to create the view.
typedef RemoveFlutterViewFn = Future<JsFlutterViewOptions?> Function(int);
typedef RemoveFlutterViewFn = JsFlutterViewOptions? Function(int);

View File

@@ -28,6 +28,8 @@ void testMain() {
});
Future<void> mockInit ([JsFlutterConfiguration? configuration]) async {
debugOverrideJsConfiguration(configuration);
addTearDown(() => debugOverrideJsConfiguration(null));
initCalled = callOrder++;
await Future<void>.delayed(const Duration(milliseconds: 1));
}
@@ -128,5 +130,41 @@ void testMain() {
expect(getJsProperty<dynamic>(maybeApp, 'addView'), isA<Function>());
expect(getJsProperty<dynamic>(maybeApp, 'removeView'), isA<Function>());
});
test('addView/removeView respectively adds/removes view', () async {
final AppBootstrap bootstrap = AppBootstrap(
initializeEngine: mockInit,
runApp: mockRunApp,
);
final FlutterEngineInitializer engineInitializer = bootstrap.prepareEngineInitializer();
final Object appInitializer = await promiseToFuture<Object>(callMethod<Object>(
engineInitializer,
'initializeEngine',
<dynamic>[jsify(<String, Object?>{
'multiViewEnabled': true,
})]
));
final Object maybeApp = await promiseToFuture<Object>(callMethod<Object>(
appInitializer,
'runApp',
<Object?>[]
));
final int viewId = callMethod<num>(
maybeApp,
'addView',
<dynamic>[jsify(<String, Object?>{
'hostElement': createDomElement('div'),
})]
).toInt();
expect(bootstrap.viewManager[viewId], isNotNull);
callMethod<Object>(
maybeApp,
'removeView',
<Object>[viewId]
);
expect(bootstrap.viewManager[viewId], isNull);
});
});
}