55package dev.gitlive.firebase.database
66
77import 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.*
159import dev.gitlive.firebase.Firebase
1610import dev.gitlive.firebase.FirebaseApp
1711import dev.gitlive.firebase.database.ChildEvent.Type
18- import dev.gitlive.firebase.decode
1912import 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
2116import kotlinx.coroutines.channels.awaitClose
17+ import kotlinx.coroutines.channels.trySendBlocking
2218import 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
2323import kotlinx.coroutines.selects.select
2424import kotlinx.coroutines.tasks.asDeferred
2525import 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.*
3026import kotlinx.serialization.DeserializationStrategy
3127import kotlinx.serialization.KSerializer
3228import kotlinx.serialization.SerializationStrategy
33- import java.util.WeakHashMap
29+ import java.util.*
3430
3531suspend fun <T > Task<T>.awaitWhileOnline (): T = coroutineScope {
3632
@@ -40,9 +36,13 @@ suspend fun <T> Task<T>.awaitWhileOnline(): T = coroutineScope {
4036 .filter { ! it.value<Boolean >() }
4137 .produceIn(this )
4238
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()
4646 }
4747}
4848
0 commit comments