rx-javarx-android

Deliver the first item immediately, 'debounce' following items


Consider the following use case:

I ended up implementing custom operator based on OperatorDebounceWithTime then using it like this

.lift(new CustomOperatorDebounceWithTime<>(1, TimeUnit.SECONDS, Schedulers.computation()))

CustomOperatorDebounceWithTime delivers the first item immediately then uses OperatorDebounceWithTime operator's logic to debounce later items.

Is there an easier way to achieve described behavior? Let's skip the compose operator, it doesn't solve the problem. I'm looking for a way to achieve this without implementing custom operators.


Solution

  • Update:
    From @lopar's comments a better way would be:

    Observable.from(items).publish(publishedItems -> publishedItems.limit(1).concatWith(publishedItems.skip(1).debounce(1, TimeUnit.SECONDS)))
    

    Would something like this work:

    String[] items = {"one", "two", "three", "four", "five"};
    Observable<String> myObservable = Observable.from(items);
    Observable.concat(
      myObservable.first(), 
      myObservable.skip(1).debounce(1, TimeUnit.SECONDS)
    ).subscribe(s -> 
      System.out.println(s));