Skip to content

Commit 8df90dd

Browse files
committed
Use try catch block
1 parent f117c3d commit 8df90dd

File tree

10 files changed

+61
-53
lines changed
  • firebase-app/src/commonMain/kotlin/dev/gitlive/firebase
  • firebase-auth/src
    • androidMain/kotlin/dev/gitlive/firebase/auth
    • iosMain/kotlin/dev/gitlive/firebase/auth
    • jsMain/kotlin/dev/gitlive/firebase/auth
  • firebase-database/src
    • androidMain/kotlin/dev/gitlive/firebase/database
    • iosMain/kotlin/dev/gitlive/firebase/database
    • jsMain/kotlin/dev/gitlive/firebase/database
  • firebase-firestore/src

10 files changed

+61
-53
lines changed

firebase-app/src/commonMain/kotlin/dev/gitlive/firebase/firebase.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
@file:JvmName("CommonKt")
77
package dev.gitlive.firebase
88

9+
import kotlinx.coroutines.channels.ClosedSendChannelException
10+
import kotlinx.coroutines.channels.SendChannel
911
import kotlin.jvm.JvmMultifileClass
1012
import kotlin.jvm.JvmName
1113

@@ -59,4 +61,10 @@ expect class FirebaseNetworkException : FirebaseException
5961

6062
expect open class FirebaseTooManyRequestsException : FirebaseException
6163

62-
expect open class FirebaseApiNotAvailableException : FirebaseException
64+
expect open class FirebaseApiNotAvailableException : FirebaseException
65+
66+
inline fun <E> SendChannel<E>.offerOrNull(element: E): Boolean? = try {
67+
offer(element)
68+
} catch (e : ClosedSendChannelException) {
69+
null
70+
}

firebase-auth/src/androidMain/kotlin/dev/gitlive/firebase/auth/auth.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.google.firebase.auth.ActionCodeResult.*
1111
import com.google.firebase.auth.FirebaseAuth.AuthStateListener
1212
import dev.gitlive.firebase.Firebase
1313
import dev.gitlive.firebase.FirebaseApp
14+
import dev.gitlive.firebase.offerOrNull
1415
import kotlinx.coroutines.channels.awaitClose
1516
import kotlinx.coroutines.flow.Flow
1617
import kotlinx.coroutines.flow.callbackFlow
@@ -26,15 +27,15 @@ actual class FirebaseAuth internal constructor(val android: com.google.firebase.
2627
actual val currentUser: FirebaseUser?
2728
get() = android.currentUser?.let { FirebaseUser(it) }
2829

29-
actual val authStateChanged get() = callbackFlow {
30-
val listener = AuthStateListener { auth -> if (!isClosedForSend) offer(auth.currentUser?.let { FirebaseUser(it) }) }
30+
actual val authStateChanged get() = callbackFlow<FirebaseUser?> {
31+
val listener = AuthStateListener { auth -> offerOrNull(auth.currentUser?.let { FirebaseUser(it) }) }
3132
android.addAuthStateListener(listener)
3233
awaitClose { android.removeAuthStateListener(listener) }
3334
}
3435

3536
actual val idTokenChanged: Flow<FirebaseUser?>
3637
get() = callbackFlow {
37-
val listener = com.google.firebase.auth.FirebaseAuth.IdTokenListener { auth -> if (!isClosedForSend) offer(auth.currentUser?.let { FirebaseUser(it) })}
38+
val listener = com.google.firebase.auth.FirebaseAuth.IdTokenListener { auth -> offerOrNull(auth.currentUser?.let { FirebaseUser(it) }) }
3839
android.addIdTokenListener(listener)
3940
awaitClose { android.removeIdTokenListener(listener) }
4041
}

firebase-auth/src/iosMain/kotlin/dev/gitlive/firebase/auth/auth.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dev.gitlive.firebase.Firebase
99
import dev.gitlive.firebase.FirebaseApp
1010
import dev.gitlive.firebase.FirebaseException
1111
import dev.gitlive.firebase.auth.ActionCodeResult.*
12+
import dev.gitlive.firebase.offerOrNull
1213
import kotlinx.cinterop.*
1314
import kotlinx.coroutines.CompletableDeferred
1415
import kotlinx.coroutines.channels.awaitClose
@@ -27,13 +28,13 @@ actual class FirebaseAuth internal constructor(val ios: FIRAuth) {
2728
actual val currentUser: FirebaseUser?
2829
get() = ios.currentUser?.let { FirebaseUser(it) }
2930

30-
actual val authStateChanged get() = callbackFlow {
31-
val handle = ios.addAuthStateDidChangeListener { _, user -> if (!isClosedForSend) offer(user?.let { FirebaseUser(it) }) }
31+
actual val authStateChanged get() = callbackFlow<FirebaseUser?> {
32+
val handle = ios.addAuthStateDidChangeListener { _, user -> offerOrNull(user?.let { FirebaseUser(it) }) }
3233
awaitClose { ios.removeAuthStateDidChangeListener(handle) }
3334
}
3435

35-
actual val idTokenChanged get() = callbackFlow {
36-
val handle = ios.addIDTokenDidChangeListener { _, user -> if (!isClosedForSend) offer(user?.let { FirebaseUser(it) }) }
36+
actual val idTokenChanged get() = callbackFlow<FirebaseUser?> {
37+
val handle = ios.addIDTokenDidChangeListener { _, user -> offerOrNull(user?.let { FirebaseUser(it) }) }
3738
awaitClose { ios.removeIDTokenDidChangeListener(handle) }
3839
}
3940

firebase-auth/src/jsMain/kotlin/dev/gitlive/firebase/auth/auth.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@ actual class FirebaseAuth internal constructor(val js: firebase.auth.Auth) {
2121
actual val currentUser: FirebaseUser?
2222
get() = rethrow { js.currentUser?.let { FirebaseUser(it) } }
2323

24-
actual val authStateChanged get() = callbackFlow {
24+
actual val authStateChanged get() = callbackFlow<FirebaseUser?> {
2525
val unsubscribe = js.onAuthStateChanged {
26-
if (!isClosedForSend)
27-
offer(it?.let { FirebaseUser(it) })
26+
offerOrNull(it?.let { FirebaseUser(it) })
2827
}
2928
awaitClose { unsubscribe() }
3029
}
3130

32-
actual val idTokenChanged get() = callbackFlow {
31+
actual val idTokenChanged get() = callbackFlow<FirebaseUser?> {
3332
val unsubscribe = js.onIdTokenChanged {
34-
if (!isClosedForSend)
35-
offer(it?.let { FirebaseUser(it) })
33+
offerOrNull(it?.let { FirebaseUser(it) })
3634
}
3735
awaitClose { unsubscribe() }
3836
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import dev.gitlive.firebase.Firebase
1313
import dev.gitlive.firebase.FirebaseApp
1414
import dev.gitlive.firebase.database.ChildEvent.Type
1515
import dev.gitlive.firebase.decode
16+
import dev.gitlive.firebase.offerOrNull
1617
import kotlinx.coroutines.FlowPreview
1718
import kotlinx.coroutines.channels.awaitClose
1819
import kotlinx.coroutines.coroutineScope
@@ -91,8 +92,7 @@ actual open class Query internal constructor(
9192
get() = callbackFlow {
9293
val listener = object : ValueEventListener {
9394
override fun onDataChange(snapshot: com.google.firebase.database.DataSnapshot) {
94-
if (!isClosedForSend)
95-
offer(DataSnapshot(snapshot))
95+
offerOrNull(DataSnapshot(snapshot))
9696
}
9797

9898
override fun onCancelled(error: com.google.firebase.database.DatabaseError) {
@@ -108,22 +108,22 @@ actual open class Query internal constructor(
108108

109109
val moved by lazy { types.contains(Type.MOVED) }
110110
override fun onChildMoved(snapshot: com.google.firebase.database.DataSnapshot, previousChildName: String?) {
111-
if(moved && !isClosedForSend) offer(ChildEvent(DataSnapshot(snapshot), Type.MOVED, previousChildName))
111+
if(moved) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.MOVED, previousChildName))
112112
}
113113

114114
val changed by lazy { types.contains(Type.CHANGED) }
115115
override fun onChildChanged(snapshot: com.google.firebase.database.DataSnapshot, previousChildName: String?) {
116-
if(changed && !isClosedForSend) offer(ChildEvent(DataSnapshot(snapshot), Type.CHANGED, previousChildName))
116+
if(changed) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.CHANGED, previousChildName))
117117
}
118118

119119
val added by lazy { types.contains(Type.ADDED) }
120120
override fun onChildAdded(snapshot: com.google.firebase.database.DataSnapshot, previousChildName: String?) {
121-
if(added && !isClosedForSend) offer(ChildEvent(DataSnapshot(snapshot), Type.ADDED, previousChildName))
121+
if(added) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.ADDED, previousChildName))
122122
}
123123

124124
val removed by lazy { types.contains(Type.REMOVED) }
125125
override fun onChildRemoved(snapshot: com.google.firebase.database.DataSnapshot) {
126-
if(removed && !isClosedForSend) offer(ChildEvent(DataSnapshot(snapshot), Type.REMOVED, null))
126+
if(removed) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.REMOVED, null))
127127
}
128128

129129
override fun onCancelled(error: com.google.firebase.database.DatabaseError) {

firebase-database/src/iosMain/kotlin/dev/gitlive/firebase/database/database.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import dev.gitlive.firebase.FirebaseApp
1111
import dev.gitlive.firebase.database.ChildEvent.Type
1212
import dev.gitlive.firebase.database.ChildEvent.Type.*
1313
import dev.gitlive.firebase.decode
14+
import dev.gitlive.firebase.offerOrNull
1415
import kotlinx.coroutines.CompletableDeferred
16+
import kotlinx.coroutines.channels.ClosedSendChannelException
1517
import kotlinx.coroutines.channels.awaitClose
1618
import kotlinx.coroutines.coroutineScope
1719
import kotlinx.coroutines.flow.callbackFlow
@@ -75,19 +77,23 @@ actual open class Query internal constructor(
7577

7678
actual fun startAt(value: Boolean, key: String?) = Query(ios.queryStartingAtValue(value, key), persistenceEnabled)
7779

78-
actual val valueEvents get() = callbackFlow {
80+
actual val valueEvents get() = callbackFlow<DataSnapshot> {
7981
val handle = ios.observeEventType(
8082
FIRDataEventTypeValue,
81-
withBlock = { if (!isClosedForSend) offer(DataSnapshot(it!!)) }
83+
withBlock = { snapShot ->
84+
offerOrNull(DataSnapshot(snapShot!!))
85+
}
8286
) { close(DatabaseException(it.toString())) }
8387
awaitClose { ios.removeObserverWithHandle(handle) }
8488
}
8589

86-
actual fun childEvents(vararg types: Type) = callbackFlow {
90+
actual fun childEvents(vararg types: Type) = callbackFlow<ChildEvent> {
8791
val handles = types.map { type ->
8892
ios.observeEventType(
8993
type.toEventType(),
90-
andPreviousSiblingKeyWithBlock = { it, key -> if (!isClosedForSend) offer(ChildEvent(DataSnapshot(it!!), type, key)) }
94+
andPreviousSiblingKeyWithBlock = { snapShot, key ->
95+
offerOrNull(ChildEvent(DataSnapshot(snapShot!!), type, key))
96+
}
9197
) { close(DatabaseException(it.toString())) }
9298
}
9399
awaitClose {

firebase-database/src/jsMain/kotlin/dev/gitlive/firebase/database/database.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,30 @@ actual open class Query internal constructor(open val js: firebase.database.Quer
4040
actual fun orderByKey() = Query(js.orderByKey())
4141
actual fun orderByChild(path: String) = Query(js.orderByChild(path))
4242

43-
actual val valueEvents get() = callbackFlow {
43+
actual val valueEvents get() = callbackFlow<DataSnapshot> {
4444
val listener = rethrow {
4545
js.on(
4646
"value",
47-
{ it, _ -> if (!isClosedForSend) offer(DataSnapshot(it)) },
47+
{ it, _ -> offerOrNull(DataSnapshot(it)) },
4848
{ close(DatabaseException(it)).run { Unit } }
4949
)
5050
}
5151
awaitClose { rethrow { js.off("value", listener) } }
5252
}
5353

54-
actual fun childEvents(vararg types: ChildEvent.Type) = callbackFlow {
54+
actual fun childEvents(vararg types: ChildEvent.Type) = callbackFlow<ChildEvent> {
5555
val listeners = rethrow {
5656
types.map { type ->
5757
"child_${type.name.toLowerCase()}".let { eventType ->
5858
eventType to js.on(
5959
eventType,
6060
{ snapshot, previousChildName ->
61-
if (!isClosedForSend) offer(
62-
ChildEvent(
63-
DataSnapshot(snapshot),
64-
type,
65-
previousChildName
61+
offerOrNull(
62+
ChildEvent(
63+
DataSnapshot(snapshot),
64+
type,
65+
previousChildName
66+
)
6667
)
6768
)
6869
},

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
package dev.gitlive.firebase.firestore
77

88
import com.google.firebase.firestore.SetOptions
9-
import dev.gitlive.firebase.Firebase
10-
import dev.gitlive.firebase.FirebaseApp
11-
import dev.gitlive.firebase.decode
12-
import dev.gitlive.firebase.encode
9+
import dev.gitlive.firebase.*
1310
import kotlinx.coroutines.channels.awaitClose
1411
import kotlinx.coroutines.flow.callbackFlow
1512
import kotlinx.coroutines.runBlocking
@@ -262,9 +259,9 @@ actual class DocumentReference(val android: com.google.firebase.firestore.Docume
262259
actual suspend fun get() =
263260
DocumentSnapshot(android.get().await())
264261

265-
actual val snapshots get() = callbackFlow {
262+
actual val snapshots get() = callbackFlow<DocumentSnapshot> {
266263
val listener = android.addSnapshotListener { snapshot, exception ->
267-
snapshot?.let { offer(DocumentSnapshot(snapshot)) }
264+
snapshot?.let { offerOrNull(DocumentSnapshot(snapshot)) }
268265
exception?.let { close(exception) }
269266
}
270267
awaitClose { listener.remove() }
@@ -277,9 +274,9 @@ actual open class Query(open val android: com.google.firebase.firestore.Query) {
277274

278275
actual fun limit(limit: Number) = Query(android.limit(limit.toLong()))
279276

280-
actual val snapshots get() = callbackFlow {
277+
actual val snapshots get() = callbackFlow<QuerySnapshot> {
281278
val listener = android.addSnapshotListener { snapshot, exception ->
282-
snapshot?.let { offer(QuerySnapshot(snapshot)) }
279+
snapshot?.let { offerOrNull(QuerySnapshot(snapshot)) }
283280
exception?.let { close(exception) }
284281
}
285282
awaitClose { listener.remove() }

firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package dev.gitlive.firebase.firestore
66

77
import cocoapods.FirebaseFirestore.*
8+
import dev.gitlive.firebase.*
89
import kotlinx.cinterop.*
910
import kotlinx.coroutines.CompletableDeferred
1011
import kotlinx.coroutines.channels.awaitClose
@@ -13,11 +14,6 @@ import kotlinx.coroutines.runBlocking
1314
import kotlinx.serialization.DeserializationStrategy
1415
import kotlinx.serialization.SerializationStrategy
1516
import platform.Foundation.NSError
16-
import dev.gitlive.firebase.Firebase
17-
import dev.gitlive.firebase.FirebaseApp
18-
import dev.gitlive.firebase.decode
19-
import dev.gitlive.firebase.encode
20-
import dev.gitlive.firebase.FirebaseException
2117

2218
actual val Firebase.firestore get() =
2319
FirebaseFirestore(FIRFirestore.firestore())
@@ -173,9 +169,9 @@ actual class DocumentReference(val ios: FIRDocumentReference) {
173169
actual suspend fun get() =
174170
DocumentSnapshot(awaitResult { ios.getDocumentWithCompletion(it) })
175171

176-
actual val snapshots get() = callbackFlow {
172+
actual val snapshots get() = callbackFlow<DocumentSnapshot> {
177173
val listener = ios.addSnapshotListener { snapshot, error ->
178-
snapshot?.let { offer(DocumentSnapshot(snapshot)) }
174+
snapshot?.let { offerOrNull(DocumentSnapshot(snapshot)) }
179175
error?.let { close(error.toException()) }
180176
}
181177
awaitClose { listener.remove() }
@@ -188,9 +184,9 @@ actual open class Query(open val ios: FIRQuery) {
188184

189185
actual fun limit(limit: Number) = Query(ios.queryLimitedTo(limit.toLong()))
190186

191-
actual val snapshots get() = callbackFlow {
187+
actual val snapshots get() = callbackFlow<QuerySnapshot> {
192188
val listener = ios.addSnapshotListener { snapshot, error ->
193-
snapshot?.let { offer(QuerySnapshot(snapshot)) }
189+
snapshot?.let { offerOrNull(QuerySnapshot(snapshot)) }
194190
error?.let { close(error.toException()) }
195191
}
196192
awaitClose { listener.remove() }

firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ actual class DocumentReference(val js: firebase.firestore.DocumentReference) {
236236

237237
actual suspend fun get() = rethrow { DocumentSnapshot(js.get().await()) }
238238

239-
actual val snapshots get() = callbackFlow {
239+
actual val snapshots get() = callbackFlow<DocumentSnapshot> {
240240
val unsubscribe = js.onSnapshot(
241-
{ if (!isClosedForSend) offer(DocumentSnapshot(it)) },
241+
{ offerOrNull(DocumentSnapshot(it)) },
242242
{ close(errorToException(it)) }
243243
)
244244
awaitClose { unsubscribe() }
@@ -286,10 +286,10 @@ actual open class Query(open val js: firebase.firestore.Query) {
286286
}
287287
)
288288

289-
actual val snapshots get() = callbackFlow {
289+
actual val snapshots get() = callbackFlow<QuerySnapshot> {
290290
val unsubscribe = rethrow {
291291
js.onSnapshot(
292-
{ if (!isClosedForSend) offer(QuerySnapshot(it)) },
292+
{ offerOrNull(QuerySnapshot(it)) },
293293
{ close(errorToException(it)) }
294294
)
295295
}

0 commit comments

Comments
 (0)