From 0ac4928c80cff4dc2ec91de624d709b8763a2400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Ayd=C4=B1n?= <84200491+ahmtydn@users.noreply.github.com> Date: Fri, 3 Apr 2026 17:49:51 +0300 Subject: [PATCH] fix: Simplify checks for wasm_bindgen availability in initialization --- packages/isar_plus/lib/src/web/web.dart | 45 ++++++++++++------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/packages/isar_plus/lib/src/web/web.dart b/packages/isar_plus/lib/src/web/web.dart index 15c53be7..8ccc4d5c 100644 --- a/packages/isar_plus/lib/src/web/web.dart +++ b/packages/isar_plus/lib/src/web/web.dart @@ -26,9 +26,7 @@ FutureOr initializePlatformBindings([String? library]) async { if (!_scriptLoaded) { // Check if wasm_bindgen is already available final wasmBindgenExists = _windowWasmBindgen; - if (wasmBindgenExists == null || - wasmBindgenExists.isUndefined || - wasmBindgenExists.isNull) { + if (wasmBindgenExists == null) { // Not loaded yet, load it dynamically await _loadWasmBindgenScript(url); } @@ -50,23 +48,26 @@ Future _loadWasmBindgenScript(String wasmUrl) async { final jsUrl = wasmUrl.replaceAll('.wasm', '.js'); // Create and inject the script tag - final script = web.document.createElement('script') as web.HTMLScriptElement - ..src = jsUrl - ..type = 'text/javascript' - ..onload = (web.Event event) { - // Schedule async work without making the callback async - unawaited(_verifyWasmBindgenLoaded(jsUrl, completer)); - }.toJS - ..onerror = (web.Event event) { - completer.completeError( - Exception( - 'Failed to load isar.js from $jsUrl. ' - 'Make sure both isar.wasm and isar.js are ' - 'available at the same location. ' - 'For local usage, copy both files to your web/ directory.', - ), - ); - }.toJS; + final script = + web.document.createElement('script') as web.HTMLScriptElement + ..src = jsUrl + ..type = 'text/javascript' + ..onload = + (web.Event event) { + // Schedule async work without making the callback async + unawaited(_verifyWasmBindgenLoaded(jsUrl, completer)); + }.toJS + ..onerror = + (web.Event event) { + completer.completeError( + Exception( + 'Failed to load isar.js from $jsUrl. ' + 'Make sure both isar.wasm and isar.js are ' + 'available at the same location. ' + 'For local usage, copy both files to your web/ directory.', + ), + ); + }.toJS; web.document.head!.appendChild(script); @@ -85,9 +86,7 @@ Future _verifyWasmBindgenLoaded( var attempts = 0; while (attempts < 10) { final wasmBindgenExists = _windowWasmBindgen; - if (wasmBindgenExists != null && - !wasmBindgenExists.isUndefined && - !wasmBindgenExists.isNull) { + if (wasmBindgenExists != null) { completer.complete(); return; }