From a024e12bceb1f7e36d0996d676e4811b88ffb68b Mon Sep 17 00:00:00 2001 From: paregi12 Date: Mon, 18 May 2026 12:57:50 +0530 Subject: [PATCH] fix(plugins): ensure runtime waits for async settings and stream results --- .../features/plugins/runtime/PluginRuntime.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/PluginRuntime.kt b/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/PluginRuntime.kt index 9d1da1486..3da6404be 100644 --- a/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/PluginRuntime.kt +++ b/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/PluginRuntime.kt @@ -12,6 +12,7 @@ import com.dokar.quickjs.binding.function import com.nuvio.app.features.plugins.runtime.network.FetchBridge import com.nuvio.app.features.plugins.runtime.network.UrlBridge import com.nuvio.app.features.plugins.runtime.wasm.WasmBridge +import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeout @@ -62,7 +63,7 @@ internal object PluginRuntime { ): String? = withContext(Dispatchers.Default) { withTimeout(PLUGIN_TIMEOUT_MS) { val jsRuntime = JsRuntime() - var resultJson: String? = null + val deferred = CompletableDeferred() try { jsRuntime.use { @@ -98,16 +99,14 @@ internal object PluginRuntime { })(); """.trimIndent() - var captureResult: String? = null function("__capture_settings_result") { args: Array -> - captureResult = args.getOrNull(0)?.toString() + deferred.complete(args.getOrNull(0)?.toString()) null } evaluate(callCode) - resultJson = captureResult + deferred.await() } - resultJson } catch (e: Exception) { null } @@ -124,11 +123,11 @@ internal object PluginRuntime { scraperSettings: Map, ): List { val jsRuntime = JsRuntime() - var resultJson = "[]" + val deferred = CompletableDeferred() val domBridge = DomBridge() val hostRegistry = HostApiRegistry().apply { - addModule(HostFunctions(scraperId) { resultJson = it }) + addModule(HostFunctions(scraperId) { deferred.complete(it) }) addModule(FetchBridge()) addModule(UrlBridge()) addModule(CryptoBridge()) @@ -178,9 +177,10 @@ internal object PluginRuntime { })(); """.trimIndent() evaluate(callCode) + + val resultJson = deferred.await() + return parseJsonResults(resultJson) } - - return parseJsonResults(resultJson) } finally { domBridge.clear() }