Reputation: 1855
In Observable, there are two methods called throttleLast and throttleLatest.
Marble diagrams are similar, but the two internal codes are different.
public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit) {
return sample(intervalDuration, unit);
}
public final Observable<T> sample(long period, TimeUnit unit, Scheduler scheduler, boolean emitLast) {
ObjectHelper.requireNonNull(unit, "unit is null");
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return RxJavaPlugins.onAssembly(new ObservableSampleTimed<T>(this, period, unit, scheduler, emitLast));
}
public final Observable<T> throttleLatest(long timeout, TimeUnit unit, Scheduler scheduler, boolean emitLast) {
ObjectHelper.requireNonNull(unit, "unit is null");
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return RxJavaPlugins.onAssembly(new ObservableThrottleLatest<T>(this, timeout, unit, scheduler, emitLast));
}
What difference between them?
Upvotes: 4
Views: 5112
Reputation: 4738
They are very similar. They both have a fixed time window (the comments above on that are wrong).
The only difference is how the first item is handled.
ThrottleLast
will NOT immediately emit the first item, it will always wait for the time window to pass and then emit the last value from that time window (if any)ThrottleLatest
will immediately emit the first item and after that it will behave the same as ThrottleLast.Upvotes: 4
Reputation: 1855
See comments of @Slaw and @akarnokd.
Not positive, but throttleLast might operate on fixed time intervals and throttleLatest resets the timeout whenever an item arrives. In other words, the difference between fixed rate and fixed delay.
And
It's in the javadoc: throttleLatest "If no items were emitted from the upstream during this timeout phase, the next upstream item is emitted immediately and the timeout window starts from then.". ThrottleLast emits at a fixed rate and if there is no item, nothing is emitted.
I did not understand well, so I tried to compare myself.
Upvotes: 6