-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Operators: Throttle and Debounce #368
Merged
benjchristensen
merged 17 commits into
ReactiveX:master
from
benjchristensen:throttle-and-debounce
Sep 11, 2013
+840
−7
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2ea065c
Created and wired an implementation for the throttle operation on Obs…
michaeldejong 622d861
Ensure static star imports are used for test cases.
michaeldejong 77ac15b
No longer using Notification<T> for scheduling throttled events.
michaeldejong 02ee6fa
Cleaned up imports and removed unnecessary final keywords in the Oper…
michaeldejong 2519ef8
Fixed a typo the UnitTest class of OperationThrottle.
michaeldejong 87e766d
Merge branch 'operation-throttle' of git://github.com/michaeldejong/R…
benjchristensen 4c0c4db
Operator: throttleWithTimeout
benjchristensen 2e625a6
Operator: throttleFirst
benjchristensen b75b37c
Update javadoc for throttleLast
benjchristensen 2a3ade2
Update javadoc for throttleWithTimeout
benjchristensen 1c47b0c
Merge branch 'operation-throttle' of git://github.com/michaeldejong/R…
benjchristensen c95b810
Merge branch 'throttleWithTimeout' into throttle
benjchristensen 57f9aa4
Merge branch 'throttleLast' into throttle
benjchristensen d0e50fe
Merge branch 'throttleFirst' into throttle
benjchristensen 78cecdd
Operators: throttleWithTimeout, throttleFirst, throttleLast
benjchristensen 5fabd58
Use 'debounce' as proper name for ThrottleWithTimeout which unfortuna…
benjchristensen 5e7edd2
Javadoc with images
benjchristensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,11 @@ | |
*/ | ||
package rx; | ||
|
||
import static rx.util.functions.Functions.not; | ||
import static rx.util.functions.Functions.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
@@ -64,6 +63,8 @@ | |
import rx.operators.OperationTakeLast; | ||
import rx.operators.OperationTakeUntil; | ||
import rx.operators.OperationTakeWhile; | ||
import rx.operators.OperationThrottleFirst; | ||
import rx.operators.OperationDebounce; | ||
import rx.operators.OperationTimestamp; | ||
import rx.operators.OperationToObservableFuture; | ||
import rx.operators.OperationToObservableIterable; | ||
|
@@ -1809,6 +1810,182 @@ public static Observable<Long> interval(long interval, TimeUnit unit, Scheduler | |
return create(OperationInterval.interval(interval, unit, scheduler)); | ||
} | ||
|
||
/** | ||
* Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call. | ||
* <p> | ||
* NOTE: If events keep firing faster than the timeout then no data will be emitted. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/debounce.png"> | ||
* <p> | ||
* Information on debounce vs throttle: | ||
* <p> | ||
* <ul> | ||
* <li>http://drupalmotion.com/article/debounce-and-throttle-visual-explanation</li> | ||
* <li>http://unscriptable.com/2009/03/20/debouncing-javascript-methods/</li> | ||
* <li>http://www.illyriad.co.uk/blog/index.php/2011/09/javascript-dont-spam-your-server-debounce-and-throttle/</li> | ||
* </ul> | ||
* | ||
* @param timeout | ||
* The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. | ||
* @param unit | ||
* The {@link TimeUnit} for the timeout. | ||
* | ||
* @return An {@link Observable} which filters out values which are too quickly followed up with newer values. | ||
* @see {@link #throttleWithTimeout}; | ||
*/ | ||
public Observable<T> debounce(long timeout, TimeUnit unit) { | ||
return create(OperationDebounce.debounce(this, timeout, unit)); | ||
} | ||
|
||
/** | ||
* Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call. | ||
* <p> | ||
* NOTE: If events keep firing faster than the timeout then no data will be emitted. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/debounce.png"> | ||
* <p> | ||
* Information on debounce vs throttle: | ||
* <p> | ||
* <ul> | ||
* <li>http://drupalmotion.com/article/debounce-and-throttle-visual-explanation</li> | ||
* <li>http://unscriptable.com/2009/03/20/debouncing-javascript-methods/</li> | ||
* <li>http://www.illyriad.co.uk/blog/index.php/2011/09/javascript-dont-spam-your-server-debounce-and-throttle/</li> | ||
* </ul> | ||
* | ||
* @param timeout | ||
* The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. | ||
* @param unit | ||
* The unit of time for the specified timeout. | ||
* @param scheduler | ||
* The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. | ||
* @return Observable which performs the throttle operation. | ||
* @see {@link #throttleWithTimeout}; | ||
*/ | ||
public Observable<T> debounce(long timeout, TimeUnit unit, Scheduler scheduler) { | ||
return create(OperationDebounce.debounce(this, timeout, unit)); | ||
} | ||
|
||
/** | ||
* Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call. | ||
* <p> | ||
* NOTE: If events keep firing faster than the timeout then no data will be emitted. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleWithTimeout.png"> | ||
* <p> | ||
* Information on debounce vs throttle: | ||
* <p> | ||
* <ul> | ||
* <li>http://drupalmotion.com/article/debounce-and-throttle-visual-explanation</li> | ||
* <li>http://unscriptable.com/2009/03/20/debouncing-javascript-methods/</li> | ||
* <li>http://www.illyriad.co.uk/blog/index.php/2011/09/javascript-dont-spam-your-server-debounce-and-throttle/</li> | ||
* </ul> | ||
* | ||
* @param timeout | ||
* The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. | ||
* @param unit | ||
* The {@link TimeUnit} for the timeout. | ||
* | ||
* @return An {@link Observable} which filters out values which are too quickly followed up with newer values. | ||
* @see {@link #debounce} | ||
*/ | ||
public Observable<T> throttleWithTimeout(long timeout, TimeUnit unit) { | ||
return create(OperationDebounce.debounce(this, timeout, unit)); | ||
} | ||
|
||
/** | ||
* Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call. | ||
* <p> | ||
* NOTE: If events keep firing faster than the timeout then no data will be emitted. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleWithTimeout.png"> | ||
* | ||
* @param timeout | ||
* The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. | ||
* @param unit | ||
* The unit of time for the specified timeout. | ||
* @param scheduler | ||
* The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. | ||
* @return Observable which performs the throttle operation. | ||
* @see {@link #debounce} | ||
*/ | ||
public Observable<T> throttleWithTimeout(long timeout, TimeUnit unit, Scheduler scheduler) { | ||
return create(OperationDebounce.debounce(this, timeout, unit, scheduler)); | ||
} | ||
|
||
/** | ||
* Throttles by skipping value until `skipDuration` passes and then emits the next received value. | ||
* <p> | ||
* This differs from {@link #throttleLast} in that this only tracks passage of time whereas {@link #throttleLast} ticks at scheduled intervals. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleFirst.png"> | ||
* | ||
* @param skipDuration | ||
* Time to wait before sending another value after emitting last value. | ||
* @param unit | ||
* The unit of time for the specified timeout. | ||
* @param scheduler | ||
* The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* @return Observable which performs the throttle operation. | ||
*/ | ||
public Observable<T> throttleFirst(long windowDuration, TimeUnit unit) { | ||
return create(OperationThrottleFirst.throttleFirst(this, windowDuration, unit)); | ||
} | ||
|
||
/** | ||
* Throttles by skipping value until `skipDuration` passes and then emits the next received value. | ||
* <p> | ||
* This differs from {@link #throttleLast} in that this only tracks passage of time whereas {@link #throttleLast} ticks at scheduled intervals. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleFirst.png"> | ||
* | ||
* @param skipDuration | ||
* Time to wait before sending another value after emitting last value. | ||
* @param unit | ||
* The unit of time for the specified timeout. | ||
* @param scheduler | ||
* The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. | ||
* @return Observable which performs the throttle operation. | ||
*/ | ||
public Observable<T> throttleFirst(long skipDuration, TimeUnit unit, Scheduler scheduler) { | ||
return create(OperationThrottleFirst.throttleFirst(this, skipDuration, unit, scheduler)); | ||
} | ||
|
||
/** | ||
* Throttles by returning the last value of each interval defined by 'intervalDuration'. | ||
* <p> | ||
* This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas {@link #throttleFirst} does not tick, it just tracks passage of time. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleLast.png"> | ||
* | ||
* @param intervalDuration | ||
* Duration of windows within with the last value will be chosen. | ||
* @param unit | ||
* The unit of time for the specified interval. | ||
* @return Observable which performs the throttle operation. | ||
* @see {@link #sample(long, TimeUnit)} | ||
*/ | ||
public Observable<T> throttleLast(long intervalDuration, TimeUnit unit) { | ||
return sample(intervalDuration, unit); | ||
} | ||
|
||
/** | ||
* Throttles by returning the last value of each interval defined by 'intervalDuration'. | ||
* <p> | ||
* This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas {@link #throttleFirst} does not tick, it just tracks passage of time. | ||
* <p> | ||
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleLast.png"> | ||
* | ||
* @param intervalDuration | ||
* Duration of windows within with the last value will be chosen. | ||
* @param unit | ||
* The unit of time for the specified interval. | ||
* @return Observable which performs the throttle operation. | ||
* @see {@link #sample(long, TimeUnit, Scheduler)} | ||
*/ | ||
public Observable<T> throttleLast(long intervalDuration, TimeUnit unit, Scheduler scheduler) { | ||
return sample(intervalDuration, unit, scheduler); | ||
} | ||
|
||
/** | ||
* Wraps each item emitted by a source Observable in a {@link Timestamped} object. | ||
* <p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scheduler
has no effect here...