From 469b1eb0eb9306a0787cca4153f3eedecf41402a Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:39:20 +0800 Subject: [PATCH 01/39] refactor: rm unused flag --- frb_dart/lib/src/wasm_module/_web.dart | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 37200bbe54..9f12b651d9 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -21,9 +21,8 @@ abstract class WasmModule { WasmModule bind(dynamic thisArg, String moduleName); /// Initialize a [WasmModule] with the specified kind of [Modules]. - static Future initialize( - {required Modules kind, WasmModule Function()? module}) => - kind.initializeModule(module); + static Future initialize({required Modules kind}) => + kind.initializeModule(); } /// Currently supported modes of module initialization. @@ -44,7 +43,7 @@ abstract class Modules { /// How a WASM module is brought into Dart's scope and initialized. /// /// Override this method to define custom initialization processes. - Future initializeModule(WasmModule Function()? module); + Future initializeModule(); void _ensureCrossOriginIsolated() { switch (crossOriginIsolated) { @@ -66,7 +65,7 @@ class _WasmBindgenNoModules extends Modules { const _WasmBindgenNoModules({required this.root}); @override - Future initializeModule(WasmModule Function()? module) async { + Future initializeModule() async { _ensureCrossOriginIsolated(); final script = ScriptElement()..src = '$root.js'; document.head!.append(script); @@ -75,9 +74,7 @@ class _WasmBindgenNoModules extends Modules { jsEval('window.wasm_bindgen = wasm_bindgen'); - final module_ = module?.call() ?? _noModules!; - - return await promiseToFuture(module_('${root}_bg.wasm')); + return await promiseToFuture(_noModules!('${root}_bg.wasm')); } } From f88b745609576ae5b85f37d26bb64fc7c132f9d3 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:40:16 +0800 Subject: [PATCH 02/39] feat: rm field --- frb_dart/lib/src/platform_types/_web.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frb_dart/lib/src/platform_types/_web.dart b/frb_dart/lib/src/platform_types/_web.dart index 0ef6343861..242e9253b3 100644 --- a/frb_dart/lib/src/platform_types/_web.dart +++ b/frb_dart/lib/src/platform_types/_web.dart @@ -26,10 +26,7 @@ typedef DartPostCObject = void; /// {@macro flutter_rust_bridge.only_for_generated_code} class ExternalLibrary extends BaseExternalLibrary { /// {@macro flutter_rust_bridge.only_for_generated_code} - final Object wasmModule; - - /// {@macro flutter_rust_bridge.only_for_generated_code} - const ExternalLibrary({required this.wasmModule, required super.debugInfo}); + const ExternalLibrary({required super.debugInfo}); } /// {@macro flutter_rust_bridge.internal} From 412d1c651b66cdbd0d2c4003ab611a67701ab7ae Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:40:23 +0800 Subject: [PATCH 03/39] feat: rm return type --- frb_dart/lib/src/loader/_web.dart | 7 ++----- frb_dart/lib/src/wasm_module/_web.dart | 8 ++++---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/frb_dart/lib/src/loader/_web.dart b/frb_dart/lib/src/loader/_web.dart index 5df60a9e81..22303b01e0 100644 --- a/frb_dart/lib/src/loader/_web.dart +++ b/frb_dart/lib/src/loader/_web.dart @@ -14,9 +14,6 @@ FutureOr loadExternalLibrary( /// Please see `loadExternalLibrary` for details Future loadExternalLibraryRaw( {required String moduleRoot}) async { - return ExternalLibrary( - wasmModule: - await WasmModule.initialize(kind: Modules.noModules(root: moduleRoot)), - debugInfo: 'moduleRoot=$moduleRoot', - ); + await WasmModule.initialize(kind: Modules.noModules(root: moduleRoot)); + return ExternalLibrary(debugInfo: 'moduleRoot=$moduleRoot'); } diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 9f12b651d9..e5d315cebe 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -21,7 +21,7 @@ abstract class WasmModule { WasmModule bind(dynamic thisArg, String moduleName); /// Initialize a [WasmModule] with the specified kind of [Modules]. - static Future initialize({required Modules kind}) => + static Future initialize({required Modules kind}) => kind.initializeModule(); } @@ -43,7 +43,7 @@ abstract class Modules { /// How a WASM module is brought into Dart's scope and initialized. /// /// Override this method to define custom initialization processes. - Future initializeModule(); + Future initializeModule(); void _ensureCrossOriginIsolated() { switch (crossOriginIsolated) { @@ -65,7 +65,7 @@ class _WasmBindgenNoModules extends Modules { const _WasmBindgenNoModules({required this.root}); @override - Future initializeModule() async { + Future initializeModule() async { _ensureCrossOriginIsolated(); final script = ScriptElement()..src = '$root.js'; document.head!.append(script); @@ -74,7 +74,7 @@ class _WasmBindgenNoModules extends Modules { jsEval('window.wasm_bindgen = wasm_bindgen'); - return await promiseToFuture(_noModules!('${root}_bg.wasm')); + await promiseToFuture(_noModules!('${root}_bg.wasm')); } } From aea88931cb7be986f06f7092c34aa9bf29176f6c Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:40:40 +0800 Subject: [PATCH 04/39] refactor: mv --- frb_dart/lib/src/wasm_module/_web.dart | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index e5d315cebe..632a3629b8 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -44,19 +44,6 @@ abstract class Modules { /// /// Override this method to define custom initialization processes. Future initializeModule(); - - void _ensureCrossOriginIsolated() { - switch (crossOriginIsolated) { - case false: - throw const MissingHeaderException(); - case true: - return; - case null: - jsConsoleWarn( - 'Warning: crossOriginIsolated is null, browser might not support buffer sharing.'); - return; - } - } } class _WasmBindgenNoModules extends Modules { @@ -80,3 +67,16 @@ class _WasmBindgenNoModules extends Modules { @JS('wasm_bindgen') external WasmModule? get _noModules; + +void _ensureCrossOriginIsolated() { + switch (crossOriginIsolated) { + case false: + throw const MissingHeaderException(); + case true: + return; + case null: + jsConsoleWarn( + 'Warning: crossOriginIsolated is null, browser might not support buffer sharing.'); + return; + } +} From d62b0f0c9a1fcbcdfeb910af3264fd73cb3d2a81 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:41:28 +0800 Subject: [PATCH 05/39] refactor: mv --- frb_dart/lib/src/wasm_module/_web.dart | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 632a3629b8..71c301e259 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -50,19 +50,18 @@ class _WasmBindgenNoModules extends Modules { final String root; const _WasmBindgenNoModules({required this.root}); +} - @override - Future initializeModule() async { - _ensureCrossOriginIsolated(); - final script = ScriptElement()..src = '$root.js'; - document.head!.append(script); +Future initializeModule({required String root}) async { + _ensureCrossOriginIsolated(); + final script = ScriptElement()..src = '$root.js'; + document.head!.append(script); - await script.onLoad.first; + await script.onLoad.first; - jsEval('window.wasm_bindgen = wasm_bindgen'); + jsEval('window.wasm_bindgen = wasm_bindgen'); - await promiseToFuture(_noModules!('${root}_bg.wasm')); - } + await promiseToFuture(_noModules!('${root}_bg.wasm')); } @JS('wasm_bindgen') From 0a3b8827b93a59660a3c70a47c5c8022c2381a4e Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:42:07 +0800 Subject: [PATCH 06/39] Revert "refactor: mv" This reverts commit d62b0f0c9a1fcbcdfeb910af3264fd73cb3d2a81. --- frb_dart/lib/src/wasm_module/_web.dart | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 71c301e259..632a3629b8 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -50,18 +50,19 @@ class _WasmBindgenNoModules extends Modules { final String root; const _WasmBindgenNoModules({required this.root}); -} -Future initializeModule({required String root}) async { - _ensureCrossOriginIsolated(); - final script = ScriptElement()..src = '$root.js'; - document.head!.append(script); + @override + Future initializeModule() async { + _ensureCrossOriginIsolated(); + final script = ScriptElement()..src = '$root.js'; + document.head!.append(script); - await script.onLoad.first; + await script.onLoad.first; - jsEval('window.wasm_bindgen = wasm_bindgen'); + jsEval('window.wasm_bindgen = wasm_bindgen'); - await promiseToFuture(_noModules!('${root}_bg.wasm')); + await promiseToFuture(_noModules!('${root}_bg.wasm')); + } } @JS('wasm_bindgen') From e50c4c9cefd31a0076f13de7ee45b4cb12bab86e Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:42:43 +0800 Subject: [PATCH 07/39] refactor: rm bind --- frb_dart/lib/src/wasm_module/_web.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 632a3629b8..9b8e086a4b 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -17,9 +17,6 @@ abstract class WasmModule { /// {@macro flutter_rust_bridge.only_for_generated_code} Object call([String? moduleName]); - /// Create a new WASM module initializer that is bound to the specified binary. - WasmModule bind(dynamic thisArg, String moduleName); - /// Initialize a [WasmModule] with the specified kind of [Modules]. static Future initialize({required Modules kind}) => kind.initializeModule(); From e885ba02bbfb14ec12ab86693d33a58c0be30345 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:49:51 +0800 Subject: [PATCH 08/39] feat: more --- frb_dart/lib/src/loader/_web.dart | 2 +- frb_dart/lib/src/wasm_module/_web.dart | 62 ++++---------------------- 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/frb_dart/lib/src/loader/_web.dart b/frb_dart/lib/src/loader/_web.dart index 22303b01e0..5d43c71874 100644 --- a/frb_dart/lib/src/loader/_web.dart +++ b/frb_dart/lib/src/loader/_web.dart @@ -14,6 +14,6 @@ FutureOr loadExternalLibrary( /// Please see `loadExternalLibrary` for details Future loadExternalLibraryRaw( {required String moduleRoot}) async { - await WasmModule.initialize(kind: Modules.noModules(root: moduleRoot)); + await initializeWasmModule(root: moduleRoot); return ExternalLibrary(debugInfo: 'moduleRoot=$moduleRoot'); } diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 9b8e086a4b..b37ec65e2f 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -5,65 +5,21 @@ import 'package:flutter_rust_bridge/src/exceptions.dart'; import 'package:flutter_rust_bridge/src/platform_utils/_web.dart'; import 'package:js/js.dart'; -/// A JS function that returns a Promise to a WASM module. -/// See [this file](https://github.com/fzyzcjy/flutter_rust_bridge/blob/ffc9c2f530daa72ebd2f77e45e67b4fa7a65c172/frb_example/pure_dart/dart/lib/ffi.web.dart) -/// for an example of how to obtain and initialize this type. -/// -/// ## Enabling cross-origin isolation -/// Rust WASM modules do not work without cross-origin isolation. -/// Please refer to [Setting up the web server](http://cjycode.com/flutter_rust_bridge/build_wasm.html#setting-up-the-web-server) -/// for an example of a Dart web server that accomplishes this task. -abstract class WasmModule { - /// {@macro flutter_rust_bridge.only_for_generated_code} - Object call([String? moduleName]); +/// {@macro flutter_rust_bridge.internal} +Future initializeWasmModule({required String root}) async { + _ensureCrossOriginIsolated(); + final script = ScriptElement()..src = '$root.js'; + document.head!.append(script); - /// Initialize a [WasmModule] with the specified kind of [Modules]. - static Future initialize({required Modules kind}) => - kind.initializeModule(); -} - -/// Currently supported modes of module initialization. -/// -/// Advanced users may wish to inherit this class and override [initializeModule] -/// to provide their own initialization process. -abstract class Modules { - /// Construct modules - const Modules(); - - /// Initialize a `wasm_bindgen` module built with the `-t no-modules` flag. - /// - /// The expected output is a file named `$root.js` and the accompanying - /// WASM binary named `${root}_bg.wasm`. - const factory Modules.noModules({required String root}) = - _WasmBindgenNoModules; - - /// How a WASM module is brought into Dart's scope and initialized. - /// - /// Override this method to define custom initialization processes. - Future initializeModule(); -} + await script.onLoad.first; -class _WasmBindgenNoModules extends Modules { - final String root; + jsEval('window.wasm_bindgen = wasm_bindgen'); - const _WasmBindgenNoModules({required this.root}); - - @override - Future initializeModule() async { - _ensureCrossOriginIsolated(); - final script = ScriptElement()..src = '$root.js'; - document.head!.append(script); - - await script.onLoad.first; - - jsEval('window.wasm_bindgen = wasm_bindgen'); - - await promiseToFuture(_noModules!('${root}_bg.wasm')); - } + await promiseToFuture(_noModules('${root}_bg.wasm')); } @JS('wasm_bindgen') -external WasmModule? get _noModules; +external dynamic get _noModules; void _ensureCrossOriginIsolated() { switch (crossOriginIsolated) { From 75ede51ac884b0e226437cbcae2d484c3067c2cf Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:51:51 +0800 Subject: [PATCH 09/39] feat: try rm manually --- .../lib/src/rust/frb_generated.web.dart | 14 +++++--------- .../pure_dart/lib/src/rust/frb_generated.web.dart | 8 +------- .../lib/src/rust/frb_generated.web.dart | 8 +------- 3 files changed, 7 insertions(+), 23 deletions(-) diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart index 101ddd1728..2cd4f18dbd 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart @@ -6,12 +6,14 @@ // Static analysis wrongly picks the IO variant, thus ignore this // ignore_for_file: argument_type_not_assignable -import 'api/minimal.dart'; import 'dart:async'; import 'dart:convert'; -import 'frb_generated.dart'; + import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated_web.dart'; +import 'api/minimal.dart'; +import 'frb_generated.dart'; + abstract class RustLibApiImplPlatform extends BaseApiImpl { RustLibApiImplPlatform({ required super.handler, @@ -56,10 +58,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} diff --git a/frb_example/pure_dart/lib/src/rust/frb_generated.web.dart b/frb_example/pure_dart/lib/src/rust/frb_generated.web.dart index 06818ae5e7..8bb5981b4c 100644 --- a/frb_example/pure_dart/lib/src/rust/frb_generated.web.dart +++ b/frb_example/pure_dart/lib/src/rust/frb_generated.web.dart @@ -59606,13 +59606,7 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); - +class RustLibWasmModule { external void wire__crate__api__function_at_api_mod_rs(NativePortType port_); external void wire__crate__api__array__boxed_blob_twin_normal( diff --git a/frb_example/pure_dart_pde/lib/src/rust/frb_generated.web.dart b/frb_example/pure_dart_pde/lib/src/rust/frb_generated.web.dart index b588cf9dd4..3ebea5b47d 100644 --- a/frb_example/pure_dart_pde/lib/src/rust/frb_generated.web.dart +++ b/frb_example/pure_dart_pde/lib/src/rust/frb_generated.web.dart @@ -14332,13 +14332,7 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); - +class RustLibWasmModule { external void rust_arc_increment_strong_count_RustOpaque_BoxdynDartDebugTwinNormal( dynamic ptr); From 65b78b108329e80c7be851473f1f6b84adcd0f22 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:52:51 +0800 Subject: [PATCH 10/39] feat: more --- .../generator/wire/dart/spec_generator/wire_class/web.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/frb_codegen/src/library/codegen/generator/wire/dart/spec_generator/wire_class/web.rs b/frb_codegen/src/library/codegen/generator/wire/dart/spec_generator/wire_class/web.rs index 271cd5ea38..b69b04db56 100644 --- a/frb_codegen/src/library/codegen/generator/wire/dart/spec_generator/wire_class/web.rs +++ b/frb_codegen/src/library/codegen/generator/wire/dart/spec_generator/wire_class/web.rs @@ -56,13 +56,7 @@ fn generate_wasm_module_class( format!( "@JS('wasm_bindgen') external {wasm_module_name} get wasmModule; - @JS() @anonymous class {wasm_module_name} implements WasmModule {{ - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external {wasm_module_name} bind(dynamic thisArg, String moduleName); - + @JS() @anonymous class {wasm_module_name} {{ {body} }} " From 881b8e243b3692c39f78f630425893d807c9eee5 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:55:58 +0800 Subject: [PATCH 11/39] chore: more --- frb_dart/lib/src/wasm_module/_web.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index b37ec65e2f..c3a76b7353 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -8,6 +8,7 @@ import 'package:js/js.dart'; /// {@macro flutter_rust_bridge.internal} Future initializeWasmModule({required String root}) async { _ensureCrossOriginIsolated(); + final script = ScriptElement()..src = '$root.js'; document.head!.append(script); From fdb5900234660451d770cd69ea204f20ce427855 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:56:20 +0800 Subject: [PATCH 12/39] feat: try rm jsEval --- frb_dart/lib/src/platform_utils/_web.dart | 3 --- frb_dart/lib/src/wasm_module/_web.dart | 2 -- 2 files changed, 5 deletions(-) diff --git a/frb_dart/lib/src/platform_utils/_web.dart b/frb_dart/lib/src/platform_utils/_web.dart index dcd480b33e..89b59c254f 100644 --- a/frb_dart/lib/src/platform_utils/_web.dart +++ b/frb_dart/lib/src/platform_utils/_web.dart @@ -16,9 +16,6 @@ class _Function { external factory _Function(String script); } -/// {@macro flutter_rust_bridge.internal} -dynamic jsEval(String script) => _Function(script)(); - /// Whether the web platform has been isolated by COOP and COEP headers, /// and is capable of sharing buffers between workers. /// diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index c3a76b7353..0cfdbb3b22 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -14,8 +14,6 @@ Future initializeWasmModule({required String root}) async { await script.onLoad.first; - jsEval('window.wasm_bindgen = wasm_bindgen'); - await promiseToFuture(_noModules('${root}_bg.wasm')); } From 01a767aa311275909e7746c38d80faf4061e28dc Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:58:32 +0800 Subject: [PATCH 13/39] chore: empty line --- frb_dart/lib/src/wasm_module/_web.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 0cfdbb3b22..04ab0bfde9 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -11,7 +11,6 @@ Future initializeWasmModule({required String root}) async { final script = ScriptElement()..src = '$root.js'; document.head!.append(script); - await script.onLoad.first; await promiseToFuture(_noModules('${root}_bg.wasm')); From e7cb0fb7df0d8814ccfc7cf8b271ed71941ffe8a Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:59:07 +0800 Subject: [PATCH 14/39] Revert "chore: empty line" This reverts commit 01a767aa311275909e7746c38d80faf4061e28dc. --- frb_dart/lib/src/wasm_module/_web.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 04ab0bfde9..0cfdbb3b22 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -11,6 +11,7 @@ Future initializeWasmModule({required String root}) async { final script = ScriptElement()..src = '$root.js'; document.head!.append(script); + await script.onLoad.first; await promiseToFuture(_noModules('${root}_bg.wasm')); From ef7271477a4f190f8930edb5000a2f68bdbed3ad Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 21:59:07 +0800 Subject: [PATCH 15/39] Revert "feat: try rm jsEval" This reverts commit fdb5900234660451d770cd69ea204f20ce427855. --- frb_dart/lib/src/platform_utils/_web.dart | 3 +++ frb_dart/lib/src/wasm_module/_web.dart | 2 ++ 2 files changed, 5 insertions(+) diff --git a/frb_dart/lib/src/platform_utils/_web.dart b/frb_dart/lib/src/platform_utils/_web.dart index 89b59c254f..dcd480b33e 100644 --- a/frb_dart/lib/src/platform_utils/_web.dart +++ b/frb_dart/lib/src/platform_utils/_web.dart @@ -16,6 +16,9 @@ class _Function { external factory _Function(String script); } +/// {@macro flutter_rust_bridge.internal} +dynamic jsEval(String script) => _Function(script)(); + /// Whether the web platform has been isolated by COOP and COEP headers, /// and is capable of sharing buffers between workers. /// diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index 0cfdbb3b22..c3a76b7353 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -14,6 +14,8 @@ Future initializeWasmModule({required String root}) async { await script.onLoad.first; + jsEval('window.wasm_bindgen = wasm_bindgen'); + await promiseToFuture(_noModules('${root}_bg.wasm')); } From f1e149dec18adb2595636a991bce6e93a0b537c7 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:01:15 +0800 Subject: [PATCH 16/39] chore: rename --- frb_dart/lib/src/wasm_module/_web.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frb_dart/lib/src/wasm_module/_web.dart b/frb_dart/lib/src/wasm_module/_web.dart index c3a76b7353..fb9a8dd870 100644 --- a/frb_dart/lib/src/wasm_module/_web.dart +++ b/frb_dart/lib/src/wasm_module/_web.dart @@ -16,11 +16,11 @@ Future initializeWasmModule({required String root}) async { jsEval('window.wasm_bindgen = wasm_bindgen'); - await promiseToFuture(_noModules('${root}_bg.wasm')); + await promiseToFuture(_jsWasmBindgen('${root}_bg.wasm')); } @JS('wasm_bindgen') -external dynamic get _noModules; +external dynamic get _jsWasmBindgen; void _ensureCrossOriginIsolated() { switch (crossOriginIsolated) { From aa6884adf7f0b90057a08745bd942402ac5b3bdf Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:04:17 +0800 Subject: [PATCH 17/39] chore: temp demo --- .../dart_minimal/flutter_rust_bridge.yaml | 5 +- frb_example/dart_minimal/frb_generated.h | 29 ++++++- .../lib/src/rust/frb_generated.dart | 32 ++++--- .../lib/src/rust/frb_generated.io.dart | 86 +++++++++++++++++++ .../lib/src/rust/frb_generated.web.dart | 26 ++++-- .../dart_minimal/rust/src/frb_generated.io.rs | 14 +++ .../dart_minimal/rust/src/frb_generated.rs | 54 ++++-------- .../rust/src/frb_generated.web.rs | 23 +++++ 8 files changed, 213 insertions(+), 56 deletions(-) diff --git a/frb_example/dart_minimal/flutter_rust_bridge.yaml b/frb_example/dart_minimal/flutter_rust_bridge.yaml index fbae6fb988..c6adde6f5f 100644 --- a/frb_example/dart_minimal/flutter_rust_bridge.yaml +++ b/frb_example/dart_minimal/flutter_rust_bridge.yaml @@ -3,4 +3,7 @@ rust_input: rust/src/api/**/*.rs dart_output: lib/src/rust c_output: frb_generated.h dump_all: true -local: true \ No newline at end of file +local: true + +# TODO temp +full_dep: true diff --git a/frb_example/dart_minimal/frb_generated.h b/frb_example/dart_minimal/frb_generated.h index ad87ade833..fe7304aab3 100644 --- a/frb_example/dart_minimal/frb_generated.h +++ b/frb_example/dart_minimal/frb_generated.h @@ -1 +1,28 @@ -// Nothing when using full_dep=false mode \ No newline at end of file +#include +#include +#include +// EXTRA BEGIN +typedef struct DartCObject *WireSyncRust2DartDco; +typedef struct WireSyncRust2DartSse { + uint8_t *ptr; + int32_t len; +} WireSyncRust2DartSse; + +typedef int64_t DartPort; +typedef bool (*DartPostCObjectFnType)(DartPort port_id, void *message); +void store_dart_post_cobject(DartPostCObjectFnType ptr); +// EXTRA END +typedef struct _Dart_Handle* Dart_Handle; + +void frbgen_frb_example_dart_minimal_wire__crate__api__minimal__init_app(int64_t port_); + +void frbgen_frb_example_dart_minimal_wire__crate__api__minimal__minimal_adder(int64_t port_, + int32_t a, + int32_t b); +static int64_t dummy_method_to_enforce_bundling(void) { + int64_t dummy_var = 0; + dummy_var ^= ((int64_t) (void*) frbgen_frb_example_dart_minimal_wire__crate__api__minimal__init_app); + dummy_var ^= ((int64_t) (void*) frbgen_frb_example_dart_minimal_wire__crate__api__minimal__minimal_adder); + dummy_var ^= ((int64_t) (void*) store_dart_post_cobject); + return dummy_var; +} diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.dart index ae3e06b17a..b5d5c2306c 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.dart @@ -85,12 +85,10 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { Future crateApiMinimalInitApp() { return handler.executeNormal(NormalTask( callFfi: (port_) { - final serializer = SseSerializer(generalizedFrbRustBinding); - pdeCallFfi(generalizedFrbRustBinding, serializer, - funcId: 1, port: port_); + return wire.wire__crate__api__minimal__init_app(port_); }, - codec: SseCodec( - decodeSuccessData: sse_decode_unit, + codec: DcoCodec( + decodeSuccessData: dco_decode_unit, decodeErrorData: null, ), constMeta: kCrateApiMinimalInitAppConstMeta, @@ -108,14 +106,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { Future crateApiMinimalMinimalAdder({required int a, required int b}) { return handler.executeNormal(NormalTask( callFfi: (port_) { - final serializer = SseSerializer(generalizedFrbRustBinding); - sse_encode_i_32(a, serializer); - sse_encode_i_32(b, serializer); - pdeCallFfi(generalizedFrbRustBinding, serializer, - funcId: 2, port: port_); + var arg0 = cst_encode_i_32(a); + var arg1 = cst_encode_i_32(b); + return wire.wire__crate__api__minimal__minimal_adder(port_, arg0, arg1); }, - codec: SseCodec( - decodeSuccessData: sse_decode_i_32, + codec: DcoCodec( + decodeSuccessData: dco_decode_i_32, decodeErrorData: null, ), constMeta: kCrateApiMinimalMinimalAdderConstMeta, @@ -159,6 +155,18 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return deserializer.buffer.getUint8() != 0; } + @protected + int cst_encode_i_32(int raw) { + // Codec=Cst (C-struct based), see doc to use other codecs + return raw; + } + + @protected + void cst_encode_unit(void raw) { + // Codec=Cst (C-struct based), see doc to use other codecs + return raw; + } + @protected void sse_encode_i_32(int self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart index 72e4d2de80..81881a1827 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.io.dart @@ -33,6 +33,12 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + int cst_encode_i_32(int raw); + + @protected + void cst_encode_unit(void raw); + @protected void sse_encode_i_32(int self, SseSerializer serializer); @@ -45,6 +51,13 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { // Section: wire_class +// ignore_for_file: camel_case_types, non_constant_identifier_names, avoid_positional_boolean_parameters, annotate_overrides, constant_identifier_names +// AUTO GENERATED FILE, DO NOT EDIT. +// +// Generated by `package:ffigen`. +// ignore_for_file: type=lint + +/// generated by flutter_rust_bridge class RustLibWire implements BaseWire { factory RustLibWire.fromExternalLibrary(ExternalLibrary lib) => RustLibWire(lib.ffiDynamicLibrary); @@ -56,4 +69,77 @@ class RustLibWire implements BaseWire { /// The symbols are looked up in [dynamicLibrary]. RustLibWire(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup; + + /// The symbols are looked up with [lookup]. + RustLibWire.fromLookup( + ffi.Pointer Function(String symbolName) + lookup) + : _lookup = lookup; + + void store_dart_post_cobject( + DartPostCObjectFnType ptr, + ) { + return _store_dart_post_cobject( + ptr, + ); + } + + late final _store_dart_post_cobjectPtr = + _lookup>( + 'store_dart_post_cobject'); + late final _store_dart_post_cobject = _store_dart_post_cobjectPtr + .asFunction(); + + void wire__crate__api__minimal__init_app( + int port_, + ) { + return _wire__crate__api__minimal__init_app( + port_, + ); + } + + late final _wire__crate__api__minimal__init_appPtr = _lookup< + ffi.NativeFunction>( + 'frbgen_frb_example_dart_minimal_wire__crate__api__minimal__init_app'); + late final _wire__crate__api__minimal__init_app = + _wire__crate__api__minimal__init_appPtr.asFunction(); + + void wire__crate__api__minimal__minimal_adder( + int port_, + int a, + int b, + ) { + return _wire__crate__api__minimal__minimal_adder( + port_, + a, + b, + ); + } + + late final _wire__crate__api__minimal__minimal_adderPtr = _lookup< + ffi + .NativeFunction>( + 'frbgen_frb_example_dart_minimal_wire__crate__api__minimal__minimal_adder'); + late final _wire__crate__api__minimal__minimal_adder = + _wire__crate__api__minimal__minimal_adderPtr + .asFunction(); + + int dummy_method_to_enforce_bundling() { + return _dummy_method_to_enforce_bundling(); + } + + late final _dummy_method_to_enforce_bundlingPtr = + _lookup>( + 'dummy_method_to_enforce_bundling'); + late final _dummy_method_to_enforce_bundling = + _dummy_method_to_enforce_bundlingPtr.asFunction(); } + +typedef DartPostCObjectFnType + = ffi.Pointer>; +typedef DartPostCObjectFnTypeFunction = ffi.Bool Function( + DartPort port_id, ffi.Pointer message); +typedef DartDartPostCObjectFnTypeFunction = bool Function( + DartDartPort port_id, ffi.Pointer message); +typedef DartPort = ffi.Int64; +typedef DartDartPort = int; diff --git a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart index 2cd4f18dbd..1ff896a666 100644 --- a/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart +++ b/frb_example/dart_minimal/lib/src/rust/frb_generated.web.dart @@ -6,13 +6,11 @@ // Static analysis wrongly picks the IO variant, thus ignore this // ignore_for_file: argument_type_not_assignable +import 'api/minimal.dart'; import 'dart:async'; import 'dart:convert'; - -import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated_web.dart'; - -import 'api/minimal.dart'; import 'frb_generated.dart'; +import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated_web.dart'; abstract class RustLibApiImplPlatform extends BaseApiImpl { RustLibApiImplPlatform({ @@ -37,6 +35,12 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + int cst_encode_i_32(int raw); + + @protected + void cst_encode_unit(void raw); + @protected void sse_encode_i_32(int self, SseSerializer serializer); @@ -51,6 +55,13 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { class RustLibWire implements BaseWire { RustLibWire.fromExternalLibrary(ExternalLibrary lib); + + void wire__crate__api__minimal__init_app(NativePortType port_) => + wasmModule.wire__crate__api__minimal__init_app(port_); + + void wire__crate__api__minimal__minimal_adder( + NativePortType port_, int a, int b) => + wasmModule.wire__crate__api__minimal__minimal_adder(port_, a, b); } @JS('wasm_bindgen') @@ -58,4 +69,9 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule {} +class RustLibWasmModule { + external void wire__crate__api__minimal__init_app(NativePortType port_); + + external void wire__crate__api__minimal__minimal_adder( + NativePortType port_, int a, int b); +} diff --git a/frb_example/dart_minimal/rust/src/frb_generated.io.rs b/frb_example/dart_minimal/rust/src/frb_generated.io.rs index 7c831957c4..a209f3e0ab 100644 --- a/frb_example/dart_minimal/rust/src/frb_generated.io.rs +++ b/frb_example/dart_minimal/rust/src/frb_generated.io.rs @@ -11,3 +11,17 @@ use flutter_rust_bridge::{Handler, IntoIntoDart}; // Section: boilerplate flutter_rust_bridge::frb_generated_boilerplate_io!(); + +#[no_mangle] +pub extern "C" fn frbgen_frb_example_dart_minimal_wire__crate__api__minimal__init_app(port_: i64) { + wire__crate__api__minimal__init_app_impl(port_) +} + +#[no_mangle] +pub extern "C" fn frbgen_frb_example_dart_minimal_wire__crate__api__minimal__minimal_adder( + port_: i64, + a: i32, + b: i32, +) { + wire__crate__api__minimal__minimal_adder_impl(port_, a, b) +} diff --git a/frb_example/dart_minimal/rust/src/frb_generated.rs b/frb_example/dart_minimal/rust/src/frb_generated.rs index 361f76bac8..13191b2526 100644 --- a/frb_example/dart_minimal/rust/src/frb_generated.rs +++ b/frb_example/dart_minimal/rust/src/frb_generated.rs @@ -28,9 +28,9 @@ use flutter_rust_bridge::{Handler, IntoIntoDart}; // Section: boilerplate flutter_rust_bridge::frb_generated_boilerplate!( - default_stream_sink_codec = SseCodec, - default_rust_opaque = RustOpaqueMoi, - default_rust_auto_opaque = RustAutoOpaqueMoi, + default_stream_sink_codec = DcoCodec, + default_rust_opaque = RustOpaqueNom, + default_rust_auto_opaque = RustAutoOpaqueNom, ); pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.38"; pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -2119384465; @@ -43,29 +43,16 @@ flutter_rust_bridge::frb_generated_default_handler!(); fn wire__crate__api__minimal__init_app_impl( port_: flutter_rust_bridge::for_generated::MessagePort, - ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, - rust_vec_len_: i32, - data_len_: i32, ) { - FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::( + FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::( flutter_rust_bridge::for_generated::TaskInfo { debug_name: "init_app", port: Some(port_), mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal, }, move || { - let message = unsafe { - flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire( - ptr_, - rust_vec_len_, - data_len_, - ) - }; - let mut deserializer = - flutter_rust_bridge::for_generated::SseDeserializer::new(message); - deserializer.end(); move |context| { - transform_result_sse((move || { + transform_result_dco((move || { Result::<_, ()>::Ok({ crate::api::minimal::init_app(); }) @@ -76,31 +63,20 @@ fn wire__crate__api__minimal__init_app_impl( } fn wire__crate__api__minimal__minimal_adder_impl( port_: flutter_rust_bridge::for_generated::MessagePort, - ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr, - rust_vec_len_: i32, - data_len_: i32, + a: impl CstDecode, + b: impl CstDecode, ) { - FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::( + FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::( flutter_rust_bridge::for_generated::TaskInfo { debug_name: "minimal_adder", port: Some(port_), mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal, }, move || { - let message = unsafe { - flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire( - ptr_, - rust_vec_len_, - data_len_, - ) - }; - let mut deserializer = - flutter_rust_bridge::for_generated::SseDeserializer::new(message); - let api_a = ::sse_decode(&mut deserializer); - let api_b = ::sse_decode(&mut deserializer); - deserializer.end(); + let api_a = a.cst_decode(); + let api_b = b.cst_decode(); move |context| { - transform_result_sse((move || { + transform_result_dco((move || { Result::<_, ()>::Ok(crate::api::minimal::minimal_adder(api_a, api_b)) })()) } @@ -110,6 +86,12 @@ fn wire__crate__api__minimal__minimal_adder_impl( // Section: dart2rust +impl CstDecode for i32 { + // Codec=Cst (C-struct based), see doc to use other codecs + fn cst_decode(self) -> i32 { + self + } +} impl SseDecode for i32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -138,8 +120,6 @@ fn pde_ffi_dispatcher_primary_impl( ) { // Codec=Pde (Serialization + dispatch), see doc to use other codecs match func_id { - 1 => wire__crate__api__minimal__init_app_impl(port, ptr, rust_vec_len, data_len), - 2 => wire__crate__api__minimal__minimal_adder_impl(port, ptr, rust_vec_len, data_len), _ => unreachable!(), } } diff --git a/frb_example/dart_minimal/rust/src/frb_generated.web.rs b/frb_example/dart_minimal/rust/src/frb_generated.web.rs index 0a83f28fdd..4fb5ac0afe 100644 --- a/frb_example/dart_minimal/rust/src/frb_generated.web.rs +++ b/frb_example/dart_minimal/rust/src/frb_generated.web.rs @@ -13,3 +13,26 @@ use flutter_rust_bridge::{Handler, IntoIntoDart}; // Section: boilerplate flutter_rust_bridge::frb_generated_boilerplate_web!(); + +// Section: dart2rust + +impl CstDecode for flutter_rust_bridge::for_generated::wasm_bindgen::JsValue { + // Codec=Cst (C-struct based), see doc to use other codecs + fn cst_decode(self) -> i32 { + self.unchecked_into_f64() as _ + } +} + +#[wasm_bindgen] +pub fn wire__crate__api__minimal__init_app(port_: flutter_rust_bridge::for_generated::MessagePort) { + wire__crate__api__minimal__init_app_impl(port_) +} + +#[wasm_bindgen] +pub fn wire__crate__api__minimal__minimal_adder( + port_: flutter_rust_bridge::for_generated::MessagePort, + a: i32, + b: i32, +) { + wire__crate__api__minimal__minimal_adder_impl(port_, a, b) +} From d6f21954491f9143633145df0a0d8359621b21bc Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:05:14 +0800 Subject: [PATCH 18/39] feat: simp --- .../lib/src/generalized_isolate/_web.dart | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 318508d71a..1b298d5068 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -124,7 +124,7 @@ ReceivePort broadcastPort(String channelName) => ReceivePort(RawReceivePort(Channel.broadcastChannel(channelName))); /// [html.MessagePort]'s interface. -abstract class PortLike extends EventTarget { +abstract class PortLike { /// {@macro flutter_rust_bridge.only_for_generated_code} factory PortLike.messagePort(html.MessagePort port) = _MessagePortWrapper; @@ -144,24 +144,24 @@ abstract class PortLike extends EventTarget { /// Delegates a subset of PortLike methods verbatim. abstract class _DelegatedPort implements PortLike { - @override - void addEventListener(String type, html.EventListener? listener, - [bool? useCapture]) => - nativePort.addEventListener(type, listener, useCapture); - - @override - void removeEventListener(String type, html.EventListener? listener, - [bool? useCapture]) => - nativePort.removeEventListener(type, listener, useCapture); - @override void close() => nativePort.close(); - @override - bool dispatchEvent(html.Event event) => nativePort.dispatchEvent(event); - - @override - html.Events get on => nativePort.on; +// @override +// void addEventListener(String type, html.EventListener? listener, +// [bool? useCapture]) => +// nativePort.addEventListener(type, listener, useCapture); +// +// @override +// void removeEventListener(String type, html.EventListener? listener, +// [bool? useCapture]) => +// nativePort.removeEventListener(type, listener, useCapture); +// +// @override +// bool dispatchEvent(html.Event event) => nativePort.dispatchEvent(event); +// +// @override +// html.Events get on => nativePort.on; } class _MessagePortWrapper extends _DelegatedPort { @@ -195,5 +195,5 @@ class _BroadcastPortWrapper extends _DelegatedPort { extension on PortLike { static const messageEvent = EventStreamProvider('message'); - Stream get onMessage => messageEvent.forTarget(this); + Stream get onMessage => messageEvent.forTarget(nativePort); } From f8bedeaef69f0bb345bbebd8ee90d6a77e37f3b7 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:06:01 +0800 Subject: [PATCH 19/39] feat: more simp --- .../lib/src/generalized_isolate/_web.dart | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 1b298d5068..62a11fa0da 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -142,29 +142,7 @@ abstract class PortLike { NativePortType get nativePort; } -/// Delegates a subset of PortLike methods verbatim. -abstract class _DelegatedPort implements PortLike { - @override - void close() => nativePort.close(); - -// @override -// void addEventListener(String type, html.EventListener? listener, -// [bool? useCapture]) => -// nativePort.addEventListener(type, listener, useCapture); -// -// @override -// void removeEventListener(String type, html.EventListener? listener, -// [bool? useCapture]) => -// nativePort.removeEventListener(type, listener, useCapture); -// -// @override -// bool dispatchEvent(html.Event event) => nativePort.dispatchEvent(event); -// -// @override -// html.Events get on => nativePort.on; -} - -class _MessagePortWrapper extends _DelegatedPort { +class _MessagePortWrapper implements PortLike { @override final html.MessagePort nativePort; @@ -173,9 +151,12 @@ class _MessagePortWrapper extends _DelegatedPort { @override void postMessage(message, [List? transfer]) => nativePort.postMessage(message, transfer); + + @override + void close() => nativePort.close(); } -class _BroadcastPortWrapper extends _DelegatedPort { +class _BroadcastPortWrapper implements PortLike { @override final html.BroadcastChannel nativePort; @@ -190,6 +171,9 @@ class _BroadcastPortWrapper extends _DelegatedPort { } nativePort.postMessage(message ?? false); } + + @override + void close() => nativePort.close(); } extension on PortLike { From b16e41d1f26ac23839a8085ea451bfe7ebcfd929 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:06:15 +0800 Subject: [PATCH 20/39] chore: rename --- frb_dart/lib/src/generalized_isolate/_web.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 62a11fa0da..99aa8d6cd4 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -177,7 +177,7 @@ class _BroadcastPortWrapper implements PortLike { } extension on PortLike { - static const messageEvent = EventStreamProvider('message'); + static const _kMessageEvent = EventStreamProvider('message'); - Stream get onMessage => messageEvent.forTarget(nativePort); + Stream get onMessage => _kMessageEvent.forTarget(nativePort); } From fa801cbcdd17b82fcb5cf0e8b0695b458d2836c0 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:09:40 +0800 Subject: [PATCH 21/39] chore: ty --- frb_dart/lib/src/generalized_isolate/_web.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 99aa8d6cd4..5e8956a0aa 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -139,7 +139,7 @@ abstract class PortLike { void close(); /// {@macro flutter_rust_bridge.only_for_generated_code} - NativePortType get nativePort; + html.EventTarget get nativePort; } class _MessagePortWrapper implements PortLike { From c69088fb810ccdb8a2cbe8c0f7151ff091baa4f3 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:10:40 +0800 Subject: [PATCH 22/39] feat: more --- .../lib/src/generalized_isolate/_web.dart | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 5e8956a0aa..ce7a3588a1 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -125,6 +125,8 @@ ReceivePort broadcastPort(String channelName) => /// [html.MessagePort]'s interface. abstract class PortLike { + const PortLike._(); + /// {@macro flutter_rust_bridge.only_for_generated_code} factory PortLike.messagePort(html.MessagePort port) = _MessagePortWrapper; @@ -140,13 +142,17 @@ abstract class PortLike { /// {@macro flutter_rust_bridge.only_for_generated_code} html.EventTarget get nativePort; + + /// {@macro flutter_rust_bridge.only_for_generated_code} + Stream get onMessage => _kMessageEvent.forTarget(nativePort); + static const _kMessageEvent = EventStreamProvider('message'); } -class _MessagePortWrapper implements PortLike { +class _MessagePortWrapper extends PortLike { @override final html.MessagePort nativePort; - _MessagePortWrapper(this.nativePort); + _MessagePortWrapper(this.nativePort) : super._(); @override void postMessage(message, [List? transfer]) => @@ -156,11 +162,11 @@ class _MessagePortWrapper implements PortLike { void close() => nativePort.close(); } -class _BroadcastPortWrapper implements PortLike { +class _BroadcastPortWrapper extends PortLike { @override final html.BroadcastChannel nativePort; - _BroadcastPortWrapper(this.nativePort); + _BroadcastPortWrapper(this.nativePort) : super._(); /// This presents a limitation of BroadcastChannel, /// i.e. it cannot carry transferables and will unconditionally clone the items. @@ -175,9 +181,3 @@ class _BroadcastPortWrapper implements PortLike { @override void close() => nativePort.close(); } - -extension on PortLike { - static const _kMessageEvent = EventStreamProvider('message'); - - Stream get onMessage => _kMessageEvent.forTarget(nativePort); -} From 02d3780563588b2c9e1414d97ab4d3d3ce5509ff Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:13:43 +0800 Subject: [PATCH 23/39] refactor: make it private --- .../lib/src/generalized_isolate/_web.dart | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index ce7a3588a1..23d7aa2b10 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -17,26 +17,20 @@ typedef MessagePort = PortLike; /// An alias to [MessagePort] on web platforms. typedef SendPort = PortLike; -/// {@macro flutter_rust_bridge.only_for_generated_code} -abstract class Channel { - /// {@macro flutter_rust_bridge.only_for_generated_code} +abstract class _Channel { SendPort get sendPort; - /// {@macro flutter_rust_bridge.only_for_generated_code} SendPort get receivePort; - /// {@macro flutter_rust_bridge.only_for_generated_code} - const Channel(); + const _Channel(); - /// {@macro flutter_rust_bridge.only_for_generated_code} - factory Channel.messageChannel() = _MessageChannelWrapper; + factory _Channel.messageChannel() = _MessageChannelWrapper; - /// {@macro flutter_rust_bridge.only_for_generated_code} - factory Channel.broadcastChannel(String channelName) = + factory _Channel.broadcastChannel(String channelName) = _BroadcastChannelWrapper; } -class _MessageChannelWrapper implements Channel { +class _MessageChannelWrapper implements _Channel { final channel = MessageChannel(); @override @@ -46,7 +40,7 @@ class _MessageChannelWrapper implements Channel { SendPort get receivePort => PortLike.messagePort(channel.port1); } -class _BroadcastChannelWrapper implements Channel { +class _BroadcastChannelWrapper implements _Channel { final BroadcastChannel _sendChannel; final BroadcastChannel _receiveChannel; @@ -67,11 +61,11 @@ class _BroadcastChannelWrapper implements Channel { /// Wrapper around a [MessageChannel]. class RawReceivePort { /// The underlying message channel. - final Channel channel; + final _Channel channel; /// {@macro flutter_rust_bridge.only_for_generated_code} - RawReceivePort([Channel? channel]) - : channel = channel ?? Channel.messageChannel(); + RawReceivePort([_Channel? channel]) + : channel = channel ?? _Channel.messageChannel(); set handler(Function(dynamic) handler) { receivePort.onMessage.listen((event) => handler(event.data)); @@ -121,7 +115,7 @@ class ReceivePort extends Stream { /// {@macro flutter_rust_bridge.internal} ReceivePort broadcastPort(String channelName) => - ReceivePort(RawReceivePort(Channel.broadcastChannel(channelName))); + ReceivePort(RawReceivePort(_Channel.broadcastChannel(channelName))); /// [html.MessagePort]'s interface. abstract class PortLike { From 11a8b74289189afb60dc700c200e45595f481254 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:13:56 +0800 Subject: [PATCH 24/39] chore: mv --- .../lib/src/generalized_isolate/_web.dart | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 23d7aa2b10..e43bc74397 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -17,47 +17,6 @@ typedef MessagePort = PortLike; /// An alias to [MessagePort] on web platforms. typedef SendPort = PortLike; -abstract class _Channel { - SendPort get sendPort; - - SendPort get receivePort; - - const _Channel(); - - factory _Channel.messageChannel() = _MessageChannelWrapper; - - factory _Channel.broadcastChannel(String channelName) = - _BroadcastChannelWrapper; -} - -class _MessageChannelWrapper implements _Channel { - final channel = MessageChannel(); - - @override - SendPort get sendPort => PortLike.messagePort(channel.port2); - - @override - SendPort get receivePort => PortLike.messagePort(channel.port1); -} - -class _BroadcastChannelWrapper implements _Channel { - final BroadcastChannel _sendChannel; - final BroadcastChannel _receiveChannel; - - _BroadcastChannelWrapper(String channelName) - // Note: It is *wrong* to reuse the same HTML BroadcastChannel object, - // because HTML BroadcastChannel spec says that, the event will not be fired - // at the object which sends it. Therefore, we need two different objects. - : _sendChannel = BroadcastChannel(channelName), - _receiveChannel = BroadcastChannel(channelName); - - @override - SendPort get sendPort => PortLike.broadcastChannel(_sendChannel); - - @override - SendPort get receivePort => PortLike.broadcastChannel(_receiveChannel); -} - /// Wrapper around a [MessageChannel]. class RawReceivePort { /// The underlying message channel. @@ -117,6 +76,47 @@ class ReceivePort extends Stream { ReceivePort broadcastPort(String channelName) => ReceivePort(RawReceivePort(_Channel.broadcastChannel(channelName))); +abstract class _Channel { + SendPort get sendPort; + + SendPort get receivePort; + + const _Channel(); + + factory _Channel.messageChannel() = _MessageChannelWrapper; + + factory _Channel.broadcastChannel(String channelName) = + _BroadcastChannelWrapper; +} + +class _MessageChannelWrapper implements _Channel { + final channel = MessageChannel(); + + @override + SendPort get sendPort => PortLike.messagePort(channel.port2); + + @override + SendPort get receivePort => PortLike.messagePort(channel.port1); +} + +class _BroadcastChannelWrapper implements _Channel { + final BroadcastChannel _sendChannel; + final BroadcastChannel _receiveChannel; + + _BroadcastChannelWrapper(String channelName) + // Note: It is *wrong* to reuse the same HTML BroadcastChannel object, + // because HTML BroadcastChannel spec says that, the event will not be fired + // at the object which sends it. Therefore, we need two different objects. + : _sendChannel = BroadcastChannel(channelName), + _receiveChannel = BroadcastChannel(channelName); + + @override + SendPort get sendPort => PortLike.broadcastChannel(_sendChannel); + + @override + SendPort get receivePort => PortLike.broadcastChannel(_receiveChannel); +} + /// [html.MessagePort]'s interface. abstract class PortLike { const PortLike._(); From 4a4201f9c65c4df2384f9b1a2d1771efa801b8aa Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:14:48 +0800 Subject: [PATCH 25/39] feat: more private --- frb_dart/lib/src/generalized_isolate/_web.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index e43bc74397..464aa426a5 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -20,24 +20,24 @@ typedef SendPort = PortLike; /// Wrapper around a [MessageChannel]. class RawReceivePort { /// The underlying message channel. - final _Channel channel; + final _Channel _channel; /// {@macro flutter_rust_bridge.only_for_generated_code} RawReceivePort([_Channel? channel]) - : channel = channel ?? _Channel.messageChannel(); + : _channel = channel ?? _Channel.messageChannel(); set handler(Function(dynamic) handler) { receivePort.onMessage.listen((event) => handler(event.data)); } /// Close the receive port. - void close() => channel.receivePort.close(); + void close() => _channel.receivePort.close(); /// The port to be used by other workers. - SendPort get sendPort => channel.sendPort; + SendPort get sendPort => _channel.sendPort; /// The port used to receive messages from other workers. - SendPort get receivePort => channel.receivePort; + SendPort get receivePort => _channel.receivePort; } /// Web implementation of the `dart:isolate`'s ReceivePort. From df8e74c425dfc9318b0b2d10c358596edb265fc5 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:15:26 +0800 Subject: [PATCH 26/39] feat: factory --- frb_dart/lib/src/generalized_isolate/_web.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 464aa426a5..2b113c3df8 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -23,7 +23,9 @@ class RawReceivePort { final _Channel _channel; /// {@macro flutter_rust_bridge.only_for_generated_code} - RawReceivePort([_Channel? channel]) + factory RawReceivePort() => RawReceivePort._raw(); + + RawReceivePort._raw([_Channel? channel]) : _channel = channel ?? _Channel.messageChannel(); set handler(Function(dynamic) handler) { @@ -74,7 +76,7 @@ class ReceivePort extends Stream { /// {@macro flutter_rust_bridge.internal} ReceivePort broadcastPort(String channelName) => - ReceivePort(RawReceivePort(_Channel.broadcastChannel(channelName))); + ReceivePort(RawReceivePort._raw(_Channel.broadcastChannel(channelName))); abstract class _Channel { SendPort get sendPort; From 801df3a289774f34af7cda3a285299cca71febb0 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:15:41 +0800 Subject: [PATCH 27/39] chore: mv --- .../lib/src/generalized_isolate/_web.dart | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 2b113c3df8..987b6d0c65 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -17,31 +17,6 @@ typedef MessagePort = PortLike; /// An alias to [MessagePort] on web platforms. typedef SendPort = PortLike; -/// Wrapper around a [MessageChannel]. -class RawReceivePort { - /// The underlying message channel. - final _Channel _channel; - - /// {@macro flutter_rust_bridge.only_for_generated_code} - factory RawReceivePort() => RawReceivePort._raw(); - - RawReceivePort._raw([_Channel? channel]) - : _channel = channel ?? _Channel.messageChannel(); - - set handler(Function(dynamic) handler) { - receivePort.onMessage.listen((event) => handler(event.data)); - } - - /// Close the receive port. - void close() => _channel.receivePort.close(); - - /// The port to be used by other workers. - SendPort get sendPort => _channel.sendPort; - - /// The port used to receive messages from other workers. - SendPort get receivePort => _channel.receivePort; -} - /// Web implementation of the `dart:isolate`'s ReceivePort. class ReceivePort extends Stream { /// The receive port. @@ -74,6 +49,31 @@ class ReceivePort extends Stream { void close() => port.receivePort.close(); } +/// Wrapper around a [MessageChannel]. +class RawReceivePort { + /// The underlying message channel. + final _Channel _channel; + + /// {@macro flutter_rust_bridge.only_for_generated_code} + factory RawReceivePort() => RawReceivePort._raw(); + + RawReceivePort._raw([_Channel? channel]) + : _channel = channel ?? _Channel.messageChannel(); + + set handler(Function(dynamic) handler) { + receivePort.onMessage.listen((event) => handler(event.data)); + } + + /// Close the receive port. + void close() => _channel.receivePort.close(); + + /// The port to be used by other workers. + SendPort get sendPort => _channel.sendPort; + + /// The port used to receive messages from other workers. + SendPort get receivePort => _channel.receivePort; +} + /// {@macro flutter_rust_bridge.internal} ReceivePort broadcastPort(String channelName) => ReceivePort(RawReceivePort._raw(_Channel.broadcastChannel(channelName))); From 8afc050a615f2784245f85b50a91636669caee41 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:17:00 +0800 Subject: [PATCH 28/39] chore: rename --- frb_dart/lib/src/generalized_isolate/_web.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 987b6d0c65..b09f2c5b58 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -20,12 +20,13 @@ typedef SendPort = PortLike; /// Web implementation of the `dart:isolate`'s ReceivePort. class ReceivePort extends Stream { /// The receive port. - final RawReceivePort port; + final RawReceivePort _rawReceivePort; static dynamic _extractData(MessageEvent event) => event.data; /// Create a new receive port from an optional [RawReceivePort]. - ReceivePort([RawReceivePort? port]) : port = port ?? RawReceivePort(); + ReceivePort([RawReceivePort? rawReceivePort]) + : _rawReceivePort = rawReceivePort ?? RawReceivePort(); @override StreamSubscription listen( @@ -34,7 +35,7 @@ class ReceivePort extends Stream { void Function()? onDone, bool? cancelOnError, }) { - return port.receivePort.onMessage.map(_extractData).listen( + return _rawReceivePort.receivePort.onMessage.map(_extractData).listen( onData, onError: onError, onDone: onDone, @@ -43,10 +44,10 @@ class ReceivePort extends Stream { } /// The send port. - SendPort get sendPort => port.sendPort; + SendPort get sendPort => _rawReceivePort.sendPort; /// Close the receive port, ignoring any further messages. - void close() => port.receivePort.close(); + void close() => _rawReceivePort.receivePort.close(); } /// Wrapper around a [MessageChannel]. From 411f96904488db893f643185ac85a2028db35e0c Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:17:24 +0800 Subject: [PATCH 29/39] feat: simp api --- frb_dart/lib/src/generalized_isolate/_web.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index b09f2c5b58..83daf246cc 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -25,7 +25,9 @@ class ReceivePort extends Stream { static dynamic _extractData(MessageEvent event) => event.data; /// Create a new receive port from an optional [RawReceivePort]. - ReceivePort([RawReceivePort? rawReceivePort]) + factory ReceivePort() => ReceivePort._raw(); + + ReceivePort._raw([RawReceivePort? rawReceivePort]) : _rawReceivePort = rawReceivePort ?? RawReceivePort(); @override @@ -76,8 +78,8 @@ class RawReceivePort { } /// {@macro flutter_rust_bridge.internal} -ReceivePort broadcastPort(String channelName) => - ReceivePort(RawReceivePort._raw(_Channel.broadcastChannel(channelName))); +ReceivePort broadcastPort(String channelName) => ReceivePort._raw( + RawReceivePort._raw(_Channel.broadcastChannel(channelName))); abstract class _Channel { SendPort get sendPort; From a2a1acd0b6097d88167991c4b1536025a594fccb Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:17:41 +0800 Subject: [PATCH 30/39] chore: mv --- frb_dart/lib/src/generalized_isolate/_web.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 83daf246cc..48e8ab0c42 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -22,8 +22,6 @@ class ReceivePort extends Stream { /// The receive port. final RawReceivePort _rawReceivePort; - static dynamic _extractData(MessageEvent event) => event.data; - /// Create a new receive port from an optional [RawReceivePort]. factory ReceivePort() => ReceivePort._raw(); @@ -45,6 +43,8 @@ class ReceivePort extends Stream { ); } + static dynamic _extractData(MessageEvent event) => event.data; + /// The send port. SendPort get sendPort => _rawReceivePort.sendPort; From e1153fa515187a74a29d1cb39d6e29745382c3c9 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:18:50 +0800 Subject: [PATCH 31/39] chore: comment --- frb_dart/lib/src/generalized_isolate/_web.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 48e8ab0c42..0ec9435dec 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -63,9 +63,10 @@ class RawReceivePort { RawReceivePort._raw([_Channel? channel]) : _channel = channel ?? _Channel.messageChannel(); - set handler(Function(dynamic) handler) { - receivePort.onMessage.listen((event) => handler(event.data)); - } + // Not used currently thus comment out; but if needed, can uncomment later + // set handler(Function(dynamic) handler) { + // receivePort.onMessage.listen((event) => handler(event.data)); + // } /// Close the receive port. void close() => _channel.receivePort.close(); From 13998358f70613768099c093c27da80a8319b333 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:19:31 +0800 Subject: [PATCH 32/39] feat: private --- frb_dart/lib/src/generalized_isolate/_web.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 0ec9435dec..9274c2d025 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -74,8 +74,7 @@ class RawReceivePort { /// The port to be used by other workers. SendPort get sendPort => _channel.sendPort; - /// The port used to receive messages from other workers. - SendPort get receivePort => _channel.receivePort; + SendPort get _receivePort => _channel.receivePort; } /// {@macro flutter_rust_bridge.internal} From 926d347eb2929a19a8b4631bc1a59fcee4974387 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:19:40 +0800 Subject: [PATCH 33/39] refactor: more --- frb_dart/lib/src/generalized_isolate/_web.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 9274c2d025..5ff22571c4 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -49,7 +49,7 @@ class ReceivePort extends Stream { SendPort get sendPort => _rawReceivePort.sendPort; /// Close the receive port, ignoring any further messages. - void close() => _rawReceivePort.receivePort.close(); + void close() => _rawReceivePort.close(); } /// Wrapper around a [MessageChannel]. From 40d621ea0573844d4c2df89930d8093cf5fd33a2 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:19:48 +0800 Subject: [PATCH 34/39] feat: more --- frb_dart/lib/src/generalized_isolate/_web.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 5ff22571c4..b6d32e0765 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -35,7 +35,7 @@ class ReceivePort extends Stream { void Function()? onDone, bool? cancelOnError, }) { - return _rawReceivePort.receivePort.onMessage.map(_extractData).listen( + return _rawReceivePort._receivePort.onMessage.map(_extractData).listen( onData, onError: onError, onDone: onDone, From 213a0fd8cf390f4946268caec9cb5f8f0c8d6356 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:20:21 +0800 Subject: [PATCH 35/39] chore: more private --- .../lib/src/generalized_isolate/_web.dart | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index b6d32e0765..5645d171c9 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -12,10 +12,10 @@ import 'package:flutter_rust_bridge/src/platform_utils/_web.dart'; String serializeNativePort(NativePortType port) => port.name; /// {@macro flutter_rust_bridge.only_for_generated_code} -typedef MessagePort = PortLike; +typedef MessagePort = _PortLike; /// An alias to [MessagePort] on web platforms. -typedef SendPort = PortLike; +typedef SendPort = _PortLike; /// Web implementation of the `dart:isolate`'s ReceivePort. class ReceivePort extends Stream { @@ -98,10 +98,10 @@ class _MessageChannelWrapper implements _Channel { final channel = MessageChannel(); @override - SendPort get sendPort => PortLike.messagePort(channel.port2); + SendPort get sendPort => _PortLike.messagePort(channel.port2); @override - SendPort get receivePort => PortLike.messagePort(channel.port1); + SendPort get receivePort => _PortLike.messagePort(channel.port1); } class _BroadcastChannelWrapper implements _Channel { @@ -116,21 +116,21 @@ class _BroadcastChannelWrapper implements _Channel { _receiveChannel = BroadcastChannel(channelName); @override - SendPort get sendPort => PortLike.broadcastChannel(_sendChannel); + SendPort get sendPort => _PortLike.broadcastChannel(_sendChannel); @override - SendPort get receivePort => PortLike.broadcastChannel(_receiveChannel); + SendPort get receivePort => _PortLike.broadcastChannel(_receiveChannel); } /// [html.MessagePort]'s interface. -abstract class PortLike { - const PortLike._(); +abstract class _PortLike { + const _PortLike._(); /// {@macro flutter_rust_bridge.only_for_generated_code} - factory PortLike.messagePort(html.MessagePort port) = _MessagePortWrapper; + factory _PortLike.messagePort(html.MessagePort port) = _MessagePortWrapper; /// {@macro flutter_rust_bridge.only_for_generated_code} - factory PortLike.broadcastChannel(BroadcastChannel channel) = + factory _PortLike.broadcastChannel(BroadcastChannel channel) = _BroadcastPortWrapper; /// {@macro flutter_rust_bridge.only_for_generated_code} @@ -147,7 +147,7 @@ abstract class PortLike { static const _kMessageEvent = EventStreamProvider('message'); } -class _MessagePortWrapper extends PortLike { +class _MessagePortWrapper extends _PortLike { @override final html.MessagePort nativePort; @@ -161,7 +161,7 @@ class _MessagePortWrapper extends PortLike { void close() => nativePort.close(); } -class _BroadcastPortWrapper extends PortLike { +class _BroadcastPortWrapper extends _PortLike { @override final html.BroadcastChannel nativePort; From 1b1464bc020a42d86fc410532c910aa112738b58 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:22:07 +0800 Subject: [PATCH 36/39] chore: uncomment --- frb_dart/lib/src/generalized_isolate/_web.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 5645d171c9..65a827a294 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -63,10 +63,9 @@ class RawReceivePort { RawReceivePort._raw([_Channel? channel]) : _channel = channel ?? _Channel.messageChannel(); - // Not used currently thus comment out; but if needed, can uncomment later - // set handler(Function(dynamic) handler) { - // receivePort.onMessage.listen((event) => handler(event.data)); - // } + set handler(Function(dynamic) handler) { + _receivePort.onMessage.listen((event) => handler(event.data)); + } /// Close the receive port. void close() => _channel.receivePort.close(); From 7c63f7809fd80a0ac2389bdbdbeb98f1a1d1f263 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:26:41 +0800 Subject: [PATCH 37/39] chore: comments --- frb_dart/lib/src/generalized_isolate/_web.dart | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/frb_dart/lib/src/generalized_isolate/_web.dart b/frb_dart/lib/src/generalized_isolate/_web.dart index 65a827a294..cd1a21060a 100644 --- a/frb_dart/lib/src/generalized_isolate/_web.dart +++ b/frb_dart/lib/src/generalized_isolate/_web.dart @@ -14,7 +14,7 @@ String serializeNativePort(NativePortType port) => port.name; /// {@macro flutter_rust_bridge.only_for_generated_code} typedef MessagePort = _PortLike; -/// An alias to [MessagePort] on web platforms. +/// {@macro flutter_rust_bridge.only_for_generated_code} typedef SendPort = _PortLike; /// Web implementation of the `dart:isolate`'s ReceivePort. @@ -125,23 +125,17 @@ class _BroadcastChannelWrapper implements _Channel { abstract class _PortLike { const _PortLike._(); - /// {@macro flutter_rust_bridge.only_for_generated_code} factory _PortLike.messagePort(html.MessagePort port) = _MessagePortWrapper; - /// {@macro flutter_rust_bridge.only_for_generated_code} factory _PortLike.broadcastChannel(BroadcastChannel channel) = _BroadcastPortWrapper; - /// {@macro flutter_rust_bridge.only_for_generated_code} void postMessage(Object? value); - /// {@macro flutter_rust_bridge.only_for_generated_code} void close(); - /// {@macro flutter_rust_bridge.only_for_generated_code} html.EventTarget get nativePort; - /// {@macro flutter_rust_bridge.only_for_generated_code} Stream get onMessage => _kMessageEvent.forTarget(nativePort); static const _kMessageEvent = EventStreamProvider('message'); } From 68ad0d9691fec91f674ffe9254c281dc5692bb6a Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:27:22 +0800 Subject: [PATCH 38/39] chore: codegen --- .../deliberate_bad/lib/src/rust/frb_generated.web.dart | 8 +------- .../lib/src/rust/frb_generated.web.dart | 8 +------- .../lib/src/rust/frb_generated.web.dart | 8 +------- frb_example/gallery/lib/src/rust/frb_generated.web.dart | 8 +------- 4 files changed, 4 insertions(+), 28 deletions(-) diff --git a/frb_example/deliberate_bad/lib/src/rust/frb_generated.web.dart b/frb_example/deliberate_bad/lib/src/rust/frb_generated.web.dart index 3efd54053f..3f52037434 100644 --- a/frb_example/deliberate_bad/lib/src/rust/frb_generated.web.dart +++ b/frb_example/deliberate_bad/lib/src/rust/frb_generated.web.dart @@ -53,10 +53,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} diff --git a/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart b/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart index 7392234242..118fd82cdf 100644 --- a/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart +++ b/frb_example/flutter_via_create/lib/src/rust/frb_generated.web.dart @@ -81,10 +81,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} diff --git a/frb_example/flutter_via_integrate/lib/src/rust/frb_generated.web.dart b/frb_example/flutter_via_integrate/lib/src/rust/frb_generated.web.dart index 7392234242..118fd82cdf 100644 --- a/frb_example/flutter_via_integrate/lib/src/rust/frb_generated.web.dart +++ b/frb_example/flutter_via_integrate/lib/src/rust/frb_generated.web.dart @@ -81,10 +81,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} diff --git a/frb_example/gallery/lib/src/rust/frb_generated.web.dart b/frb_example/gallery/lib/src/rust/frb_generated.web.dart index 85c6901522..7adde927d2 100644 --- a/frb_example/gallery/lib/src/rust/frb_generated.web.dart +++ b/frb_example/gallery/lib/src/rust/frb_generated.web.dart @@ -130,10 +130,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} From f9331abecbd54d1c76a06774f8e505165cf8142f Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Mon, 10 Jun 2024 22:32:22 +0800 Subject: [PATCH 39/39] chore: more codegen --- .../lib/src/rust/frb_generated.web.dart | 8 +------- .../dart_build_rs/lib/src/rust/frb_generated.web.dart | 8 +------- .../lib/src/rust/frb_generated.web.dart | 8 +------- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/frb_codegen/assets/integration_template/lib/src/rust/frb_generated.web.dart b/frb_codegen/assets/integration_template/lib/src/rust/frb_generated.web.dart index 7392234242..118fd82cdf 100644 --- a/frb_codegen/assets/integration_template/lib/src/rust/frb_generated.web.dart +++ b/frb_codegen/assets/integration_template/lib/src/rust/frb_generated.web.dart @@ -81,10 +81,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} diff --git a/frb_example/dart_build_rs/lib/src/rust/frb_generated.web.dart b/frb_example/dart_build_rs/lib/src/rust/frb_generated.web.dart index 101ddd1728..69e39a7a08 100644 --- a/frb_example/dart_build_rs/lib/src/rust/frb_generated.web.dart +++ b/frb_example/dart_build_rs/lib/src/rust/frb_generated.web.dart @@ -56,10 +56,4 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); -} +class RustLibWasmModule {} diff --git a/frb_example/integrate_third_party/lib/src/rust/frb_generated.web.dart b/frb_example/integrate_third_party/lib/src/rust/frb_generated.web.dart index 4ee42d3588..2caf1d33f5 100644 --- a/frb_example/integrate_third_party/lib/src/rust/frb_generated.web.dart +++ b/frb_example/integrate_third_party/lib/src/rust/frb_generated.web.dart @@ -4186,13 +4186,7 @@ external RustLibWasmModule get wasmModule; @JS() @anonymous -class RustLibWasmModule implements WasmModule { - @override - external Object /* Promise */ call([String? moduleName]); - - @override - external RustLibWasmModule bind(dynamic thisArg, String moduleName); - +class RustLibWasmModule { external void rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerAnalyserNode( dynamic ptr);