2

I'm using the .debounce operator in rxjava/rxscala to capture some events that take place within a given time period of each other even, but would like to make the given time period controllable. The time period .debounce uses is given as arguments.

Ideally, I'd like to be able to pass the .debounce operator an observable that it uses the latest value of to determine the time period to debounce using. Something like .debounce(timePeriodController, TimeUnit.Seconds).

I've seen that .debounce can take a DebounceSelector, and think that this may be the solution.

I've also seen that .flatMap can be used in quite complex ways in this kind of circumstance.

Edit: It appears that in RxJS, .debounce can take a durationSelector, but that this is not currently possible in RxJava. I wonder if there may be a workaround?

I'd appreciate any help. Cheers! Adam

1 Answers1

1

When needing to vary parameters to a timer operator, such as debounce, I use the switchMap() operator.

BehaviorSubject<Long> timer = BehaviorSubject.create(100);
...
timer
  .distinctUntilChanged()
  .switchMap( timerValue ->
    originalSource
      .debounce(timerValue))
  .subscribe(...);

Whenever the timer value changes, the debounce interval will change.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42