Skip to content

Commit fc3f0c7

Browse files
committed
fix awaitWhileOnline
1 parent 467908b commit fc3f0c7

File tree

1 file changed

+10
-6
lines changed
  • firebase-database/src/androidMain/kotlin/dev/teamhub/firebase/database

1 file changed

+10
-6
lines changed

firebase-database/src/androidMain/kotlin/dev/teamhub/firebase/database/database.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import dev.teamhub.firebase.encode
1111
import kotlinx.coroutines.channels.awaitClose
1212
import kotlinx.coroutines.coroutineScope
1313
import kotlinx.coroutines.flow.callbackFlow
14-
import kotlinx.coroutines.flow.conflate
1514
import kotlinx.coroutines.flow.filter
1615
import kotlinx.coroutines.flow.produceIn
1716
import kotlinx.coroutines.selects.select
@@ -21,21 +20,26 @@ import kotlinx.serialization.DeserializationStrategy
2120
import kotlinx.serialization.SerializationStrategy
2221

2322
suspend fun <T> Task<T>.awaitWhileOnline(): T = coroutineScope {
23+
2424
val notConnected = Firebase.database
2525
.reference(".info/connected")
2626
.snapshots
2727
.filter { !it.value<Boolean>() }
28-
.conflate()
2928
.produceIn(this)
3029

31-
select<T> {
32-
asDeferred().onAwait { it }
33-
notConnected.onReceive { throw DatabaseException("Database not connected") }
30+
try {
31+
select<T> {
32+
asDeferred().onAwait { it }
33+
notConnected.onReceive { throw DatabaseException("Database not connected") }
34+
}
35+
} finally {
36+
notConnected.cancel()
3437
}
38+
3539
}
3640

3741
actual val Firebase.database
38-
get() = FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance())
42+
by lazy { FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance()) }
3943

4044
actual fun Firebase.database(url: String) =
4145
FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance(url))

0 commit comments

Comments
 (0)