5
5
package dev.gitlive.firebase.database
6
6
7
7
import com.google.android.gms.tasks.Task
8
- import com.google.firebase.database.ChildEventListener
9
- import com.google.firebase.database.DatabaseError
10
- import com.google.firebase.database.Logger
11
- import com.google.firebase.database.MutableData
12
- import com.google.firebase.database.Transaction
13
- import com.google.firebase.database.ValueEventListener
14
- import dev.gitlive.firebase.encode
8
+ import com.google.firebase.database.*
15
9
import dev.gitlive.firebase.Firebase
16
10
import dev.gitlive.firebase.FirebaseApp
17
11
import dev.gitlive.firebase.database.ChildEvent.Type
18
- import dev.gitlive.firebase.decode
19
12
import dev.gitlive.firebase.database.FirebaseDatabase.Companion.FirebaseDatabase
20
- import kotlinx.coroutines.CancellationException
13
+ import dev.gitlive.firebase.decode
14
+ import dev.gitlive.firebase.encode
15
+ import kotlinx.coroutines.CompletableDeferred
21
16
import kotlinx.coroutines.channels.awaitClose
17
+ import kotlinx.coroutines.channels.trySendBlocking
22
18
import kotlinx.coroutines.coroutineScope
19
+ import kotlinx.coroutines.flow.Flow
20
+ import kotlinx.coroutines.flow.callbackFlow
21
+ import kotlinx.coroutines.flow.filter
22
+ import kotlinx.coroutines.flow.produceIn
23
23
import kotlinx.coroutines.selects.select
24
24
import kotlinx.coroutines.tasks.asDeferred
25
25
import kotlinx.coroutines.tasks.await
26
- import kotlinx.coroutines.CompletableDeferred
27
- import kotlinx.coroutines.cancel
28
- import kotlinx.coroutines.channels.trySendBlocking
29
- import kotlinx.coroutines.flow.*
30
26
import kotlinx.serialization.DeserializationStrategy
31
27
import kotlinx.serialization.KSerializer
32
28
import kotlinx.serialization.SerializationStrategy
33
- import java.util.WeakHashMap
29
+ import java.util.*
34
30
35
31
suspend fun <T > Task<T>.awaitWhileOnline (): T = coroutineScope {
36
32
@@ -40,9 +36,13 @@ suspend fun <T> Task<T>.awaitWhileOnline(): T = coroutineScope {
40
36
.filter { ! it.value<Boolean >() }
41
37
.produceIn(this )
42
38
43
- select<T > {
44
- asDeferred().onAwait { it.also { notConnected.cancel() } }
45
- notConnected.onReceive { throw DatabaseException (" Database not connected" , null ) }
39
+ try {
40
+ select<T > {
41
+ asDeferred().onAwait { it }
42
+ notConnected.onReceive { throw DatabaseException (" Database not connected" , null ) }
43
+ }
44
+ } finally {
45
+ notConnected.cancel()
46
46
}
47
47
}
48
48
0 commit comments