Skip to content

Commit

Permalink
Merge pull request #281 from GitLiveApp/includeMetadataChanges
Browse files Browse the repository at this point in the history
add includeMetadataChanges support to firestore Query.snapshots
  • Loading branch information
nbransby authored Apr 5, 2022
2 parents 96ccf89 + 1de87d3 commit d413cb6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ external object firebase {
fun where(field: String, opStr: String, value: Any?): Query
fun where(field: FieldPath, opStr: String, value: Any?): Query
fun onSnapshot(next: (snapshot: QuerySnapshot) -> Unit, error: (error: Error) -> Unit): () -> Unit
fun onSnapshot(options: Json, next: (snapshot: QuerySnapshot) -> Unit, error: (error: Error) -> Unit): () -> Unit
fun limit(limit: Double): Query
fun orderBy(field: String, direction: Any): Query
fun orderBy(field: FieldPath, direction: Any): Query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package dev.gitlive.firebase.firestore

import com.google.firebase.Timestamp
import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.MetadataChanges
import com.google.firebase.firestore.SetOptions
import dev.gitlive.firebase.*
import kotlinx.coroutines.channels.awaitClose
Expand Down Expand Up @@ -321,6 +322,15 @@ actual open class Query(open val android: com.google.firebase.firestore.Query) {
awaitClose { listener.remove() }
}

actual fun snapshots(includeMetadataChanges: Boolean) = callbackFlow<QuerySnapshot> {
val metadataChanges = if(includeMetadataChanges) MetadataChanges.INCLUDE else MetadataChanges.EXCLUDE
val listener = android.addSnapshotListener(metadataChanges) { snapshot, exception ->
snapshot?.let { safeOffer(QuerySnapshot(snapshot)) }
exception?.let { close(exception) }
}
awaitClose { listener.remove() }
}

internal actual fun _where(field: String, equalTo: Any?) = Query(android.whereEqualTo(field, equalTo))
internal actual fun _where(path: FieldPath, equalTo: Any?) = Query(android.whereEqualTo(path.android, equalTo))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ expect class Transaction {
expect open class Query {
fun limit(limit: Number): Query
val snapshots: Flow<QuerySnapshot>
fun snapshots(includeMetadataChanges: Boolean = false): Flow<QuerySnapshot>
suspend fun get(): QuerySnapshot
internal fun _where(field: String, equalTo: Any?): Query
internal fun _where(path: FieldPath, equalTo: Any?): Query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ actual open class Query(open val ios: FIRQuery) {
awaitClose { listener.remove() }
}

actual fun snapshots(includeMetadataChanges: Boolean) = callbackFlow<QuerySnapshot> {
val listener = ios.addSnapshotListenerWithIncludeMetadataChanges(includeMetadataChanges) { snapshot, error ->
snapshot?.let { safeOffer(QuerySnapshot(snapshot)) }
error?.let { close(error.toException()) }
}
awaitClose { listener.remove() }
}

internal actual fun _where(field: String, equalTo: Any?) = Query(ios.queryWhereField(field, isEqualTo = equalTo!!))
internal actual fun _where(path: FieldPath, equalTo: Any?) = Query(ios.queryWhereFieldPath(path.ios, isEqualTo = equalTo!!))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ actual open class Query(open val js: firebase.firestore.Query) {
}
awaitClose { rethrow { unsubscribe() } }
}

actual fun snapshots(includeMetadataChanges: Boolean) = callbackFlow<QuerySnapshot> {
val unsubscribe = rethrow {
js.onSnapshot(
json("includeMetadataChanges" to includeMetadataChanges),
{ safeOffer(QuerySnapshot(it)) },
{ close(errorToException(it)) }
)
}
awaitClose { rethrow { unsubscribe() } }
}
}

actual class CollectionReference(override val js: firebase.firestore.CollectionReference) : Query(js) {
Expand Down

0 comments on commit d413cb6

Please sign in to comment.