-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Decouple switching logic from switchMap operator and consistent naming #1335
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
To me, "Latest" as a suffix is more meaningful than "switch" as a prefix, particularly regarding the cancellation behavior. I'd still like to have That's only my take, I'm interested to know what you and others think about |
I agree with @LouisCAD, the I think the names |
Ok. So here is a take on the
This would also mean that we deprecate This nomenclature produces the following distinctly named variants of
Indeed, I like the suffix nomenclature more. I don't like introducing a strategy enum, though. It only makes subsequent dead-code elimination in Android via R8, in Kotiln/JS, and in Kotlin/Native harder. P.S. There is might be some confusion with |
I'm wondering if |
I would interpret This is a general issue that affects all operators that take suspend functions isn't it? Every such operator could have a cancelling and non-cancelling variant. A more scalable solution might be to use a dedicated operator: assume that suspending operators will not cancel by default, and introduce a The implementation would basically be the same as fun <T> Flow<T>.cancelOnNext(): Flow<T> = scopedFlow { downstream ->
var previousFlow: Job? = null
collect { value ->
previousFlow?.cancel(ChildCancelledException())
previousFlow?.join()
// Undispatched to have better user experience in case of synchronous flows
previousFlow = launch(start = CoroutineStart.UNDISPATCHED) {
downstream.emit(value)
}
}
} So then |
@LouisCAD That is indeed something to ponder about. It did not occur to me that I planned to have separate design discussion on the extraction of basic functionality that drive both (current) |
@zach-klippenstein Terrific. This (tentative)
The trick is how to name it. Note, that we don't have the concept of
|
Yes that name was terrible, didn't think too much about it. Maybe a bit verbose, but |
Just "switch"? "switchOver"? Conflate has a concrete meaning for me in coroutines, that is applied to upstream, not "sidestream". |
…nd to have a consistent and meaningful naming scheme for the rest of the 'latest' operators * Make flatMapLatest pure, do not leak cancellation behaviour to downstream * Make *latest buffered by default to amortize constant re-dispatch cost * Introducing transformLatest * Introducing mapLatest Fixes #1335
…nd to have a consistent and meaningful naming scheme for the rest of the 'latest' operators * Make flatMapLatest pure, do not leak cancellation behaviour to downstream * Make *latest buffered by default to amortize constant re-dispatch cost * Introducing transformLatest * Introducing mapLatest Fixes #1335
…nd to have a consistent and meaningful naming scheme for the rest of the 'latest' operators * Make flatMapLatest pure, do not leak cancellation behaviour to downstream * Make *latest buffered by default to amortize constant re-dispatch cost * Introducing transformLatest * Introducing mapLatest Fixes #1335
We currently have a
switchMap { ... }
operator that is similar to the corresponding swithMap operator from Rx but from the standpoint of Kotlin coroutines looks like an amalgam of different operations that might need to be decoupled. The most basic operation seems to be this one, tentatively named asswitchTransform
:It would be similar to
Flow.transform
but with a difference thattransform
waits for transformation to complete for each element whileswitchTransfrom
would cancel the ongoing transformation on new incoming element and start it again.Now, note that
transform
operator can be used to implement the host of basic operators:So, if we have a basic
switchTransform
operator, then it would be fitting to potentially have (at least reserve the names) the whole family ofswitchXxx
operators that are similar to the above above when you replacetransform
withswitchTransform
in their implementations.Moreover, there is a use case (see #1269) for the
collectLatest
terminal operator that should be calledswitchCollect
under this proposed nomenclature, because:However, this
switchXxx
nomenclature has the following problem.Under the proposed
switchXxx
nomenclatureswitchMap
operator shall have the same signature asmap
(operates onFlow<T>
) and should be equivalent toswitchTransform { emit(block(it)) }
. But we already have experimentalswitchMap
operator that operates onFlow<Flow<T>>
and doesswitchTransform { emitAll(block(it)) }
which under the new nomenclature should have been calledswitchFlattenMapConcat
. It is not clear how to proceed.The text was updated successfully, but these errors were encountered: