1

I am trying to prevent rapid clicks with plain/pure RxJava using throttleLast. The below code executes fine, but the blockingSubscribe code block never gets called. Am I missing something?

I am not looking for any suggestions around JakeWharton/RxBinding. I am using only RxJava.

Code:

fun previousDateOnClick(view: View) {
    val time = System.currentTimeMillis()
    Timber.d("previousButtonClick time=%s", time)
    Observable.just(time)
            .throttleLast(500, TimeUnit.MILLISECONDS)
            .blockingSubscribe {
                Timber.d("blockingSubscribe throttleLast time=%s", time)
            }
}

Log Output:

2020-03-09 16:29:11.367 D: previousButtonClick time=1583751551367
2020-03-09 16:29:11.535 D: previousButtonClick time=1583751551535
2020-03-09 16:29:11.705 D: previousButtonClick time=1583751551704
2020-03-09 16:29:11.858 D: previousButtonClick time=1583751551857
2020-03-09 16:29:12.009 D: previousButtonClick time=1583751552009
2020-03-09 16:29:12.169 D: previousButtonClick time=1583751552169
2020-03-09 16:29:12.321 D: previousButtonClick time=1583751552320
2020-03-09 16:29:12.475 D: previousButtonClick time=1583751552474
2020-03-09 16:29:12.634 D: previousButtonClick time=1583751552633
2020-03-09 16:29:12.794 D: previousButtonClick time=1583751552793
2020-03-09 16:29:12.954 D: previousButtonClick time=1583751552953
2020-03-09 16:29:13.114 D: previousButtonClick time=1583751553114
2020-03-09 16:29:13.977 D: previousButtonClick time=1583751553976
2020-03-09 16:29:14.221 D: previousButtonClick time=1583751554220
2020-03-09 16:29:14.415 D: previousButtonClick time=1583751554414
2020-03-09 16:29:14.610 D: previousButtonClick time=1583751554609
2020-03-09 16:29:14.786 D: previousButtonClick time=1583751554786
2020-03-09 16:29:14.958 D: previousButtonClick time=1583751554957
2020-03-09 16:29:15.133 D: previousButtonClick time=1583751555132
2020-03-09 16:29:15.311 D: previousButtonClick time=1583751555310
2020-03-09 16:29:15.487 D: previousButtonClick time=1583751555486
2020-03-09 16:29:15.606 D: previousButtonClick time=1583751555605

This thread is not a duplicate of Preventing rapid clicks with RXJava, because there the question is not about preventing rapid click using pure/plain RxJava I can see some third party suggestions there. I implemented whatever is the accepted answer there but still, I am unable to achieve it and I am not interested in other libraries.

Sai
  • 15,188
  • 20
  • 81
  • 121
  • Does this answer your question? [Preventing rapid clicks with RXJava](https://stackoverflow.com/questions/22204774/preventing-rapid-clicks-with-rxjava) – Bob Dalgleish Mar 13 '20 at 14:19

1 Answers1

0

The throttleLast() operator doesn't emit the last item if the Observable was completed before the time interval expired. In your case you have an Observable that emits one element and immediately completes.

If you don't mind receiving the 1st element immediately (followed by throttled events), you could use:

.throttleLatest( 500, TimeUnit.MILLISECONDS, true )

The true argument there indicates that the last element should be emitted even if the Observable completes before the end of the time interval.

If you're really only interested in the last element at the end of each interval and not interested in the 1st, you can split up the Observable using the window() operator and take the last element of each time window:

.window( 500, TimeUnit.MILLISECONDS )
.flatMap( window -> window.lastElement().toObservable() )

But.. there might be a larger issue with the code you provided, but it's tough to tell without knowing your intent. You might not want to create an Observable for each click, but rather generate an Observable using Observable.generate(...) or use a Subject.

TrogDor
  • 984
  • 6
  • 14