-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Flow.materialize/dematerialize operators #2092
Comments
My 2c on why this should be given higher priority. There are real-world use cases for dividing up a Flow using filters and processing them separately. Take, for instance, reading a large log file. You'd like to handle INFO/WARN/DEBUG logs separately so you share the flow and filter individually. However, this code never terminates:
I'm using count here as an example(which could be achieved in other ways), it will be much more complex in practice. This was possible a while back using So as I see it, you have 4 options at the moment:
As far as I understand, this is what the original shareIn proposal was about #1261 In terms of materialize/dematerialize, I find it a bad API. Here are my reasons:
I would propose that SharedFlow be materialized by default. Or at least add some parameter to |
@eduanb This was discussed multiple times and the answer always was (example) that |
@pacher Thank you for sharing. After reading more from that post, it seems like Side note, a |
Just in case anyone actually needs materialize/dematerialize (with all the caveats that usually you it means you are looking at your problem from the wrong perspective), they are extremely easy to implement in your own code: sealed class ValueOrCompletion<out T> {
data class Value<out T>(val value: T) : ValueOrCompletion<T>()
data class Completion(val exception: Throwable?) : ValueOrCompletion<Nothing>()
}
fun <T> Flow<T>.materializeCompletion(): Flow<ValueOrCompletion<T>> = flow {
val result = runCatching {
collect { it -> emit(ValueOrCompletion.Value(it)) }
}
emit(ValueOrCompletion.Completion(result.exceptionOrNull()))
}
fun <T> Flow<ValueOrCompletion<T>>.dematerializeCompletion(): Flow<T> = transformWhile { vc ->
when(vc) {
is ValueOrCompletion.Value -> {
emit(vc.value)
true
}
is ValueOrCompletion.Completion -> {
vc.exception?.let { throw it }
false
}
}
} |
This is a stub issue to introduce
Flow.materialize
andFlow.dematerialize
operators that would materilize flow completion (both normal completion and error) and will be primarily designed to integrate with sharing operators (see #2047). The detailed design is TBD.The text was updated successfully, but these errors were encountered: