Kotlin coroutine flow sends time interval debounce

Kotlin coroutine flow sends time interval debounce

The function of debounce is to space out consecutively transmitted data. A typical application scenario is keyword input in a search engine. When a user enters a character, sometimes, it is not expected that the user will trigger a real query in the background every time the user enters any word. It is guessed that the user has completed the input of the complete keyword at this time), and the last input is actually sent. Debounce contains a certain buffering idea, that is, the event is not triggered immediately, but the data to be emitted is put into the queue first, and the trigger is delayed for a certain period of time (time), and the important condition of the trigger depends on the previous data and the next data Note that the previous one and the next one have not been fired, but are just waiting. If two consecutive intervals satisfy timeOut, only the last piece of data will be kept.

 

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main(args: Array<String>) {
    val timeOut = 100L

    runBlocking {
        val result = userInput()
            .debounce(timeOut).collect {
                println(it)
            }
    }
}

fun userInput() = flow {
    emit("A")
    delay(100)
    emit("AB")
    delay(150)
    emit("ABC")
    delay(200)
    emit("ABCD")
    delay(100)
    emit("ABCDE")
}

 

when timeOut=100

AB
ABC
ABCD
ABCDE

Both A and AB are to be transmitted, and the interval between A and AB is 100, so A is skipped and AB is transmitted directly.

After launching AB, because the interval between ABC and the previous AB exceeds 100, continue to launch.

 

 

 

 

When timeOut=150, it is more difficult to understand.

AB
ABC
ABCDE

A is waiting to be launched, and Kotlin finds that the interval between A and AB is only 100, so A is discarded. Kotlin finds that the waiting time from A to ABC after AB exceeds 150, and the previous A has not launched, so AB is launched.

After launching AB, there is an interval of 150 between the next ABC and AB, and there is still a time interval between ABC and AB, so continue to transmit ABC.

After launching ABC, Kotlin examines the ABCD to be launched. It stands to reason that the interval between ABCD and ABC is 200, which meets the launch timeOut value, but the interval between ABCD and ABCDE is 100, so ABCD is skipped and ABCDE is directly launched. Pay special attention here. When Kotlin transmits the current data, it needs to read through the previous and next items of this data.

 

 

 

When timeOut=200

ABC
ABCDE

The interval between A and AB is 100, and A is discarded. The interval between AB and the previous A is 100, and the interval between ABC and ABC is 150. If it is less than the timeOut value, it is discarded. ABC has a time interval with the previous AB, and the interval with the subsequent ABCD is just equal to the timeOut value, so it is transmitted.

 

 

 

When timeOut=250

ABCDE

 

To put it simply, when Kotlin uses debounce to launch the current data, the previous data and the next data of this data must meet the time interval before it can be launched, otherwise it will be discarded.

 

Kotlin coroutine flow, firstOrNull only needs to be first first, lastOrNull only needs to be last_zhangphil's blog-CSDN blog firstOrNull() //Although a lot of data is emitted, only the data emitted for the first time is required. .lastOrNull() //Although a lot of data is transmitted, only the data transmitted last time is required. emit(i)delay(10) kotlin coroutine flow task ends unexpectedly and does not emit data retryWhen onEmpty(5)_zhangphil's blog-CSDN blog 1. flow, emit, onCompletion, collect. https://blog.csdn.net/zhangphil/article/details/132492588 kotlin coroutine flow filter map flowOn zip combine (1)_zhangphil's blog - CSDN blog 1. flow, emit, onCompletion, collect. Fourth, map, reorganize and rewrite data. Eight, conflate merge. Nine, debounce deduplication. Second, the function as a flow. https://blog.csdn.net/zhangphil/article/details/130084723

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/132515686