Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/web_ui/lib/src/engine/app_bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 4 additions & 6 deletions lib/web_ui/lib/src/engine/js_interop/js_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
38 changes: 38 additions & 0 deletions lib/web_ui/test/engine/app_bootstrap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
});
});
}