Here is the code that seems to work for the debut buffer:
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;
public class DebounceBuffer {
public static void main(String args[]) {
Observable<Integer> burstStream = intermittentBursts().publish().refCount();
Observable<Integer> debounced = burstStream.debounce(10, TimeUnit.MILLISECONDS);
Observable<List<Integer>> buffered = burstStream.buffer(debounced);
buffered.take(20).toBlocking().forEach(System.out::println);
}
public static Observable<Integer> intermittentBursts() {
return Observable.create((Subscriber<? super Integer> s) -> {
while (!s.isUnsubscribed()) {
for (int i = 0; i < Math.random() * 20; i++) {
s.onNext(i);
}
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (Exception e) {
}
}
}).subscribeOn(Schedulers.newThread());
}
}
It emits the following:
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1]
[0, 1, 2, 3, 4, 5]
[0, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3]
[0]
[0, 1, 2]
[0]