The debounce operator

Think of a situation where you're receiving emissions rapidly, and are willing to take the last one after taking some time to be sure about it.

When developing an application UI/UX, we often come to such a situation. For example, you have created a text input and are willing to perform some operation when the user types something, but you don't want to perform this operation on each keystroke. You would like to wait a little bit for the user to stop typing (so you've got a good query matching what the user actually wants) and then send it to the downstream operator. The debounce operator serves that exact purpose.

For the sake of simplicity, we will not use any UI/UX code of any platform here (we will definitely try that in the later chapters while learning to implement RxKotlin in Android). Rather, we will try to simulate this using the Observable.create method (if you have any doubt about the Observable.create method, then rush to Chapter 3, Observables, Observers, and Subjects before this). Please refer to the following code:

    fun main(args: Array<String>) { 
      createObservable()//(1) 
        .debounce(200, TimeUnit.MILLISECONDS)//(2) 
        .subscribe { 
           println(it)//(3) 
         } 
    } 
 
    inline fun createObservable():Observable<String> = 
Observable.create<String> { it.onNext("R")//(4) runBlocking { delay(100) }//(5) it.onNext("Re") it.onNext("Reac") runBlocking { delay(130) } it.onNext("Reactiv") runBlocking { delay(140) } it.onNext("Reactive") runBlocking { delay(250) }//(6) it.onNext("Reactive P") runBlocking { delay(130) } it.onNext("Reactive Pro") runBlocking { delay(100) } it.onNext("Reactive Progra") runBlocking { delay(100) } it.onNext("Reactive Programming") runBlocking { delay(300) } it.onNext("Reactive Programming in") runBlocking { delay(100) } it.onNext("Reactive Programming in Ko") runBlocking { delay(150) } it.onNext("Reactive Programming in Kotlin") runBlocking { delay(250) } it.onComplete() }

In this program, we tried to keep the main function clean by exporting the Observable creation to another function (createObservable()) to help you understand better. On comment (1), we called the createObservable() function to create an Observable instance.

Inside the createObservable() function, we tried to simulate user typing behavior by emitting a series of incremental Strings with intervals, until it reached the final version (Reactive Programming in Kotlin). We provided bigger intervals after completing each word depicting an ideal user behavior.

On comment (2), we used the debounce() operator with 200 and TimeUnit.MILLISECONDS as parameters that'll make the downstream wait for 200 milliseconds after each emission and take the emissions only if no other emissions occurred in between.

The output is as follows:

Observer receives only three emits, after which the Observable took at least 200 milliseconds before emitting the next one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.144