Skip to content

Commit f4e9553

Browse files
elizarovqwwdfsad
authored andcommitted
Deprecate delayFlow and delayEach
This will make Flow API surface more orthogonal with less operators to remember. Both of them can be easily written without too much additional code and still produce quite readable and easy to understand code: delayFlow(time) = onStart { delay(time) } delayEach(time) = onEach { delay(time) } Fixes #1429
1 parent 3f16360 commit f4e9553

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

kotlinx-coroutines-core/common/src/flow/Migration.kt

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
package kotlinx.coroutines.flow
1010

11+
import kotlinx.coroutines.*
12+
import kotlinx.coroutines.flow.internal.*
13+
import kotlinx.coroutines.flow.internal.unsafeFlow
1114
import kotlin.coroutines.*
1215
import kotlin.jvm.*
1316

@@ -361,3 +364,26 @@ public fun <T> Flow<T>.concatWith(value: T): Flow<T> = noImpl()
361364
)
362365
public fun <T> Flow<T>.concatWith(other: Flow<T>): Flow<T> = noImpl()
363366

367+
/**
368+
* Delays the emission of values from this flow for the given [timeMillis].
369+
* Use `onStart { delay(timeMillis) }`.
370+
* @suppress
371+
*/
372+
@Deprecated(
373+
level = DeprecationLevel.WARNING, // since 1.3.0, error in 1.4.0
374+
message = "Use 'onStart { delay(timeMillis) }'",
375+
replaceWith = ReplaceWith("onStart { delay(timeMillis) }")
376+
)
377+
public fun <T> Flow<T>.delayFlow(timeMillis: Long): Flow<T> = onStart { delay(timeMillis) }
378+
379+
/**
380+
* Delays each element emitted by the given flow for the given [timeMillis].
381+
* Use `onEach { delay(timeMillis) }`.
382+
* @suppress
383+
*/
384+
@Deprecated(
385+
level = DeprecationLevel.WARNING, // since 1.3.0, error in 1.4.0
386+
message = "Use 'onEach { delay(timeMillis) }'",
387+
replaceWith = ReplaceWith("onEach { delay(timeMillis) }")
388+
)
389+
public fun <T> Flow<T>.delayEach(timeMillis: Long): Flow<T> = onEach { delay(timeMillis) }

kotlinx-coroutines-core/common/src/flow/operators/Delay.kt

-20
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@ import kotlinx.coroutines.selects.*
1414
import kotlin.jvm.*
1515
import kotlinx.coroutines.flow.internal.unsafeFlow as flow
1616

17-
/**
18-
* Delays the emission of values from this flow for the given [timeMillis].
19-
*/
20-
@ExperimentalCoroutinesApi
21-
public fun <T> Flow<T>.delayFlow(timeMillis: Long): Flow<T> = flow {
22-
delay(timeMillis)
23-
collect(this@flow)
24-
}
25-
26-
/**
27-
* Delays each element emitted by the given flow for the given [timeMillis].
28-
*/
29-
@ExperimentalCoroutinesApi
30-
public fun <T> Flow<T>.delayEach(timeMillis: Long): Flow<T> = flow {
31-
collect { value ->
32-
delay(timeMillis)
33-
emit(value)
34-
}
35-
}
36-
3717
/**
3818
* Returns a flow that mirrors the original flow, but filters out values
3919
* that are followed by the newer values within the given [timeout][timeoutMillis].

0 commit comments

Comments
 (0)