-
Notifications
You must be signed in to change notification settings - Fork 605
Add 'toFlow()' extensions to DocumentSnapshot and Query #1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
af0f469
Add toFlow extensions to DocumentSnapshot and Query
michgauz 931e73f
Add toFlow extensions to DocumentSnapshot and Query
michgauz 57fbe74
Update API Txt file
michgauz c87ea63
Implement 'com.google.android.gms:play-services-base' to retrieve Fir…
michgauz fe0916c
Add MetadataChanges optional parameter
michgauz 15bb8b9
Wrap 'offer()' in runCatching
michgauz fcfb4b1
Use 'flow' and 'Channel' instead of unstable 'callbackFlow'
michgauz ad9db4d
pull the version from @svenjacobs
martinbonnin 4d7237d
add "bufferCapacity" as a parameter
martinbonnin de32290
remove wildcard imports
martinbonnin cccf86f
use cancel(message, cause)
martinbonnin c91731a
make compile
martinbonnin a3d04ac
update public API
martinbonnin 31d1553
fix lint
martinbonnin 2e51a63
return non nullable snapshots
martinbonnin 288ae9a
toFlow -> snapshots
martinbonnin bcb69e5
remove wrong BuildConfig
martinbonnin f6e1aeb
add play-services-basement to ktx
martinbonnin fbd4864
Update firebase-firestore/ktx/src/main/kotlin/com/google/firebase/fir…
martinbonnin 31d3b61
Update firebase-firestore/ktx/src/main/kotlin/com/google/firebase/fir…
martinbonnin 8cd52cd
let the user decide the buffering
martinbonnin 253fbca
remove wildcard import
martinbonnin 692114c
make lint happy
martinbonnin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,8 +24,17 @@ import com.google.firebase.firestore.FirebaseFirestore | |||||
import com.google.firebase.firestore.FirebaseFirestoreSettings | ||||||
import com.google.firebase.firestore.QueryDocumentSnapshot | ||||||
import com.google.firebase.firestore.QuerySnapshot | ||||||
import com.google.firebase.firestore.DocumentReference | ||||||
import com.google.firebase.firestore.Query | ||||||
import com.google.firebase.firestore.MetadataChanges | ||||||
import com.google.firebase.firestore.util.Executors.BACKGROUND_EXECUTOR | ||||||
import com.google.firebase.ktx.Firebase | ||||||
import com.google.firebase.platforminfo.LibraryVersionComponent | ||||||
import kotlinx.coroutines.cancel | ||||||
import kotlinx.coroutines.channels.awaitClose | ||||||
import kotlinx.coroutines.channels.trySendBlocking | ||||||
import kotlinx.coroutines.flow.Flow | ||||||
import kotlinx.coroutines.flow.callbackFlow | ||||||
|
||||||
/** Returns the [FirebaseFirestore] instance of the default [FirebaseApp]. */ | ||||||
val Firebase.firestore: FirebaseFirestore | ||||||
|
@@ -162,3 +171,49 @@ class FirebaseFirestoreKtxRegistrar : ComponentRegistrar { | |||||
override fun getComponents(): List<Component<*>> = | ||||||
listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME)) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Starts listening to the document referenced by this `DocumentReference` with the given options and emits its values via a [Flow]. | ||||||
* | ||||||
* - When the returned flow starts being collected, an [EventListener] will be attached. | ||||||
* - When the flow completes, the listener will be removed. | ||||||
* | ||||||
* @param metadataChanges controls metadata-only changes. Default: [MetadataChanges.EXCLUDE] | ||||||
*/ | ||||||
fun DocumentReference.snapshots( | ||||||
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE, | ||||||
): Flow<DocumentSnapshot> { | ||||||
return callbackFlow { | ||||||
val registration = addSnapshotListener(BACKGROUND_EXECUTOR, metadataChanges) { snapshot, exception -> | ||||||
if (exception != null) { | ||||||
cancel(message = "Error getting DocumentReference snapshot", cause = exception) | ||||||
} else if (snapshot != null) { | ||||||
trySendBlocking(snapshot) | ||||||
} | ||||||
} | ||||||
awaitClose { registration.remove() } | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Starts listening to this query with the given options and emits its values via a [Flow]. | ||||||
* | ||||||
* - When the returned flow starts being collected, an [EventListener] will be attached. | ||||||
* - When the flow completes, the listener will be removed. | ||||||
* | ||||||
* @param metadataChanges controls metadata-only changes. Default: [MetadataChanges.EXCLUDE] | ||||||
*/ | ||||||
fun Query.snapshots( | ||||||
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
): Flow<QuerySnapshot> { | ||||||
return callbackFlow { | ||||||
val registration = addSnapshotListener(BACKGROUND_EXECUTOR, metadataChanges) { snapshot, exception -> | ||||||
if (exception != null) { | ||||||
cancel(message = "Error getting Query snapshot", cause = exception) | ||||||
} else if (snapshot != null) { | ||||||
trySendBlocking(snapshot) | ||||||
} | ||||||
} | ||||||
awaitClose { registration.remove() } | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, sorry, just realized that. It's fixed in 692114c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also interestingly this seemed to be a lint-only issue as Kotlin 1.4 is perfectly happy with the trailing coma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I think our version of ktlint is pretty ancient, we are planning to upgrade it eventually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vkryachko I think all comments have been addressed. Can you please approve the pending CI workflows and review the PR once again?