https://kotlinlang.org logo
Title
d

doubov

05/05/2022, 6:57 PM
Curious if the following workflow is supported: 1. Setup a Flow<Model> (not a one-time action, but a stream that's dependent on some input) 2. Upon receiving new input, cancel the old Flow<Model> stream and restart it with the new input If
intents
were a Flow<Intent>:
intents.filterIsInstance<Intent.A>.flatMapLatest { intent -> repo.fetchModels(intent.input).map { data -> Msg.SetData(data) } }
Upon receiving
Intent.A
we cancel the previously setup
repo.fetchModels(...)
stream and restart it with the new input. As far as I can see, this isn't really doable with the current implementation, unless I've missed something. Thanks in advance! 🙏
a

Arkadii Ivanov

05/05/2022, 7:03 PM
It looks like you want, on new Intent, cancel any previous async job and start a new one. A typical use case is filtering a list while the user types the query. If this is what you need, then you can remember the
Job
returned by
launch
. On new Intent, cancel the previous Job if any, then
launch
and remember the new
Job
.
d

doubov

05/05/2022, 7:04 PM
👍 yeah, that's the solution that I ended up with, just wanted to see if there's a more idiomatic one. Thanks!