Skip to content

Commit 3638747

Browse files
committed
BroadcastChannel pseudo-constructor is replaced with top-level fun
1 parent f2a710a commit 3638747

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/ArrayBroadcastChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import kotlin.concurrent.withLock
3030
* Note, that elements that are sent to the broadcast channel while there are no [openSubscription] subscribers are immediately
3131
* lost.
3232
*
33+
* This channel is created by `BroadcastChannel(capacity)` factory function invocation.
34+
*
3335
* This implementation uses lock to protect the buffer, which is held only during very short buffer-update operations.
3436
* The lock at each subscription is also used to manage concurrent attempts to receive from the same subscriber.
3537
* The lists of suspended senders or receivers are lock-free.

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/BroadcastChannel.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,21 @@ import java.io.Closeable
2525
* that subscribe for the elements using [openSubscription] function and unsubscribe using [SubscriptionReceiveChannel.close]
2626
* function.
2727
*
28-
* See [BroadcastChannel()][BroadcastChannel.invoke] factory function for the description of available
28+
* See `BroadcastChannel()` factory function for the description of available
2929
* broadcast channel implementations.
3030
*/
3131
public interface BroadcastChannel<E> : SendChannel<E> {
3232
/**
3333
* Factory for broadcast channels.
34+
* @suppress **Deprecated**
3435
*/
3536
public companion object Factory {
3637
/**
3738
* Creates a broadcast channel with the specified buffer capacity.
38-
*
39-
* The resulting channel type depends on the specified [capacity] parameter:
40-
* * when `capacity` positive, but less than [UNLIMITED] -- creates [ArrayBroadcastChannel];
41-
* * when `capacity` is [CONFLATED] -- creates [ConflatedBroadcastChannel] that conflates back-to-back sends;
42-
* * otherwise -- throws [IllegalArgumentException].
39+
* @suppress **Deprecated**
4340
*/
44-
public operator fun <E> invoke(capacity: Int): BroadcastChannel<E> =
45-
when (capacity) {
46-
0 -> throw IllegalArgumentException("Unsupported 0 capacity for BroadcastChannel")
47-
UNLIMITED -> throw IllegalArgumentException("Unsupported UNLIMITED capacity for BroadcastChannel")
48-
CONFLATED -> ConflatedBroadcastChannel()
49-
else -> ArrayBroadcastChannel(capacity)
50-
}
41+
@Deprecated("Replaced with top-level function", level = DeprecationLevel.HIDDEN)
42+
public operator fun <E> invoke(capacity: Int): BroadcastChannel<E> = BroadcastChannel(capacity)
5143
}
5244

5345
/**
@@ -65,6 +57,22 @@ public interface BroadcastChannel<E> : SendChannel<E> {
6557
public fun open(): SubscriptionReceiveChannel<E> = openSubscription()
6658
}
6759

60+
/**
61+
* Creates a broadcast channel with the specified buffer capacity.
62+
*
63+
* The resulting channel type depends on the specified [capacity] parameter:
64+
* * when `capacity` positive, but less than [UNLIMITED] -- creates [ArrayBroadcastChannel];
65+
* * when `capacity` is [CONFLATED] -- creates [ConflatedBroadcastChannel] that conflates back-to-back sends;
66+
* * otherwise -- throws [IllegalArgumentException].
67+
*/
68+
public fun <E> BroadcastChannel(capacity: Int): BroadcastChannel<E> =
69+
when (capacity) {
70+
0 -> throw IllegalArgumentException("Unsupported 0 capacity for BroadcastChannel")
71+
UNLIMITED -> throw IllegalArgumentException("Unsupported UNLIMITED capacity for BroadcastChannel")
72+
CONFLATED -> ConflatedBroadcastChannel()
73+
else -> ArrayBroadcastChannel(capacity)
74+
}
75+
6876
/**
6977
* Return type for [BroadcastChannel.openSubscription] that can be used to [receive] elements from the
7078
* open subscription and to [close] it to unsubscribe.

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/ConflatedBroadcastChannel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import java.util.concurrent.atomic.AtomicReferenceFieldUpdater
3131
* Sender to this broadcast channel never suspends and [offer] always returns `true`.
3232
*
3333
* A secondary constructor can be used to create an instance of this class that already holds a value.
34+
* This channel is also created by `BroadcastChannel(Channel.CONFLATED)` factory function invocation.
3435
*
3536
* This implementation is fully lock-free. In this implementation
3637
* [opening][openSubscription] and [closing][SubscriptionReceiveChannel.close] subscription takes O(N) time, where N is the

0 commit comments

Comments
 (0)