@@ -9,15 +9,15 @@ import io.rebble.cobble.shared.data.js.fromDevice
99import io.rebble.cobble.shared.database.dao.LockerDao
1010import io.rebble.cobble.shared.domain.state.ConnectionStateManager
1111import io.rebble.cobble.shared.domain.state.watchOrNull
12- import kotlinx.coroutines.CoroutineScope
12+ import kotlinx.coroutines.*
1313import kotlinx.coroutines.flow.MutableSharedFlow
14- import kotlinx.coroutines.launch
1514import kotlinx.serialization.encodeToString
1615import kotlinx.serialization.json.Json
1716import org.koin.core.component.KoinComponent
1817import org.koin.core.component.inject
18+ import kotlin.time.Duration.Companion.seconds
1919
20- class WebViewPrivatePKJSInterface (private val jsRunner : WebViewJsRunner , private val scope : CoroutineScope , private val outgoingAppMessages : MutableSharedFlow <String >): PrivatePKJSInterface, KoinComponent {
20+ class WebViewPrivatePKJSInterface (private val jsRunner : WebViewJsRunner , private val scope : CoroutineScope , private val outgoingAppMessages : MutableSharedFlow <Pair < CompletableDeferred < UByte >, String > >): PrivatePKJSInterface, KoinComponent {
2121 private val lockerDao: LockerDao by inject()
2222
2323 @JavascriptInterface
@@ -118,10 +118,17 @@ class WebViewPrivatePKJSInterface(private val jsRunner: WebViewJsRunner, private
118118 }
119119
120120 @JavascriptInterface
121- fun sendAppMessageString (jsonAppMessage : String ) {
121+ fun sendAppMessageString (jsonAppMessage : String ): Int {
122122 Logging .v(" sendAppMessageString" )
123- if (! outgoingAppMessages.tryEmit(jsonAppMessage)) {
123+ val completable = CompletableDeferred <UByte >()
124+ if (! outgoingAppMessages.tryEmit(Pair (completable, jsonAppMessage))) {
124125 Logging .e(" Failed to emit outgoing AppMessage" )
126+ error(" Failed to emit outgoing AppMessage" )
127+ }
128+ return runBlocking {
129+ withTimeout(10 .seconds) {
130+ completable.await().toInt()
131+ }
125132 }
126133 }
127134
0 commit comments