-
Notifications
You must be signed in to change notification settings - Fork 602
dataconnect: Improve usage of MutableStateFlow to improve readability #6840
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
dataconnect: Improve usage of MutableStateFlow to improve readability #6840
Conversation
…f AtomicReference
…DataConnect.awaitAuthReady() and FirebaseDataConnect.awaitAppCheckReady()
…w.compareAndSet() directly for improved readability and less potential for bugs
Coverage Report 1Affected Products
Test Logs |
Test Results 66 files - 968 66 suites - 968 1m 19s ⏱️ - 33m 16s Results for commit b1f01b0. ± Comparison against base commit e98dd2c. This pull request removes 5322 tests.
♻️ This comment has been updated with latest results. |
Size Report 1Affected Products
Test Logs |
…aconnect/MutableStateFlowUseUpdateInsteadOfCompareAndSet
…dateInsteadOfCompareAndSet
…dateInsteadOfCompareAndSet
…teInsteadOfCompareAndSet
Vertex AI Mock Responses Check
|
…teInsteadOfCompareAndSet
…teInsteadOfCompareAndSet
📝 PRs merging into main branchOur main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released. |
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.
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectCredentialsTokenManager.kt:196
- [nitpick] Consider renaming the lambda parameter 'oldState' to 'currentState' in the forceRefresh function for improved clarity, as it represents the prior state before the update.
state.getAndUpdate { oldState ->
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectCredentialsTokenManager.kt:343
- [nitpick] Consider renaming the lambda parameter 'oldState' to 'currentState' in the onProviderAvailable function for consistency and clearer indication that it represents the pre-update state.
state.getAndUpdate { oldState ->
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.
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectCredentialsTokenManager.kt:195
- In forceRefresh(), the previous implementation checked that the new state had forceTokenRefresh set to true. Please verify that the removal of this explicit check in the getAndUpdate update block does not allow for unexpected state transitions.
val oldState = state.getAndUpdate { currentState ->
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectCredentialsTokenManager.kt:342
- In onProviderAvailable(), the CAS loop is replaced with a getAndUpdate call that always returns the old state. Confirm that this change still satisfies the intended concurrency and state transition guarantees.
val oldState = state.getAndUpdate { currentState ->
…efresh() to avoid unintentional internal state corruption. This was suggested by Copilot: #6840 (review)
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.
Pull Request Overview
This PR enhances readability and reduces the potential for bugs by replacing explicit CAS loops with the more concise and expressive MutableStateFlow update helpers. Key changes include:
- Replacing while loops using compareAndSet with update(), updateAndGet(), and getAndUpdate().
- Simplifying asynchronous state management in the close job and token management flows.
- Improving clarity of state transitions in multiple components.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/SuspendingCountDownLatch.kt | Replaced CAS loop with update() for decrementing the count. |
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/querymgr/RegisteredDataDeserialzer.kt | Replaced CAS loop with update() for atomically setting the latest update. |
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/QuerySubscriptionImpl.kt | Replaced CAS loop with update() to safely update the last result. |
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/FirebaseDataConnectImpl.kt | Replaced CAS loop with updateAndGet() to manage the close job state more robustly. |
firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectCredentialsTokenManager.kt | Replaced CAS loops with getAndUpdate() to handle state transitions during close, force refresh, and token provider updates. |
Use MutableStateFlow.update() instead of MutableStateFlow.compareAndSet() directly for improved readability and less potential for bugs