From 742abb18022564edaa144ccdcb5e235831bcd7b4 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Fri, 21 Feb 2020 01:03:25 +0100 Subject: [PATCH 1/3] Add more documentation about a case where .offer() can throw --- kotlinx-coroutines-core/common/src/flow/Builders.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/flow/Builders.kt b/kotlinx-coroutines-core/common/src/flow/Builders.kt index 294044d875..2d60769779 100644 --- a/kotlinx-coroutines-core/common/src/flow/Builders.kt +++ b/kotlinx-coroutines-core/common/src/flow/Builders.kt @@ -293,9 +293,14 @@ public fun channelFlow(@BuilderInference block: suspend ProducerScope.() * fun flowFrom(api: CallbackBasedApi): Flow = callbackFlow { * val callback = object : Callback { // implementation of some callback interface * override fun onNextValue(value: T) { - * // Note: offer drops value when buffer is full + * // Note: offer drops values when the buffer is full * // Use either buffer(Channel.CONFLATED) or buffer(Channel.UNLIMITED) to avoid overfill - * offer(value) + * // Also, offer will throw if the channel is canceled. + * // To avoid that, we wrap the call in runBlocking. + * // See https://github.com/Kotlin/kotlinx.coroutines/issues/974 + * runBlocking { + * offer(value) + * } * } * override fun onApiError(cause: Throwable) { * cancel(CancellationException("API Error", cause)) From e8acbbe3cea3cfc42cb56bd7708c3883c0b405ce Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Fri, 21 Feb 2020 15:52:33 +0100 Subject: [PATCH 2/3] Update kotlinx-coroutines-core/common/src/flow/Builders.kt Co-Authored-By: Louis CAD --- kotlinx-coroutines-core/common/src/flow/Builders.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/flow/Builders.kt b/kotlinx-coroutines-core/common/src/flow/Builders.kt index 2d60769779..776e64ea65 100644 --- a/kotlinx-coroutines-core/common/src/flow/Builders.kt +++ b/kotlinx-coroutines-core/common/src/flow/Builders.kt @@ -298,7 +298,7 @@ public fun channelFlow(@BuilderInference block: suspend ProducerScope.() * // Also, offer will throw if the channel is canceled. * // To avoid that, we wrap the call in runBlocking. * // See https://github.com/Kotlin/kotlinx.coroutines/issues/974 - * runBlocking { + * runCatching { * offer(value) * } * } @@ -332,4 +332,4 @@ private class ChannelFlowBuilder( override fun toString(): String = "block[$block] -> ${super.toString()}" -} \ No newline at end of file +} From 3d1d63a54f3404f062d98d99cb7c73cde928716e Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Fri, 21 Feb 2020 15:53:50 +0100 Subject: [PATCH 3/3] Update Builders.kt --- kotlinx-coroutines-core/common/src/flow/Builders.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kotlinx-coroutines-core/common/src/flow/Builders.kt b/kotlinx-coroutines-core/common/src/flow/Builders.kt index 776e64ea65..ec12e01269 100644 --- a/kotlinx-coroutines-core/common/src/flow/Builders.kt +++ b/kotlinx-coroutines-core/common/src/flow/Builders.kt @@ -296,7 +296,7 @@ public fun channelFlow(@BuilderInference block: suspend ProducerScope.() * // Note: offer drops values when the buffer is full * // Use either buffer(Channel.CONFLATED) or buffer(Channel.UNLIMITED) to avoid overfill * // Also, offer will throw if the channel is canceled. - * // To avoid that, we wrap the call in runBlocking. + * // To avoid that, we wrap the call in runCatching. * // See https://github.com/Kotlin/kotlinx.coroutines/issues/974 * runCatching { * offer(value)