@@ -14,13 +14,30 @@ import kotlinx.coroutines.selects.*
14
14
import kotlin.jvm.*
15
15
import kotlin.time.*
16
16
17
+ /* Scaffolding for Knit code examples
18
+ <!--- TEST_NAME FlowDelayTest -->
19
+ <!--- PREFIX .*-duration-.*
20
+ @file:OptIn(ExperimentalTime::class)
21
+ ----- INCLUDE .*-duration-.*
22
+ import kotlin.time.*
23
+ ----- INCLUDE .*
24
+ import kotlinx.coroutines.*
25
+ import kotlinx.coroutines.flow.*
26
+
27
+ fun main() = runBlocking {
28
+ ----- SUFFIX .*
29
+ .toList().joinToString().let { println(it) } }
30
+ -->
31
+ */
32
+
17
33
/* *
18
34
* Returns a flow that mirrors the original flow, but filters out values
19
35
* that are followed by the newer values within the given [timeout][timeoutMillis].
20
36
* The latest value is always emitted.
21
37
*
22
38
* Example:
23
- * ```
39
+ *
40
+ * ```kotlin
24
41
* flow {
25
42
* emit(1)
26
43
* delay(90)
@@ -33,7 +50,14 @@ import kotlin.time.*
33
50
* emit(5)
34
51
* }.debounce(1000)
35
52
* ```
36
- * produces `3, 4, 5`.
53
+ * <!--- KNIT example-delay-01.kt -->
54
+ *
55
+ * produces the following emissions
56
+ *
57
+ * ```text
58
+ * 3, 4, 5
59
+ * ```
60
+ * <!--- TEST -->
37
61
*
38
62
* Note that the resulting flow does not emit anything as long as the original flow emits
39
63
* items faster than every [timeoutMillis] milliseconds.
@@ -77,7 +101,8 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
77
101
* The latest value is always emitted.
78
102
*
79
103
* Example:
80
- * ```
104
+ *
105
+ * ```kotlin
81
106
* flow {
82
107
* emit(1)
83
108
* delay(90.milliseconds)
@@ -90,7 +115,14 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
90
115
* emit(5)
91
116
* }.debounce(1000.milliseconds)
92
117
* ```
93
- * produces `3, 4, 5`.
118
+ * <!--- KNIT example-delay-duration-01.kt -->
119
+ *
120
+ * produces the following emissions
121
+ *
122
+ * ```text
123
+ * 3, 4, 5
124
+ * ```
125
+ * <!--- TEST -->
94
126
*
95
127
* Note that the resulting flow does not emit anything as long as the original flow emits
96
128
* items faster than every [timeout] milliseconds.
@@ -103,15 +135,23 @@ public fun <T> Flow<T>.debounce(timeout: Duration): Flow<T> = debounce(timeout.t
103
135
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period][periodMillis].
104
136
*
105
137
* Example:
106
- * ```
138
+ *
139
+ * ```kotlin
107
140
* flow {
108
141
* repeat(10) {
109
142
* emit(it)
110
- * delay(50 )
143
+ * delay(110 )
111
144
* }
112
- * }.sample(100)
145
+ * }.sample(200)
146
+ * ```
147
+ * <!--- KNIT example-delay-02.kt -->
148
+ *
149
+ * produces the following emissions
150
+ *
151
+ * ```text
152
+ * 1, 3, 5, 7, 9
113
153
* ```
114
- * produces `1, 3, 5, 7, 9`.
154
+ * <!--- TEST -->
115
155
*
116
156
* Note that the latest element is not emitted if it does not fit into the sampling window.
117
157
*/
@@ -166,15 +206,23 @@ internal fun CoroutineScope.fixedPeriodTicker(delayMillis: Long, initialDelayMil
166
206
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period].
167
207
*
168
208
* Example:
169
- * ```
209
+ *
210
+ * ```kotlin
170
211
* flow {
171
212
* repeat(10) {
172
213
* emit(it)
173
- * delay(50 .milliseconds)
214
+ * delay(110 .milliseconds)
174
215
* }
175
- * }.sample(100.milliseconds)
216
+ * }.sample(200.milliseconds)
217
+ * ```
218
+ * <!--- KNIT example-delay-duration-02.kt -->
219
+ *
220
+ * produces the following emissions
221
+ *
222
+ * ```text
223
+ * 1, 3, 5, 7, 9
176
224
* ```
177
- * produces `1, 3, 5, 7, 9`.
225
+ * <!--- TEST -->
178
226
*
179
227
* Note that the latest element is not emitted if it does not fit into the sampling window.
180
228
*/
0 commit comments