File tree 1 file changed +11
-7
lines changed
android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util
1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change 1
1
package net.mullvad.mullvadvpn.lib.daemon.grpc.util
2
2
3
+ import co.touchlab.kermit.Logger
3
4
import io.grpc.ConnectivityState
4
5
import io.grpc.ManagedChannel
5
- import kotlin.coroutines.resume
6
- import kotlin.coroutines.suspendCoroutine
6
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
7
7
import kotlinx.coroutines.flow.Flow
8
8
import kotlinx.coroutines.flow.callbackFlow
9
9
import kotlinx.coroutines.isActive
10
+ import kotlinx.coroutines.suspendCancellableCoroutine
10
11
12
+ @OptIn(ExperimentalCoroutinesApi ::class )
11
13
internal fun ManagedChannel.connectivityFlow (): Flow <ConnectivityState > {
12
14
return callbackFlow {
13
15
var currentState = getState(false )
14
- send(currentState)
15
16
16
17
while (isActive) {
17
- currentState =
18
- suspendCoroutine<ConnectivityState > {
19
- notifyWhenStateChanged(currentState) { it.resume(getState(false )) }
20
- }
18
+ // Check that we are active before sending
21
19
send(currentState)
20
+ currentState = suspendCancellableCoroutine {
21
+ notifyWhenStateChanged(currentState) {
22
+ // If we are cancelled we will just log
23
+ it.resume(getState(false )) { Logger .w(" Resume while cancelled" , it) }
24
+ }
25
+ }
22
26
}
23
27
}
24
28
}
You can’t perform that action at this time.
0 commit comments