Skip to content

Commit

Permalink
Add Rinf bindings object after checking
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Oct 23, 2024
1 parent 9a4e8ab commit f6c14fc
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions flutter_package/lib/src/load_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,20 @@ void setJsLibPath(String path) {
}

bool wasAlreadyLoaded = false;
js.JsObject rinfBindingsObject = getRinfBindingsObject();
js.JsObject rinfBindingsObject = js.context['rinfBindings'];
js.JsObject wasmBindingsObject = js.context['wasmBindings'];

/// When Dart performs hot restart,
/// the `rinfBindings` JavaScript object is already defined
/// as a global JavaScript variable.
void checkIfAlreadyLoaded() {
// When Dart performs hot restart,
// the `rinfBindings` JavaScript object is already defined
// as a global JavaScript variable.
wasAlreadyLoaded = js.context.hasProperty('rinfBindings');
}

/// Create the namespace JavaScript object.
/// This namespace object is used by Rust
/// to call functions defined in Dart.
js.JsObject getRinfBindingsObject() {
// Create a new `rinfBindings` JavaScript object if not present.
// Otherwise, return the existing one.
final js.JsObject jsObject;
if (wasAlreadyLoaded) {
jsObject = js.context['rinfBindings'];
} else {
jsObject = js.JsObject.jsify({});
js.context['rinfBindings'] = jsObject;
if (!wasAlreadyLoaded) {
// Create the namespace JavaScript object.
// This namespace object is used by Rust
// to call functions defined in Dart.
js.context['rinfBindings'] = js.JsObject.jsify({});
}
return jsObject;
}

Future<void> loadJsFile() async {
Expand All @@ -43,7 +33,7 @@ Future<void> loadJsFile() async {
}

final loadCompleter = Completer<void>();
js.context['completeRinfLoad'] = loadCompleter.complete;
rinfBindingsObject['completeRinfLoad'] = loadCompleter.complete;

// 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 @@ -60,8 +50,8 @@ Future<void> loadJsFile() async {
import init, * as wasmBindings from "$fullUrl";
globalThis.wasmBindings = wasmBindings;
await init();
completeRinfLoad();
delete window.completeRinfLoad;
rinfBindings.completeRinfLoad();
delete rinfBindings.completeRinfLoad;
''';
document.head!.append(scriptElement);

Expand Down

0 comments on commit f6c14fc

Please sign in to comment.