@@ -9,15 +9,15 @@ import io.rebble.cobble.shared.data.js.fromDevice
9
9
import io.rebble.cobble.shared.database.dao.LockerDao
10
10
import io.rebble.cobble.shared.domain.state.ConnectionStateManager
11
11
import io.rebble.cobble.shared.domain.state.watchOrNull
12
- import kotlinx.coroutines.CoroutineScope
12
+ import kotlinx.coroutines.*
13
13
import kotlinx.coroutines.flow.MutableSharedFlow
14
- import kotlinx.coroutines.launch
15
14
import kotlinx.serialization.encodeToString
16
15
import kotlinx.serialization.json.Json
17
16
import org.koin.core.component.KoinComponent
18
17
import org.koin.core.component.inject
18
+ import kotlin.time.Duration.Companion.seconds
19
19
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 {
21
21
private val lockerDao: LockerDao by inject()
22
22
23
23
@JavascriptInterface
@@ -118,10 +118,17 @@ class WebViewPrivatePKJSInterface(private val jsRunner: WebViewJsRunner, private
118
118
}
119
119
120
120
@JavascriptInterface
121
- fun sendAppMessageString (jsonAppMessage : String ) {
121
+ fun sendAppMessageString (jsonAppMessage : String ): Int {
122
122
Logging .v(" sendAppMessageString" )
123
- if (! outgoingAppMessages.tryEmit(jsonAppMessage)) {
123
+ val completable = CompletableDeferred <UByte >()
124
+ if (! outgoingAppMessages.tryEmit(Pair (completable, jsonAppMessage))) {
124
125
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
+ }
125
132
}
126
133
}
127
134
0 commit comments