gabin
04/20/2020, 5:40 PMoctylFractal
04/20/2020, 5:43 PMcollectLatest
is what you want?
* The crucial difference from [collect] is that when the original flow emits a new value, [action] block for previous
* value is cancelled.
Marc Knaup
04/20/2020, 5:43 PMmapLatest
and simply returning the passed value.Marc Knaup
04/20/2020, 5:48 PMonEach
and add `Latest`:
fun <T> Flow<T>.onEachLatest(action: suspend (T) -> Unit): Flow<T> = transformLatest { value ->
action(value)
emit(value)
}
gabin
04/20/2020, 5:48 PMgabin
04/20/2020, 5:50 PMgabin
04/20/2020, 5:51 PMMarc Knaup
04/20/2020, 5:52 PMupdateUI
can be safely canceled?
If you get too many events you could also use .conflate()
before .onEach()
gabin
04/20/2020, 5:52 PMMarc Knaup
04/20/2020, 5:52 PMgabin
04/20/2020, 5:52 PMgabin
04/20/2020, 5:53 PMMarc Knaup
04/20/2020, 5:53 PMMarc Knaup
04/20/2020, 5:53 PMdebounce
and sample
.Marc Knaup
04/20/2020, 5:53 PMdebounce
causes an initial delay, doesn’t it?Marc Knaup
04/20/2020, 5:54 PMsample
may be good for events but not for data 😅Marc Knaup
04/20/2020, 5:54 PMgabin
04/20/2020, 5:54 PMdebounce
takes the recent value in the time rangeMarc Knaup
04/20/2020, 5:54 PMMarc Knaup
04/20/2020, 5:55 PMgabin
04/20/2020, 5:55 PMMarc Knaup
04/20/2020, 5:55 PMMarc Knaup
04/20/2020, 5:55 PMgabin
04/20/2020, 6:03 PMcollectLatest{}
or single()
?Marc Knaup
04/20/2020, 6:06 PMgabin
04/20/2020, 6:07 PMMarc Knaup
04/20/2020, 6:08 PMthrottleLatest
Marc Knaup
04/20/2020, 6:09 PMMarc Knaup
04/20/2020, 6:33 PMMarc Knaup
04/20/2020, 6:37 PMonEachLatest
😉)bezrukov
04/20/2020, 7:24 PMmapLatest
as is (according to example you used)
events()
.mapLatest { event -> updateUI(event) }
.launchIn(scope)
gabin
04/20/2020, 7:33 PMbezrukov
04/20/2020, 7:36 PMbezrukov
04/20/2020, 7:38 PMonEachLatest(action: ..) = mapLatest(action)
gabin
04/20/2020, 7:50 PM