https://kotlinlang.org logo
#flow
Title
# flow
g

Giorgi

03/13/2023, 7:01 PM
Hi. Does
.debounce
work correctly on JS? For examlpe I have such test
Copy code
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalCoroutinesApi::class)
class Learning {

    @Test
    fun someRandomLearningTest() = runTest {
        val someRandomScope = CoroutineScope(Dispatchers.Default)
        val selectedFiles = MutableStateFlow(123)
        val minimumAmountOfVideoClicks = MutableStateFlow(123)

        val data = selectedFiles.combine(minimumAmountOfVideoClicks) { i: Int, i1: Int -> i + i1 }
            .onEach { println("--- before debounce") }
            .debounce(2000)
            .onEach { println("--- after debounce") }
            .map(::doSomeWork)
            .stateIn(someRandomScope, SharingStarted.Lazily, 0)

        assertEquals(60516, data.take(2).last())
        assertEquals(60516, data.first())
    }

    private fun doSomeWork(n: Int): Int {
        return n * n
    }
}
On JVM it runs okay. but on JS it shows error in Intellij and on browser(while using it in my project) it shows a bit different error. So am I doing it wrong or how can I fix it so it works as on JVM?
4 Views