1
1
package dev.teamhub.firebase.database
2
2
3
+ import com.google.android.gms.tasks.Task
3
4
import com.google.firebase.database.*
4
5
import com.google.firebase.database.DataSnapshot
5
6
import com.google.firebase.database.DatabaseError
@@ -9,11 +10,34 @@ import com.google.firebase.database.Exclude
9
10
import com.google.firebase.database.FirebaseDatabase
10
11
import com.google.firebase.database.IgnoreExtraProperties
11
12
import com.google.firebase.database.OnDisconnect
12
- import com.google.firebase.database.ValueEventListener
13
- import kotlinx.coroutines.tasks.await
13
+ import kotlinx.coroutines.CompletableDeferred
14
+ import kotlinx.coroutines.selects.select
15
+ import kotlinx.coroutines.tasks.asDeferred
14
16
15
17
import kotlin.reflect.KClass
16
18
19
+ suspend fun <T > Task<T>.awaitWhileOnline (): T {
20
+ val notConnected = CompletableDeferred <Unit >()
21
+ val reference = getFirebaseDatabase().getReference(" .info/connected" )
22
+ val listener = reference.addValueEventListener(object : ValueEventListener {
23
+ override fun onCancelled (error : DatabaseError ) {
24
+ notConnected.completeExceptionally(error.toException())
25
+ }
26
+ override fun onDataChange (snapshot : DataSnapshot ) {
27
+ if (snapshot.value == false ) notConnected.complete(Unit )
28
+ }
29
+ })
30
+
31
+ try {
32
+ return select {
33
+ asDeferred().onAwait { it }
34
+ notConnected.onAwait { throw DatabaseException (" Database not connected" ) }
35
+ }
36
+ } finally {
37
+ reference.removeEventListener(listener)
38
+ }
39
+ }
40
+
17
41
actual fun getFirebaseDatabase () = FirebaseDatabase .getInstance()
18
42
19
43
actual typealias LoggerLevel = Logger .Level
@@ -22,11 +46,11 @@ actual typealias FirebaseDatabase = FirebaseDatabase
22
46
23
47
actual typealias DatabaseReference = DatabaseReference
24
48
25
- actual suspend fun DatabaseReference.awaitSetValue (value : Any? ) = setValue(value).await ().run { Unit }
49
+ actual suspend fun DatabaseReference.awaitSetValue (value : Any? ) = setValue(value).awaitWhileOnline ().run { Unit }
26
50
27
- actual suspend fun DatabaseReference.awaitUpdateChildren (update : Map <String , Any ?>) = updateChildren(update).await ().run { Unit }
51
+ actual suspend fun DatabaseReference.awaitUpdateChildren (update : Map <String , Any ?>) = updateChildren(update).awaitWhileOnline ().run { Unit }
28
52
29
- actual typealias ValueEventListener = ValueEventListener
53
+ actual typealias ValueEventListener = com.google.firebase.database. ValueEventListener
30
54
31
55
actual typealias DataSnapshot = DataSnapshot
32
56
@@ -52,17 +76,17 @@ actual typealias DatabaseError = DatabaseError
52
76
53
77
actual typealias OnDisconnect = OnDisconnect
54
78
55
- actual suspend fun OnDisconnect.awaitRemoveValue () = removeValue().await ().run { Unit }
79
+ actual suspend fun OnDisconnect.awaitRemoveValue () = removeValue().awaitWhileOnline ().run { Unit }
56
80
57
- actual suspend fun OnDisconnect.awaitCancel () = cancel().await ().run { Unit }
81
+ actual suspend fun OnDisconnect.awaitCancel () = cancel().awaitWhileOnline ().run { Unit }
58
82
59
- actual suspend fun OnDisconnect.awaitSetValue (value : Any? ) = setValue(value).await ().run { Unit }
83
+ actual suspend fun OnDisconnect.awaitSetValue (value : Any? ) = setValue(value).awaitWhileOnline ().run { Unit }
60
84
61
- actual suspend fun OnDisconnect.awaitUpdateChildren (update : Map <String , Any ?>) = updateChildren(update).await ().run { Unit }
85
+ actual suspend fun OnDisconnect.awaitUpdateChildren (update : Map <String , Any ?>) = updateChildren(update).awaitWhileOnline ().run { Unit }
62
86
63
87
actual val TIMESTAMP = ServerValue .TIMESTAMP
64
88
65
- actual suspend fun DatabaseReference.awaitRemoveValue () = removeValue().await ().run { Unit }
89
+ actual suspend fun DatabaseReference.awaitRemoveValue () = removeValue().awaitWhileOnline ().run { Unit }
66
90
67
91
actual fun FirebaseDatabase.getReference (path : String ) = getReference(path)
68
92
0 commit comments