Skip to content

#1834 fixup #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
}
}