-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Flow: withLatestFrom operator #1498
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
Comments
@elizarov Is this the draft for the real implementation, or is PR #1315 going to be the real implementation and this is just equivalent for easier reading? Afaiu, there's a requirement in #1315 that Also, a question for my own education, if it's not too much trouble:
|
@ivanbartsov This implementation given above completes only when The reason for |
Right, that slipped my mind. Everything falls into place, thanks :) |
I think there's a slight difference in behavior with this implementation vs the one in #1315, which is that the latter will still start collecting both flows eagerly but it will apply backpressure to |
correct me if i'm wrong. This implementation would not support null in the second flow right? Because the default for the atomicReference is null |
I had need for this operator, so i just used your code, but then realised null not allowed for the second flow. I came up with this fix:
I think it will work and support for both flows to be null. Am i right? |
Is there a multi-platform version of this? It would be really useful. |
All Flow operators are multi-platform by default. |
@BugsBunnyBR ...that moment when your dreams come true. 👍 |
I meant to ask for a version of this operator that is not bound to JVM. |
I find this operator extremely useful for UI reactive programming. I have a document, which is simply a list of blocks representing different text fields in which user can type something. This document's state is composed from different data streams, including meta-data about this document along with ui-events like a specific block focusing. Only when document's structure is changed (i.e. new blocks added or deleted, etc.), it should be re-rendered. Given these steams:
I don't need |
@uburoiubu Can you, please, explain it in a little bit more detail. I really don't get how it is a case when |
@BugsBunnyBR In order to make this KMP, you can implement the AtomicFU library (https://github.com/Kotlin/kotlinx.atomicfu) or just use the ConflatedBroadcastChannel instead of the AtomicReference. The ConflatedBroadcastChannel uses AtomicFU in its internals. For example:
|
Seeing as #1315 seems to have stalled since 2019. Seeing as StateFlow has been released, is there anything blocking the implementation of |
@Progdrasil It would be extremely helpful if you drop a link here with some examples of your code that uses |
Unfortunately the code that uses val editTextList: List<TextInputEditText>
val textFlows: List<Flow<String>> = editTextList.map { it.textChanges() }
val submit: Button
submit
.clicks()
.withLatestFrom(textFlows) { _, data -> validate(data) }
.launchIn(lifecycleScope) Note its simplified for the actual validation and how its shown back to the user. Else we also have the following: val editTextList: List<TextInputEditText>
val validatedFlow: Flow<SomeDataClass> = editTextList
.map { it.textChanges() }
.combine { data -> parse(data) }
val submit: Button
submit
.clicks()
.withLatestFrom(validatedFlow) { _, data -> applyBusinessLogic(data) }
.launchIn(lifecycleScope) |
@Progdrasil Thanks a lot! What's the value of representing May I suggest you to consider an alternative approach to structure your code that seems simpler to me. Instead of
It is not using
You can do a similar simplification for the other snippet. Relace
Then you will be able to write simpler code there, too:
Does it help? |
Without getting into too much detail, The reason we have a list of flows is actually because its the end result of what we end up with after creating a bunch of Input fields in a recycler view. So what we end up with is actually a |
It's 2021 and I'm still coming back to this thread to get the |
@chris-hatton If you have something in your app that you take a snapshot of, then it might benefit from being modelled as a |
Thanks @elizarov. I feel like I should have realised that before - I've now refactored my code to use |
I have added it in https://github.com/hoc081098/FlowExt library for anyone who needs it |
Here is another version. Collection from Flow doesn't start until Flow emit's its first value. This has a dependency on Arrow Core for boxing the value from Flow.
|
This is great! I have a use case for this operator and I was surprised it is still not part of the official list. Is there anything holding it back? |
See #1315 for one of the use-cases. Note, that is operator can be implemented in a quite a straightforward way using stable
Flow
APIs. The simple implementation is given below:TODO: We need to figure out a name for this operation that is consistent with
combine
andxxxLatest
naming convention. Even better, it should be a part of the customizable "combine family" of operations where each source flow can either react to the the incoming values or just reference the most recent value. It is also related to #1354 and #1082 which would introduce the concept ofStateFlow
(a flow with the most recent value).The text was updated successfully, but these errors were encountered: