Last active
October 19, 2018 07:44
-
-
Save kiwiandroiddev/8bb9fe5b3966f663efae52a0089ede1f to your computer and use it in GitHub Desktop.
Selectively throttles items in an observable stream
This file contains hidden or 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
fun <T> Observable<T>.throttleMatchingItems( | |
windowDuration: Long, | |
unit: TimeUnit, | |
predicate: (T) -> Boolean | |
): Observable<T> { | |
return this.compose { stream -> | |
val throttledItems = stream | |
.filter(predicate) | |
.throttleFirst(windowDuration, unit) | |
val allOtherItems = stream.filter { !predicate(it) } | |
Observable.merge(throttledItems, allOtherItems) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment