What are the Flux equivalents of debounce, throttleFirst, and throttleLast
Asked Answered
R

2

8

debounce, throttleFirst, and throttleLast are most conspicuously absent from Project Reactor's Flux. Do they have any counterparts?

Rimester answered 18/6, 2017 at 17:55 Comment(0)
S
11

The sample operators are the once relating to the behavior you're searching for.

sampleTimeout could be used as debounce.
sampleFirst could be used as throttleFirst.
sample could be used as throttleLast.

Shaikh answered 18/6, 2017 at 20:16 Comment(0)
L
1

I've been struggling to understand how to use sampleTimeout to do a debounce so I though I would put it here in case someone else is looking for this:

The would be equivalent to a debounce of 200ms

myFlux.sampleTimeout(u -> Mono.empty().delaySubscription(Duration.ofMillis(200)))
Lightness answered 18/3, 2021 at 11:15 Comment(3)
I'm trying to write a test showing a difference in behaviour between that and myFlux.sample(Duration.ofMillis(200)) and so far I haven't managed it - what do you think the difference is?Bigley
That would do a fixed sampling every 200ms. So a window would never last more than 200ms whereas with what I did the windows will last as long as you have elements being received less than 200ms apart from each others. The marbles in the documentation may help.Lightness
Thanks. OK, I want it to emit every window, just not more often, so sample(Duration) is fine for my use case.Bigley

© 2022 - 2024 — McMap. All rights reserved.