kotlinx.coroutines.experimental.channels.BroadcastChannel.kt Maven / Gradle / Ivy
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlinx.coroutines.experimental.channels
import kotlinx.coroutines.experimental.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.experimental.channels.Channel.Factory.UNLIMITED
import java.io.Closeable
/**
* Broadcast channel is a non-blocking primitive for communication between the sender and multiple receivers
* that subscribe for the elements using [openSubscription] function and unsubscribe using [SubscriptionReceiveChannel.close]
* function.
*
* See `BroadcastChannel()` factory function for the description of available
* broadcast channel implementations.
*/
public interface BroadcastChannel : SendChannel {
/**
* Factory for broadcast channels.
* @suppress **Deprecated**
*/
public companion object Factory {
/**
* Creates a broadcast channel with the specified buffer capacity.
* @suppress **Deprecated**
*/
@Deprecated("Replaced with top-level function", level = DeprecationLevel.HIDDEN)
public operator fun invoke(capacity: Int): BroadcastChannel = BroadcastChannel(capacity)
}
/**
* Subscribes to this [BroadcastChannel] and returns a channel to receive elements from it.
* The resulting channel shall be [closed][SubscriptionReceiveChannel.close] to unsubscribe from this
* broadcast channel.
*/
public fun openSubscription(): SubscriptionReceiveChannel
/**
* @suppress **Deprecated**: Renamed to [openSubscription]
*/
@Deprecated(message = "Renamed to `openSubscription`",
replaceWith = ReplaceWith("openSubscription()"))
public fun open(): SubscriptionReceiveChannel = openSubscription()
}
/**
* Creates a broadcast channel with the specified buffer capacity.
*
* The resulting channel type depends on the specified [capacity] parameter:
* * when `capacity` positive, but less than [UNLIMITED] -- creates [ArrayBroadcastChannel];
* * when `capacity` is [CONFLATED] -- creates [ConflatedBroadcastChannel] that conflates back-to-back sends;
* * otherwise -- throws [IllegalArgumentException].
*/
public fun BroadcastChannel(capacity: Int): BroadcastChannel =
when (capacity) {
0 -> throw IllegalArgumentException("Unsupported 0 capacity for BroadcastChannel")
UNLIMITED -> throw IllegalArgumentException("Unsupported UNLIMITED capacity for BroadcastChannel")
CONFLATED -> ConflatedBroadcastChannel()
else -> ArrayBroadcastChannel(capacity)
}
/**
* Return type for [BroadcastChannel.openSubscription] that can be used to [receive] elements from the
* open subscription and to [close] it to unsubscribe.
*
* Note, that invocation of [cancel] also closes subscription.
*/
public interface SubscriptionReceiveChannel : ReceiveChannel, Closeable {
/**
* Closes this subscription. This is a synonym for [cancel].
*/
public override fun close() { cancel() }
}