Skip to content

Commit 9fb0d0d

Browse files
authored
Merge pull request #94 from GitLiveApp/safeOffer
Safe offer
2 parents c4b45b0 + 76ba0ba commit 9fb0d0d

File tree

11 files changed

+42
-40
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-common/src/commonMain/kotlin/dev/gitlive/firebase
  • firebase-database/src
    • androidMain/kotlin/dev/gitlive/firebase/database
    • iosMain/kotlin/dev/gitlive/firebase/database
    • jsMain/kotlin/dev/gitlive/firebase/database
  • firebase-firestore/src

11 files changed

+42
-40
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
@file:JvmName("CommonKt")
77
package dev.gitlive.firebase
88

9-
import kotlinx.coroutines.CancellationException
10-
import kotlinx.coroutines.channels.ClosedSendChannelException
11-
import kotlinx.coroutines.channels.SendChannel
129
import kotlin.jvm.JvmMultifileClass
1310
import kotlin.jvm.JvmName
1411

@@ -64,10 +61,3 @@ expect open class FirebaseTooManyRequestsException : FirebaseException
6461

6562
expect open class FirebaseApiNotAvailableException : FirebaseException
6663

67-
inline fun <E> SendChannel<E>.offerOrNull(element: E): Boolean? = try {
68-
offer(element)
69-
} catch (e : ClosedSendChannelException) {
70-
null
71-
} catch (e : CancellationException) {
72-
null
73-
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +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
14+
import dev.gitlive.firebase.safeOffer
1515
import kotlinx.coroutines.channels.awaitClose
1616
import kotlinx.coroutines.flow.Flow
1717
import kotlinx.coroutines.flow.callbackFlow
@@ -28,14 +28,14 @@ actual class FirebaseAuth internal constructor(val android: com.google.firebase.
2828
get() = android.currentUser?.let { FirebaseUser(it) }
2929

3030
actual val authStateChanged get() = callbackFlow<FirebaseUser?> {
31-
val listener = AuthStateListener { auth -> offerOrNull(auth.currentUser?.let { FirebaseUser(it) }) }
31+
val listener = AuthStateListener { auth -> safeOffer(auth.currentUser?.let { FirebaseUser(it) }) }
3232
android.addAuthStateListener(listener)
3333
awaitClose { android.removeAuthStateListener(listener) }
3434
}
3535

3636
actual val idTokenChanged: Flow<FirebaseUser?>
3737
get() = callbackFlow {
38-
val listener = com.google.firebase.auth.FirebaseAuth.IdTokenListener { auth -> offerOrNull(auth.currentUser?.let { FirebaseUser(it) }) }
38+
val listener = com.google.firebase.auth.FirebaseAuth.IdTokenListener { auth -> safeOffer(auth.currentUser?.let { FirebaseUser(it) }) }
3939
android.addIdTokenListener(listener)
4040
awaitClose { android.removeIdTokenListener(listener) }
4141
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import cocoapods.FirebaseAuth.*
88
import dev.gitlive.firebase.Firebase
99
import dev.gitlive.firebase.FirebaseApp
1010
import dev.gitlive.firebase.FirebaseException
11-
import dev.gitlive.firebase.offerOrNull
11+
import dev.gitlive.firebase.safeOffer
1212
import dev.gitlive.firebase.auth.ActionCodeResult.*
1313
import kotlinx.cinterop.*
1414
import kotlinx.coroutines.CompletableDeferred
@@ -29,12 +29,12 @@ actual class FirebaseAuth internal constructor(val ios: FIRAuth) {
2929
get() = ios.currentUser?.let { FirebaseUser(it) }
3030

3131
actual val authStateChanged get() = callbackFlow<FirebaseUser?> {
32-
val handle = ios.addAuthStateDidChangeListener { _, user -> offerOrNull(user?.let { FirebaseUser(it) }) }
32+
val handle = ios.addAuthStateDidChangeListener { _, user -> safeOffer(user?.let { FirebaseUser(it) }) }
3333
awaitClose { ios.removeAuthStateDidChangeListener(handle) }
3434
}
3535

3636
actual val idTokenChanged get() = callbackFlow<FirebaseUser?> {
37-
val handle = ios.addIDTokenDidChangeListener { _, user -> offerOrNull(user?.let { FirebaseUser(it) }) }
37+
val handle = ios.addIDTokenDidChangeListener { _, user -> safeOffer(user?.let { FirebaseUser(it) }) }
3838
awaitClose { ios.removeIDTokenDidChangeListener(handle) }
3939
}
4040

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ actual class FirebaseAuth internal constructor(val js: firebase.auth.Auth) {
2323

2424
actual val authStateChanged get() = callbackFlow<FirebaseUser?> {
2525
val unsubscribe = js.onAuthStateChanged {
26-
offerOrNull(it?.let { FirebaseUser(it) })
26+
safeOffer(it?.let { FirebaseUser(it) })
2727
}
2828
awaitClose { unsubscribe() }
2929
}
3030

3131
actual val idTokenChanged get() = callbackFlow<FirebaseUser?> {
3232
val unsubscribe = js.onIdTokenChanged {
33-
offerOrNull(it?.let { FirebaseUser(it) })
33+
safeOffer(it?.let { FirebaseUser(it) })
3434
}
3535
awaitClose { unsubscribe() }
3636
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.gitlive.firebase
2+
3+
import kotlinx.coroutines.ExperimentalCoroutinesApi
4+
import kotlinx.coroutines.channels.ClosedSendChannelException
5+
import kotlinx.coroutines.channels.SendChannel
6+
7+
//workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/974
8+
@ExperimentalCoroutinesApi
9+
fun <E> SendChannel<E>.safeOffer(element: E) = try {
10+
!isClosedForSend && offer(element)
11+
} catch (e : ClosedSendChannelException) {
12+
false
13+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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
16+
import dev.gitlive.firebase.safeOffer
1717
import kotlinx.coroutines.FlowPreview
1818
import kotlinx.coroutines.channels.awaitClose
1919
import kotlinx.coroutines.coroutineScope
@@ -92,7 +92,7 @@ actual open class Query internal constructor(
9292
get() = callbackFlow {
9393
val listener = object : ValueEventListener {
9494
override fun onDataChange(snapshot: com.google.firebase.database.DataSnapshot) {
95-
offerOrNull(DataSnapshot(snapshot))
95+
safeOffer(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) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.MOVED, previousChildName))
111+
if(moved) safeOffer(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) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.CHANGED, previousChildName))
116+
if(changed) safeOffer(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) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.ADDED, previousChildName))
121+
if(added) safeOffer(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) offerOrNull(ChildEvent(DataSnapshot(snapshot), Type.REMOVED, null))
126+
if(removed) safeOffer(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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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
14+
import dev.gitlive.firebase.safeOffer
1515
import kotlinx.coroutines.CompletableDeferred
1616
import kotlinx.coroutines.channels.ClosedSendChannelException
1717
import kotlinx.coroutines.channels.awaitClose
@@ -81,7 +81,7 @@ actual open class Query internal constructor(
8181
val handle = ios.observeEventType(
8282
FIRDataEventTypeValue,
8383
withBlock = { snapShot ->
84-
offerOrNull(DataSnapshot(snapShot!!))
84+
safeOffer(DataSnapshot(snapShot!!))
8585
}
8686
) { close(DatabaseException(it.toString())) }
8787
awaitClose { ios.removeObserverWithHandle(handle) }
@@ -92,7 +92,7 @@ actual open class Query internal constructor(
9292
ios.observeEventType(
9393
type.toEventType(),
9494
andPreviousSiblingKeyWithBlock = { snapShot, key ->
95-
offerOrNull(ChildEvent(DataSnapshot(snapShot!!), type, key))
95+
safeOffer(ChildEvent(DataSnapshot(snapShot!!), type, key))
9696
}
9797
) { close(DatabaseException(it.toString())) }
9898
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ actual open class Query internal constructor(open val js: firebase.database.Quer
4444
val listener = rethrow {
4545
js.on(
4646
"value",
47-
{ it, _ -> offerOrNull(DataSnapshot(it)) },
47+
{ it, _ -> safeOffer(DataSnapshot(it)) },
4848
{ close(DatabaseException(it)).run { Unit } }
4949
)
5050
}
@@ -58,12 +58,11 @@ actual open class Query internal constructor(open val js: firebase.database.Quer
5858
eventType to js.on(
5959
eventType,
6060
{ snapshot, previousChildName ->
61-
offerOrNull(
62-
ChildEvent(
63-
DataSnapshot(snapshot),
64-
type,
65-
previousChildName
66-
)
61+
safeOffer(
62+
ChildEvent(
63+
DataSnapshot(snapshot),
64+
type,
65+
previousChildName
6766
)
6867
)
6968
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ actual class DocumentReference(val android: com.google.firebase.firestore.Docume
261261

262262
actual val snapshots get() = callbackFlow<DocumentSnapshot> {
263263
val listener = android.addSnapshotListener { snapshot, exception ->
264-
snapshot?.let { offerOrNull(DocumentSnapshot(snapshot)) }
264+
snapshot?.let { safeOffer(DocumentSnapshot(snapshot)) }
265265
exception?.let { close(exception) }
266266
}
267267
awaitClose { listener.remove() }
@@ -276,7 +276,7 @@ actual open class Query(open val android: com.google.firebase.firestore.Query) {
276276

277277
actual val snapshots get() = callbackFlow<QuerySnapshot> {
278278
val listener = android.addSnapshotListener { snapshot, exception ->
279-
snapshot?.let { offerOrNull(QuerySnapshot(snapshot)) }
279+
snapshot?.let { safeOffer(QuerySnapshot(snapshot)) }
280280
exception?.let { close(exception) }
281281
}
282282
awaitClose { listener.remove() }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ actual class DocumentReference(val ios: FIRDocumentReference) {
171171

172172
actual val snapshots get() = callbackFlow<DocumentSnapshot> {
173173
val listener = ios.addSnapshotListener { snapshot, error ->
174-
snapshot?.let { offerOrNull(DocumentSnapshot(snapshot)) }
174+
snapshot?.let { safeOffer(DocumentSnapshot(snapshot)) }
175175
error?.let { close(error.toException()) }
176176
}
177177
awaitClose { listener.remove() }
@@ -186,7 +186,7 @@ actual open class Query(open val ios: FIRQuery) {
186186

187187
actual val snapshots get() = callbackFlow<QuerySnapshot> {
188188
val listener = ios.addSnapshotListener { snapshot, error ->
189-
snapshot?.let { offerOrNull(QuerySnapshot(snapshot)) }
189+
snapshot?.let { safeOffer(QuerySnapshot(snapshot)) }
190190
error?.let { close(error.toException()) }
191191
}
192192
awaitClose { listener.remove() }

0 commit comments

Comments
 (0)