-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add ReceiveChannel.toList(destination) #4520
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1142,6 +1142,7 @@ final suspend fun kotlinx.coroutines/yield() // kotlinx.coroutines/yield|yield() | |
final suspend inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.coroutines.flow/Flow<#A>).kotlinx.coroutines.flow/fold(#B, crossinline kotlin.coroutines/SuspendFunction2<#B, #A, #B>): #B // kotlinx.coroutines.flow/fold|[email protected]<0:0>(0:1;kotlin.coroutines.SuspendFunction2<0:1,0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0] | ||
final suspend inline fun <#A: kotlin/Any?> (kotlinx.coroutines.channels/BroadcastChannel<#A>).kotlinx.coroutines.channels/consumeEach(kotlin/Function1<#A, kotlin/Unit>) // kotlinx.coroutines.channels/consumeEach|[email protected]<0:0>(kotlin.Function1<0:0,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final suspend inline fun <#A: kotlin/Any?> (kotlinx.coroutines.channels/ReceiveChannel<#A>).kotlinx.coroutines.channels/consumeEach(kotlin/Function1<#A, kotlin/Unit>) // kotlinx.coroutines.channels/consumeEach|[email protected]<0:0>(kotlin.Function1<0:0,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final suspend inline fun <#A: kotlin/Any?> (kotlinx.coroutines.channels/ReceiveChannel<#A>).kotlinx.coroutines.channels/toList(kotlin.collections/MutableList<#A> = ...): kotlin.collections/List<#A> // kotlinx.coroutines.channels/toList|[email protected]<0:0>(kotlin.collections.MutableList<0:0>){0§<kotlin.Any?>}[0] | ||
final suspend inline fun <#A: kotlin/Any?> (kotlinx.coroutines.flow/Flow<#A>).kotlinx.coroutines.flow/collect(crossinline kotlin.coroutines/SuspendFunction1<#A, kotlin/Unit>) // kotlinx.coroutines.flow/collect|[email protected]<0:0>(kotlin.coroutines.SuspendFunction1<0:0,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final suspend inline fun <#A: kotlin/Any?> (kotlinx.coroutines.flow/Flow<#A>).kotlinx.coroutines.flow/collectIndexed(crossinline kotlin.coroutines/SuspendFunction2<kotlin/Int, #A, kotlin/Unit>) // kotlinx.coroutines.flow/collectIndexed|[email protected]<0:0>(kotlin.coroutines.SuspendFunction2<kotlin.Int,0:0,kotlin.Unit>){0§<kotlin.Any?>}[0] | ||
final suspend inline fun <#A: kotlin/Any?> (kotlinx.coroutines.flow/SharedFlow<#A>).kotlinx.coroutines.flow/count(): kotlin/Int // kotlinx.coroutines.flow/count|[email protected]<0:0>(){0§<kotlin.Any?>}[0] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,7 +162,8 @@ public suspend inline fun <E> ReceiveChannel<E>.consumeEach(action: (E) -> Unit) | |
} | ||
|
||
/** | ||
* Returns a [List] containing all the elements sent to this channel, preserving their order. | ||
* Consumes the elements of this channel into the given [destination] mutable list. | ||
* If none is provided, a new [ArrayList] will be created. | ||
* | ||
* This function will attempt to receive elements and put them into the list until the channel is | ||
* [closed][SendChannel.close]. | ||
|
@@ -172,6 +173,8 @@ public suspend inline fun <E> ReceiveChannel<E>.consumeEach(action: (E) -> Unit) | |
* until exhausting it. | ||
* | ||
* If the channel is [closed][SendChannel.close] with a cause, [toList] will rethrow that cause. | ||
* However, the [destination] list is left in a consistent state containing all the elements received from the channel | ||
* up to that point. | ||
* | ||
* The operation is _terminal_. | ||
* This function [consumes][ReceiveChannel.consume] all elements of the original [ReceiveChannel]. | ||
|
@@ -183,16 +186,13 @@ public suspend inline fun <E> ReceiveChannel<E>.consumeEach(action: (E) -> Unit) | |
* // sends elements to it, and closes it | ||
* // once the coroutine's body finishes | ||
* val channel = produce { | ||
* values.forEach { send(it) } | ||
* values.forEach { send(it) } | ||
* } | ||
* check(channel.toList() == values) | ||
* ``` | ||
*/ | ||
public suspend fun <E> ReceiveChannel<E>.toList(): List<E> = buildList { | ||
consumeEach { | ||
add(it) | ||
} | ||
} | ||
public suspend inline fun <T> ReceiveChannel<T>.toList(destination: MutableList<T> = ArrayList()): List<T> = | ||
|
||
consumeEach(destination::add).let { destination } | ||
|
||
@PublishedApi | ||
internal fun ReceiveChannel<*>.cancelConsumed(cause: Throwable?) { | ||
|
@@ -201,3 +201,5 @@ internal fun ReceiveChannel<*>.cancelConsumed(cause: Throwable?) { | |
}) | ||
} | ||
|
||
@Deprecated("Preserving binary compatibility, was stable", level = DeprecationLevel.HIDDEN) | ||
public suspend fun <T> ReceiveChannel<T>.toList(): List<T> = toList(ArrayList()) | ||
|
Uh oh!
There was an error while loading. Please reload this page.