File tree Expand file tree Collapse file tree 2 files changed +53
-2
lines changed
kotlinx-coroutines-core/common Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -144,9 +144,19 @@ public inline fun <T> SharedFlow<T>.retryWhen(noinline predicate: suspend FlowCo
144
144
level = DeprecationLevel .WARNING
145
145
)
146
146
@InlineOnly
147
- public suspend inline fun <T > SharedFlow<T>.toList (destination : MutableList < T > = ArrayList () ): List <T > =
147
+ public suspend inline fun <T > SharedFlow<T>.toList (): List <T > =
148
148
(this as Flow <T >).toList()
149
149
150
+ /* *
151
+ * A specialized version of [Flow.toList] that returns [Nothing]
152
+ * to indicate that [SharedFlow] collection never completes.
153
+ */
154
+ @InlineOnly
155
+ public suspend inline fun <T > SharedFlow<T>.toList (destination : MutableList <T >): Nothing {
156
+ (this as Flow <T >).toList(destination)
157
+ throw IllegalStateException (" this code is supposed to be unreachable" )
158
+ }
159
+
150
160
/* *
151
161
* @suppress
152
162
*/
@@ -156,9 +166,19 @@ public suspend inline fun <T> SharedFlow<T>.toList(destination: MutableList<T> =
156
166
level = DeprecationLevel .WARNING
157
167
)
158
168
@InlineOnly
159
- public suspend inline fun <T > SharedFlow<T>.toSet (destination : MutableSet < T > = LinkedHashSet () ): Set <T > =
169
+ public suspend inline fun <T > SharedFlow<T>.toSet (): Set <T > =
160
170
(this as Flow <T >).toSet()
161
171
172
+ /* *
173
+ * A specialized version of [Flow.toSet] that returns [Nothing]
174
+ * to indicate that [SharedFlow] collection never completes.
175
+ */
176
+ @InlineOnly
177
+ public suspend inline fun <T > SharedFlow<T>.toSet (destination : MutableSet <T >): Nothing {
178
+ (this as Flow <T >).toSet(destination)
179
+ throw IllegalStateException (" this code is supposed to be unreachable" )
180
+ }
181
+
162
182
/* *
163
183
* @suppress
164
184
*/
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.flow.operators
6
+
7
+ import kotlinx.coroutines.*
8
+ import kotlinx.coroutines.flow.*
9
+ import kotlin.test.*
10
+
11
+ class LintTest : TestBase () {
12
+ /* *
13
+ * Tests that using [SharedFlow.toList] and similar functions by passing a mutable collection does add values
14
+ * to the provided collection.
15
+ */
16
+ @Test
17
+ fun testSharedFlowToCollection () = runTest {
18
+ val sharedFlow = MutableSharedFlow <Int >()
19
+ val list = mutableListOf<Int >()
20
+ val set = mutableSetOf<Int >()
21
+ val jobs = listOf (suspend { sharedFlow.toList(list) }, { sharedFlow.toSet(set) }).map {
22
+ launch(Dispatchers .Unconfined ) { it() }
23
+ }
24
+ repeat(10 ) {
25
+ sharedFlow.emit(it)
26
+ }
27
+ jobs.forEach { it.cancelAndJoin() }
28
+ assertEquals((0 .. 9 ).toList(), list)
29
+ assertEquals((0 .. 9 ).toSet(), set)
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments