Skip to content

Commit df8809d

Browse files
committed
Deprecate BroadcastChannel and the corresponding API
Fixes #2680
1 parent cb0ef71 commit df8809d

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

kotlinx-coroutines-core/common/src/channels/Broadcast.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:Suppress("DEPRECATION")
6+
57
package kotlinx.coroutines.channels
68

79
import kotlinx.coroutines.*
@@ -35,13 +37,13 @@ import kotlin.coroutines.intrinsics.*
3537
* [send][BroadcastChannel.send] and [close][BroadcastChannel.close] operations that interfere with
3638
* the broadcasting coroutine in hard-to-specify ways.
3739
*
38-
* **Note: This API is obsolete since 1.5.0.** It will be deprecated with warning in 1.6.0
39-
* and with error in 1.7.0. It is replaced with [Flow.shareIn][kotlinx.coroutines.flow.shareIn]
40-
* operator.
40+
* **Note: This API is obsolete since 1.5.0.** It is deprecated with warning in 1.7.0.
41+
* It is replaced with [Flow.shareIn][kotlinx.coroutines.flow.shareIn] operator.
4142
*
4243
* @param start coroutine start option. The default value is [CoroutineStart.LAZY].
4344
*/
4445
@ObsoleteCoroutinesApi
46+
@Deprecated(level = DeprecationLevel.WARNING, message = "BroadcastChannel is deprecated in the favour of SharedFlow and is no longer supported")
4547
public fun <E> ReceiveChannel<E>.broadcast(
4648
capacity: Int = 1,
4749
start: CoroutineStart = CoroutineStart.LAZY
@@ -98,12 +100,11 @@ public fun <E> ReceiveChannel<E>.broadcast(
98100
*
99101
* ### Future replacement
100102
*
101-
* This API is obsolete since 1.5.0.
103+
* This API is obsolete since 1.5.0 and deprecated with warning since 1.7.0.
102104
* This function has an inappropriate result type of [BroadcastChannel] which provides
103105
* [send][BroadcastChannel.send] and [close][BroadcastChannel.close] operations that interfere with
104-
* the broadcasting coroutine in hard-to-specify ways. It will be deprecated with warning in 1.6.0
105-
* and with error in 1.7.0. It is replaced with [Flow.shareIn][kotlinx.coroutines.flow.shareIn]
106-
* operator.
106+
* the broadcasting coroutine in hard-to-specify ways.
107+
* It is replaced with [Flow.shareIn][kotlinx.coroutines.flow.shareIn] operator.
107108
*
108109
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
109110
* @param capacity capacity of the channel's buffer (1 by default).
@@ -112,6 +113,7 @@ public fun <E> ReceiveChannel<E>.broadcast(
112113
* @param block the coroutine code.
113114
*/
114115
@ObsoleteCoroutinesApi
116+
@Deprecated(level = DeprecationLevel.WARNING, message = "BroadcastChannel is deprecated in the favour of SharedFlow and is no longer supported")
115117
public fun <E> CoroutineScope.broadcast(
116118
context: CoroutineContext = EmptyCoroutineContext,
117119
capacity: Int = 1,

kotlinx-coroutines-core/common/src/channels/BroadcastChannel.kt

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
@file:Suppress("FunctionName")
5+
@file:Suppress("FunctionName", "DEPRECATION")
66

77
package kotlinx.coroutines.channels
88

@@ -24,10 +24,11 @@ import kotlin.native.concurrent.*
2424
* See `BroadcastChannel()` factory function for the description of available
2525
* broadcast channel implementations.
2626
*
27-
* **Note: This API is obsolete since 1.5.0.** It will be deprecated with warning in 1.6.0
28-
* and with error in 1.7.0. It is replaced with [SharedFlow][kotlinx.coroutines.flow.SharedFlow].
27+
* **Note: This API is obsolete since 1.5.0 and deprecated for removal since 1.7.0**
28+
* It is replaced with [SharedFlow][kotlinx.coroutines.flow.SharedFlow].
2929
*/
3030
@ObsoleteCoroutinesApi
31+
@Deprecated(level = DeprecationLevel.WARNING, message = "BroadcastChannel is deprecated in the favour of SharedFlow and is no longer supported")
3132
public interface BroadcastChannel<E> : SendChannel<E> {
3233
/**
3334
* Subscribes to this [BroadcastChannel] and returns a channel to receive elements from it.
@@ -64,11 +65,11 @@ public interface BroadcastChannel<E> : SendChannel<E> {
6465
* * when `capacity` is [BUFFERED] -- creates `ArrayBroadcastChannel` with a default capacity.
6566
* * otherwise -- throws [IllegalArgumentException].
6667
*
67-
* **Note: This API is obsolete since 1.5.0.** It will be deprecated with warning in 1.6.0
68-
* and with error in 1.7.0. It is replaced with [StateFlow][kotlinx.coroutines.flow.StateFlow]
69-
* and [SharedFlow][kotlinx.coroutines.flow.SharedFlow].
68+
* **Note: This API is obsolete since 1.5.0 and deprecated for removal since 1.7.0**
69+
* It is replaced with [SharedFlow][kotlinx.coroutines.flow.SharedFlow] and [StateFlow][kotlinx.coroutines.flow.StateFlow].
7070
*/
7171
@ObsoleteCoroutinesApi
72+
@Deprecated(level = DeprecationLevel.WARNING, message = "BroadcastChannel is deprecated in the favour of SharedFlow and StateFlow, and is no longer supported")
7273
public fun <E> BroadcastChannel(capacity: Int): BroadcastChannel<E> =
7374
when (capacity) {
7475
0 -> throw IllegalArgumentException("Unsupported 0 capacity for BroadcastChannel")
@@ -92,10 +93,11 @@ public fun <E> BroadcastChannel(capacity: Int): BroadcastChannel<E> =
9293
* In this implementation, [opening][openSubscription] and [closing][ReceiveChannel.cancel] subscription
9394
* takes linear time in the number of subscribers.
9495
*
95-
* **Note: This API is obsolete since 1.5.0.** It will be deprecated with warning in 1.7.0
96-
* and with error in 1.8.0. It is replaced with [StateFlow][kotlinx.coroutines.flow.StateFlow].
96+
* **Note: This API is obsolete since 1.5.0 and deprecated for removal since 1.7.0**
97+
* It is replaced with [SharedFlow][kotlinx.coroutines.flow.StateFlow].
9798
*/
9899
@ObsoleteCoroutinesApi
100+
@Deprecated(level = DeprecationLevel.WARNING, message = "ConflatedBroadcastChannel is deprecated in the favour of SharedFlow and is no longer supported")
99101
public class ConflatedBroadcastChannel<E> private constructor(
100102
private val broadcast: BroadcastChannelImpl<E>
101103
) : BroadcastChannel<E> by broadcast {
@@ -408,5 +410,4 @@ internal class BroadcastChannelImpl<E>(
408410
"SUBSCRIBERS=${subscribers.joinToString(separator = ";", prefix = "<", postfix = ">")}"
409411
}
410412

411-
@SharedImmutable
412-
private val NO_ELEMENT = Symbol("NO_ELEMENT")
413+
private val NO_ELEMENT = Symbol("NO_ELEMENT")

kotlinx-coroutines-core/common/src/channels/Channels.common.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
@file:JvmMultifileClass
55
@file:JvmName("ChannelsKt")
6-
@file:Suppress("DEPRECATION_ERROR")
6+
@file:Suppress("DEPRECATION_ERROR", "DEPRECATION")
77
@file:OptIn(ExperimentalContracts::class)
88

99
package kotlinx.coroutines.channels
@@ -22,10 +22,13 @@ internal const val DEFAULT_CLOSE_MESSAGE = "Channel was closed"
2222
* Opens subscription to this [BroadcastChannel] and makes sure that the given [block] consumes all elements
2323
* from it by always invoking [cancel][ReceiveChannel.cancel] after the execution of the block.
2424
*
25-
* **Note: This API will become obsolete in future updates with introduction of lazy asynchronous streams.**
26-
* See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254).
25+
* **Note: This API is obsolete since 1.5.0 and deprecated for removal since 1.7.0**
26+
* It is replaced with [SharedFlow][kotlinx.coroutines.flow.SharedFlow].
27+
*
28+
* Safe to remove in 1.9.0 as was inline before.
2729
*/
2830
@ObsoleteCoroutinesApi
31+
@Deprecated(level = DeprecationLevel.WARNING, message = "BroadcastChannel is deprecated in the favour of SharedFlow and is no longer supported")
2932
public inline fun <E, R> BroadcastChannel<E>.consume(block: ReceiveChannel<E>.() -> R): R {
3033
val channel = openSubscription()
3134
try {
@@ -107,7 +110,6 @@ public suspend inline fun <E> ReceiveChannel<E>.consumeEach(action: (E) -> Unit)
107110
* The operation is _terminal_.
108111
* This function [consumes][ReceiveChannel.consume] all elements of the original [ReceiveChannel].
109112
*/
110-
@OptIn(ExperimentalStdlibApi::class)
111113
public suspend fun <E> ReceiveChannel<E>.toList(): List<E> = buildList {
112114
consumeEach {
113115
add(it)
@@ -117,10 +119,9 @@ public suspend fun <E> ReceiveChannel<E>.toList(): List<E> = buildList {
117119
/**
118120
* Subscribes to this [BroadcastChannel] and performs the specified action for each received element.
119121
*
120-
* **Note: This API will become obsolete in future updates with introduction of lazy asynchronous streams.**
121-
* See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254).
122+
* **Note: This API is obsolete since 1.5.0 and deprecated for removal since 1.7.0**
122123
*/
123-
@ObsoleteCoroutinesApi
124+
@Deprecated(level = DeprecationLevel.WARNING, message = "BroadcastChannel is deprecated in the favour of SharedFlow and is no longer supported")
124125
public suspend inline fun <E> BroadcastChannel<E>.consumeEach(action: (E) -> Unit): Unit =
125126
consume {
126127
for (element in this) action(element)

kotlinx-coroutines-core/common/src/flow/Channels.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ private class ChannelAsFlow<T>(
144144
* 2) Flow consumer completes normally when the original channel completes (~is closed) normally.
145145
* 3) If the flow consumer fails with an exception, subscription is cancelled.
146146
*/
147+
@Suppress("DEPRECATION")
147148
@Deprecated(
148149
level = DeprecationLevel.WARNING,
149150
message = "'BroadcastChannel' is obsolete and all corresponding operators are deprecated " +
150151
"in the favour of StateFlow and SharedFlow"
151-
) // Since 1.5.0, was @FlowPreview, safe to remove in 1.7.0
152+
) // Since 1.5.0, was @FlowPreview, safe to remove in 1.8.0
152153
public fun <T> BroadcastChannel<T>.asFlow(): Flow<T> = flow {
153154
emitAll(openSubscription())
154155
}

0 commit comments

Comments
 (0)