Skip to content

Commit

Permalink
Merge pull request #511 from cunarist/js-interop-package
Browse files Browse the repository at this point in the history
Replace deprecated Dart web packages
  • Loading branch information
temeddix authored Feb 13, 2025
2 parents 5cc45b5 + dd1eb06 commit 8b4c35e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
23 changes: 13 additions & 10 deletions flutter_package/lib/src/interface_web.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// ignore_for_file: avoid_web_libraries_in_flutter

import 'load_web.dart';
import 'dart:typed_data';
import 'interface.dart';
import 'dart:async';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'dart:typed_data';
import 'dart:convert';
import 'load_web.dart';
import 'interface.dart';

/// Sets the path to the JavaScript module
/// that needs to be loaded.
Expand All @@ -31,14 +33,14 @@ Future<void> prepareInterfaceReal(
return;
}
assignRustSignal(messageId, messageBytes, binary);
};
}.jsify();
}

void startRustLogicReal() {
if (wasAlreadyLoaded) {
return;
}
wasmBindingsObject.callMethod('start_rust_logic_extern', []);
wasmBindingsObject.callMethod('start_rust_logic_extern'.toJS);
}

void stopRustLogicReal() {
Expand All @@ -50,9 +52,10 @@ void sendDartSignalReal(
Uint8List messageBytes,
Uint8List binary,
) {
wasmBindingsObject.callMethod('send_dart_signal_extern', [
messageId,
messageBytes,
binary,
]);
wasmBindingsObject.callMethod(
'send_dart_signal_extern'.toJS,
messageId.toJS,
messageBytes.toJS,
binary.toJS,
);
}
22 changes: 12 additions & 10 deletions flutter_package/lib/src/load_web.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// ignore_for_file: avoid_web_libraries_in_flutter

import 'dart:js' as js;
import 'dart:html';
import 'dart:async';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:web/web.dart';

String? jsLibPath;

Expand All @@ -11,14 +12,14 @@ void setJsLibPath(String path) {
}

bool wasAlreadyLoaded = false;
js.JsObject rinfBindingsObject = js.context['rinfBindings'];
js.JsObject wasmBindingsObject = js.context['wasmBindings'];
final rinfBindingsObject = globalContext['rinfBindings'] as JSObject;
final wasmBindingsObject = globalContext['wasmBindings'] as JSObject;

Future<void> loadJsFile() async {
// When Dart performs hot restart,
// the `rinfBindings` JavaScript object is already defined
// as a global JavaScript variable.
wasAlreadyLoaded = js.context.hasProperty('rinfBindings');
wasAlreadyLoaded = globalContext.hasProperty('rinfBindings'.toJS) as bool;

// Stop loading if it already has been done.
if (wasAlreadyLoaded) {
Expand All @@ -28,11 +29,11 @@ Future<void> loadJsFile() async {
// Create the namespace JavaScript object.
// This namespace object is used by Rust
// to call functions defined in Dart.
js.context['rinfBindings'] = js.JsObject.jsify({});
globalContext['rinfBindings'] = JSObject();

// Prepare to await the module load.
final loadCompleter = Completer<void>();
rinfBindingsObject['completeRinfLoad'] = loadCompleter.complete;
rinfBindingsObject['completeRinfLoad'] = loadCompleter.complete.jsify();

// Flutter app doesn't always have the top-level path of the domain.
// Sometimes, the flutter app might be placed in a lower path.
Expand All @@ -43,15 +44,16 @@ Future<void> loadJsFile() async {
final path = jsLibPath ?? 'pkg/hub.js';

final fullUrl = baseHref.resolve(path);
final scriptElement = ScriptElement();
final scriptElement = HTMLScriptElement();
scriptElement.type = 'module';
scriptElement.innerHtml = '''
scriptElement.innerHTML = '''
import init, * as wasmBindings from "$fullUrl";
globalThis.wasmBindings = wasmBindings;
await init();
rinfBindings.completeRinfLoad();
delete rinfBindings.completeRinfLoad;
''';
'''
.toJS;
document.head!.append(scriptElement);

await loadCompleter.future;
Expand Down
1 change: 1 addition & 0 deletions flutter_package/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
yaml: ^3.1.2
args: ^2.5.0
chalkdart: ^2.2.1
web: ^1.1.0

dev_dependencies:
lints: ">=4.0.0 <6.0.0"
Expand Down

0 comments on commit 8b4c35e

Please sign in to comment.