I'm wondering if there's a clean way to implement an observable which filters out any events that occur within a time window after the most recent emitted event?
I currently have this:
source.timeInterval(TimeUnit.MILLISECONDS)
.filter(new Predicate<Timed<Object>>() {
final long TIME_LIMIT = 10 * 1000;
long totalTime = 0;
@Override
public boolean test(@NonNull Timed<Object> objectTimed) throws Exception {
totalTime += objectTimed.time();
if (totalTime > TIME_LIMIT) {
totalTime = 0;
return true;
}
return false;
}
})
.subscribe(objectTimed -> {
doSomething(objectTimed)
});
This technically does the trick, but requires a bit of extra state in the filter that's a bit ugly and prevents me from using a lambda. Instead, I'd like to see if there's a way to compose observables that would do the same thing.
Seems like your desire behavior is exactly throttleFirst()
operator:
it will emit only the first item at each window of time.