Difference between debounce and flow when Kotlin pipeline Channel receivesAsFlow

Difference between debounce and flow when Kotlin pipeline Channel receivesAsFlow

 

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

const val timeOut = Long.MAX_VALUE
//val delayTimes = arrayListOf(50L, 100L, 150L, 200L, 250L)

fun main(args: Array<String>) {
    val channel = Channel<String>()

    runBlocking {
        launch(Dispatchers.IO) {
            repeat(5) {
                println("-")

                val t = System.currentTimeMillis()
                channel.send("$it-$t")

                val d = getDelayTime()
                println("$it channel 休眠 $d")
                delay(d)
            }
        }

        launch(Dispatchers.IO) {
            flow {
                repeat(5) {
                    println("--")

                    val t = System.currentTimeMillis()
                    emit("$it-$t")

                    val d = getDelayTime()
                    println("$it flow 休眠 $d")
                    delay(d)
                }
            }.debounce(timeOut) //这里的timeOut值很大,flow的collect收到。
                .collect {
                    println("flow $it")
                }
        }

        channel.receiveAsFlow()
            .debounce(timeOut) //这里的timeOut值很大,collect收不到。
            .collect {
                println("debounce $it")
            } //一直阻塞接收消息
    }

    //阻塞,其实走不到这里。
    channel.close()
}

fun getDelayTime(): Long {
    return 10
}

 

-
--
0 channel sleep 10
0 flow sleep 10
--
-
1 flow sleep 10
1 channel sleep 10
-
--
2 channel sleep 10
2 flow sleep 10
-
--
3 channel sleep 10
3 flow sleep 10
-
--
4 channel sleep 10
4 flow sleep 10
flow 4-1693561918986
 

After the program runs, flow will soon receive the last piece of data 4-xxx..., and Channel will wait forever when receiveAsFlow receives data debounce. Note the difference between this pure flow and Channel's receiveAsFlow.

 

 

Kotlin coroutine flow's debounce and pipeline Channel_zhangphil's blog-CSDN blog kotlin coroutine pipeline Channel. 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 for hit. 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. Both A and AB are to be transmitted, and the interval between A and AB is 100, so skip A and directly transmit AB. https://blog.csdn.net/zhangphil/article/details/132525124

The debounce parameter timeoutMillis feature of Kotlin coroutine flow_zhangphil's blog-CSDN blog debounce contains a certain buffering idea, that is, instead of triggering an event immediately, it first puts the data to be emitted into the queue and waits for a certain period of time (time) delay Trigger, and the important condition of the trigger depends on the time interval between the previous data and the next data. Note that the previous and the next data have not been emitted yet, but are just waiting. 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. Fourth, map, reorganize and rewrite data. Both A and AB are to be transmitted, and the interval between A and AB is 100, so skip A and directly transmit AB. https://blog.csdn.net/zhangphil/article/details/132525869

Kotlin coroutine flow sending time interval debounce_zhangphil's blog-CSDN blog debounce contains a certain buffering idea, that is, instead of triggering an event immediately, it first puts the data to be emitted into the queue, and waits for a certain period of time (time) to delay the trigger, and The important condition for triggering depends on the time interval between the previous data and the next data. Note that the previous and next data have not been sent out yet, but are just waiting. 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. Fourth, map, reorganize and rewrite data. Both A and AB are to be transmitted, and the interval between A and AB is 100, so skip A and directly transmit AB. https://blog.csdn.net/zhangphil/article/details/132515686

 

Guess you like

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