Skip to content

Commit

Permalink
java.time adapter for Flow.debounce and Flow.sample (#1834)
Browse files Browse the repository at this point in the history
Co-authored-by: Francesco Vasco <fsco_v-github@yahoo.it>
  • Loading branch information
elizarov and fvasco committed Mar 13, 2020
1 parent 4116fbf commit 5cfd6c0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Expand Up @@ -12,8 +12,10 @@ public final class kotlinx/coroutines/stream/StreamKt {
}

public final class kotlinx/coroutines/time/TimeKt {
public static final fun debounce (Lkotlinx/coroutines/flow/Flow;Ljava/time/Duration;)Lkotlinx/coroutines/flow/Flow;
public static final fun delay (Ljava/time/Duration;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun onTimeout (Lkotlinx/coroutines/selects/SelectBuilder;Ljava/time/Duration;Lkotlin/jvm/functions/Function1;)V
public static final fun sample (Lkotlinx/coroutines/flow/Flow;Ljava/time/Duration;)Lkotlinx/coroutines/flow/Flow;
public static final fun withTimeout (Ljava/time/Duration;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun withTimeoutOrNull (Ljava/time/Duration;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
Expand Down
13 changes: 13 additions & 0 deletions integration/kotlinx-coroutines-jdk8/src/time/Time.kt
Expand Up @@ -4,6 +4,7 @@
package kotlinx.coroutines.time

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.selects.*
import java.time.*
import java.time.temporal.*
Expand All @@ -14,6 +15,18 @@ import java.time.temporal.*
public suspend fun delay(duration: Duration) =
kotlinx.coroutines.delay(duration.coerceToMillis())

/**
* "java.time" adapter method for [kotlinx.coroutines.flow.debounce].
*/
@FlowPreview
public fun <T> Flow<T>.debounce(timeout: Duration) = debounce(timeout.coerceToMillis())

/**
* "java.time" adapter method for [kotlinx.coroutines.flow.sample].
*/
@FlowPreview
public fun <T> Flow<T>.sample(period: Duration) = sample(period.coerceToMillis())

/**
* "java.time" adapter method for [SelectBuilder.onTimeout].
*/
Expand Down
38 changes: 38 additions & 0 deletions integration/kotlinx-coroutines-jdk8/test/time/FlowDebounceTest.kt
@@ -0,0 +1,38 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.time

import kotlinx.coroutines.TestBase
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.withVirtualTime
import org.junit.Test
import java.time.Duration
import kotlin.test.assertEquals

class FlowDebounceTest : TestBase() {
@Test
public fun testBasic() = withVirtualTime {
expect(1)
val flow = flow {
expect(3)
emit("A")
delay(Duration.ofMillis(1500))
emit("B")
delay(Duration.ofMillis(500))
emit("C")
delay(Duration.ofMillis(250))
emit("D")
delay(Duration.ofMillis(2000))
emit("E")
expect(4)
}

expect(2)
val result = flow.debounce(Duration.ofMillis(1000)).toList()
assertEquals(listOf("A", "D", "E"), result)
finish(5)
}
}
39 changes: 39 additions & 0 deletions integration/kotlinx-coroutines-jdk8/test/time/FlowSampleTest.kt
@@ -0,0 +1,39 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.time

import kotlinx.coroutines.TestBase
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.withVirtualTime
import org.junit.Test
import java.time.Duration
import kotlin.test.assertEquals


class SampleTest : TestBase() {
@Test
public fun testBasic() = withVirtualTime {
expect(1)
val flow = flow {
expect(3)
emit("A")
delay(Duration.ofMillis(1500))
emit("B")
delay(Duration.ofMillis(500))
emit("C")
delay(Duration.ofMillis(250))
emit("D")
delay(Duration.ofMillis(2000))
emit("E")
expect(4)
}

expect(2)
val result = flow.sample(Duration.ofMillis(1000)).toList()
assertEquals(listOf("A", "B", "D"), result)
finish(5)
}
}

0 comments on commit 5cfd6c0

Please sign in to comment.