Skip to content

Instantly share code, notes, and snippets.

Created October 11, 2020 16:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import rx.Subscription
import rx.functions.Action1
import rx.subjects.PublishSubject
import rx.subjects.Subject
import java.util.concurrent.TimeUnit
class ThrottleTrackingBus(private val onSuccess: Action1<Set<Int>>) {
private val publishSubject: Subject<VisibleState, VisibleState>
private val subscription: Subscription
private var recentVisibleState = VisibleState()
init {
publishSubject = PublishSubject.create()
subscription = publishSubject
.throttleWithTimeout(THRESHOLD_MS.toLong(), TimeUnit.MILLISECONDS)
fun postViewEvent(visibleState: VisibleState) = publishSubject.onNext(visibleState)
fun unsubscribe() = subscription.unsubscribe()
private fun onCallback(visibleState: VisibleState) {
val recentRange =
val currentRange = visibleState.firstCompletelyVisible..visibleState.lastCompletelyVisible
val subtractItems = currentRange.subtract(recentRange)
if (subtractItems.isNotEmpty()) {
recentVisibleState = visibleState
if (subtractItems.contains(-1).not())
companion object {
private const val THRESHOLD_MS = 250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment