Skip to content

Instantly share code, notes, and snippets.

@kiwiandroiddev
Last active October 19, 2018 07:44
Show Gist options
  • Save kiwiandroiddev/8bb9fe5b3966f663efae52a0089ede1f to your computer and use it in GitHub Desktop.
Save kiwiandroiddev/8bb9fe5b3966f663efae52a0089ede1f to your computer and use it in GitHub Desktop.
Selectively throttles items in an observable stream
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