Skip to content

Commit 54a155c

Browse files
committed
Update the documentation of time-based operators
1 parent d0aa7fc commit 54a155c

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

Diff for: kotlinx-coroutines-core/common/src/Delay.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ public suspend fun awaitCancellation(): Nothing = suspendCancellableCoroutine {}
117117
*
118118
* Note that delay can be used in [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
119119
*
120-
* Implementation note: how exactly time is tracked is an implementation detail of [CoroutineDispatcher] in the context.
120+
* By default, on the JVM and Native, a `Dispatchers.IO` thread is used to calculate when the delay has passed,
121+
* whereas on JS, the `Window.setTimeout` function is used, and on Wasm/WASI, `poll_oneoff` with the monotonic clock
122+
* event type is used.
123+
* It is possible for a [CoroutineDispatcher] to override this behavior and provide its own implementation
124+
* of time tracking.
125+
* For example, [Dispatchers.Main] typically uses the main thread's event loop to track time.
126+
* However, the functionality of defining custom time tracking is not exposed to the public API.
127+
*
121128
* @param timeMillis time in milliseconds.
122129
*/
123130
public suspend fun delay(timeMillis: Long) {
@@ -143,7 +150,13 @@ public suspend fun delay(timeMillis: Long) {
143150
*
144151
* Note that delay can be used in [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
145152
*
146-
* Implementation note: how exactly time is tracked is an implementation detail of [CoroutineDispatcher] in the context.
153+
* By default, on the JVM and Native, a `Dispatchers.IO` thread is used to calculate when the delay has passed,
154+
* whereas on JS, the `Window.setTimeout` function is used, and on Wasm/WASI, `poll_oneoff` with the monotonic clock
155+
* event type is used.
156+
* It is possible for a [CoroutineDispatcher] to override this behavior and provide its own implementation
157+
* of time tracking.
158+
* For example, [Dispatchers.Main] typically uses the main thread's event loop to track time.
159+
* However, the functionality of defining custom time tracking is not exposed to the public API.
147160
*/
148161
public suspend fun delay(duration: Duration): Unit = delay(duration.toDelayMillis())
149162

Diff for: kotlinx-coroutines-core/common/src/Timeout.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import kotlin.time.Duration.Companion.milliseconds
3131
* [Asynchronous timeout and resources](https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources)
3232
* section of the coroutines guide for details.
3333
*
34-
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
34+
* For a description of how waiting for a specific duration is implemented, see [delay].
3535
*
3636
* @param timeMillis timeout time in milliseconds.
3737
*/
@@ -63,7 +63,7 @@ public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineSco
6363
* [Asynchronous timeout and resources](https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources)
6464
* section of the coroutines guide for details.
6565
*
66-
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
66+
* For a description of how waiting for a specific duration is implemented, see [delay].
6767
*/
6868
public suspend fun <T> withTimeout(timeout: Duration, block: suspend CoroutineScope.() -> T): T {
6969
contract {

Diff for: kotlinx-coroutines-core/common/src/flow/SharingStarted.kt

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public fun interface SharingStarted {
9696
*
9797
* This function throws [IllegalArgumentException] when either [stopTimeoutMillis] or [replayExpirationMillis]
9898
* are negative.
99+
*
100+
* For a description of how waiting for a specific duration is implemented, see [delay].
99101
*/
100102
@Suppress("FunctionName")
101103
public fun WhileSubscribed(
@@ -129,6 +131,8 @@ public fun interface SharingStarted {
129131
*
130132
* This function throws [IllegalArgumentException] when either [stopTimeout] or [replayExpiration]
131133
* are negative.
134+
*
135+
* For a description of how waiting for a specific duration is implemented, see [delay].
132136
*/
133137
@Suppress("FunctionName")
134138
public fun SharingStarted.Companion.WhileSubscribed(

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

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ fun main() = runBlocking {
5656
*
5757
* Note that the resulting flow does not emit anything as long as the original flow emits
5858
* items faster than every [timeoutMillis] milliseconds.
59+
*
60+
* For a description of how waiting for a specific duration is implemented, see [delay].
5961
*/
6062
@FlowPreview
6163
public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
@@ -104,6 +106,8 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
104106
* Note that the resulting flow does not emit anything as long as the original flow emits
105107
* items faster than every [timeoutMillis] milliseconds.
106108
*
109+
* For a description of how waiting for a specific duration is implemented, see [delay].
110+
*
107111
* @param timeoutMillis [T] is the emitted value and the return value is timeout in milliseconds.
108112
*/
109113
@FlowPreview
@@ -142,6 +146,8 @@ public fun <T> Flow<T>.debounce(timeoutMillis: (T) -> Long): Flow<T> =
142146
*
143147
* Note that the resulting flow does not emit anything as long as the original flow emits
144148
* items faster than every [timeout] milliseconds.
149+
*
150+
* For a description of how waiting for a specific duration is implemented, see [delay].
145151
*/
146152
@FlowPreview
147153
public fun <T> Flow<T>.debounce(timeout: Duration): Flow<T> =
@@ -187,6 +193,8 @@ public fun <T> Flow<T>.debounce(timeout: Duration): Flow<T> =
187193
* Note that the resulting flow does not emit anything as long as the original flow emits
188194
* items faster than every [timeout] unit.
189195
*
196+
* For a description of how waiting for a specific duration is implemented, see [delay].
197+
*
190198
* @param timeout [T] is the emitted value and the return value is timeout in [Duration].
191199
*/
192200
@FlowPreview
@@ -264,6 +272,8 @@ private fun <T> Flow<T>.debounceInternal(timeoutMillisSelector: (T) -> Long): Fl
264272
* <!--- TEST -->
265273
*
266274
* Note that the latest element is not emitted if it does not fit into the sampling window.
275+
*
276+
* For a description of how waiting for a specific duration is implemented, see [delay].
267277
*/
268278
@FlowPreview
269279
public fun <T> Flow<T>.sample(periodMillis: Long): Flow<T> {
@@ -335,6 +345,8 @@ internal fun CoroutineScope.fixedPeriodTicker(
335345
* <!--- TEST -->
336346
*
337347
* Note that the latest element is not emitted if it does not fit into the sampling window.
348+
*
349+
* For a description of how waiting for a specific duration is implemented, see [delay].
338350
*/
339351
@FlowPreview
340352
public fun <T> Flow<T>.sample(period: Duration): Flow<T> = sample(period.toDelayMillis())
@@ -377,6 +389,8 @@ public fun <T> Flow<T>.sample(period: Duration): Flow<T> = sample(period.toDelay
377389
*
378390
* Note that delaying on the downstream doesn't trigger the timeout.
379391
*
392+
* For a description of how waiting for a specific duration is implemented, see [delay].
393+
*
380394
* @param timeout Timeout duration. If non-positive, the flow is timed out immediately
381395
*/
382396
@FlowPreview

Diff for: kotlinx-coroutines-core/common/src/selects/OnTimeout.kt

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import kotlin.time.*
77
* Clause that selects the given [block] after a specified timeout passes.
88
* If timeout is negative or zero, [block] is selected immediately.
99
*
10+
* For a description of how waiting for a specific duration is implemented, see [delay].
11+
*
1012
* **Note: This is an experimental api.** It may be replaced with light-weight timer/timeout channels in the future.
1113
*
1214
* @param timeMillis timeout time in milliseconds.
@@ -20,6 +22,8 @@ public fun <R> SelectBuilder<R>.onTimeout(timeMillis: Long, block: suspend () ->
2022
* Clause that selects the given [block] after the specified [timeout] passes.
2123
* If timeout is negative or zero, [block] is selected immediately.
2224
*
25+
* For a description of how waiting for a specific duration is implemented, see [delay].
26+
*
2327
* **Note: This is an experimental api.** It may be replaced with light-weight timer/timeout channels in the future.
2428
*/
2529
@ExperimentalCoroutinesApi

0 commit comments

Comments
 (0)