public class BroadcastKt
Modifier and Type | Method and Description |
---|---|
static <E> BroadcastChannel<E> |
broadcast(ReceiveChannel<? extends E> $receiver,
int capacity,
CoroutineStart start)
Broadcasts all elements of the channel.
|
static <E> BroadcastChannel<E> |
broadcast(CoroutineScope $receiver,
NonExistentClass context,
int capacity,
CoroutineStart start,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,kotlin.Unit> onCompletion,
kotlin.jvm.functions.Function2<? super kotlinx.coroutines.channels.ProducerScope<? super E>,? super kotlin.coroutines.experimental.Continuation<? super kotlin.Unit>,? extends java.lang.Object> block)
Launches new coroutine to produce a stream of values by sending them to a broadcast channel
and returns a reference to the coroutine as a
interface BroadcastChannel . The resulting
object can be used to BroadcastChannel.openSubscription to elements produced by this coroutine. |
public static <E> BroadcastChannel<E> broadcast(ReceiveChannel<? extends E> $receiver, int capacity, CoroutineStart start)
Broadcasts all elements of the channel.
capacity
- capacity of the channel's buffer (1 by default).start
- coroutine start option. The default value is CoroutineStart.LAZY.public static <E> BroadcastChannel<E> broadcast(CoroutineScope $receiver, NonExistentClass context, int capacity, CoroutineStart start, kotlin.jvm.functions.Function1<? super java.lang.Throwable,kotlin.Unit> onCompletion, kotlin.jvm.functions.Function2<? super kotlinx.coroutines.channels.ProducerScope<? super E>,? super kotlin.coroutines.experimental.Continuation<? super kotlin.Unit>,? extends java.lang.Object> block)
Launches new coroutine to produce a stream of values by sending them to a broadcast channel
and returns a reference to the coroutine as a interface BroadcastChannel
. The resulting
object can be used to BroadcastChannel.openSubscription
to elements produced by this coroutine.
The scope of the coroutine contains interface ProducerScope
interface, which implements
both interface CoroutineScope
and interface SendChannel
, so that coroutine can invoke
SendChannel.send
directly. The channel is SendChannel.close
when the coroutine completes.
Coroutine context is inherited from a interface CoroutineScope
, additional context elements can be specified with context argument.
If the context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.getDefault
is used.
The parent job is inherited from a interface CoroutineScope
as well, but it can also be overridden
with corresponding coroutineContext element.
Uncaught exceptions in this coroutine close the channel with this exception as a cause and the resulting channel becomes failed, so that any attempt to receive from such a channel throws exception.
The kind of the resulting channel depends on the specified capacity parameter:
when capacity
positive (1 by default), but less than UNLIMITED -- uses ArrayBroadcastChannel
with a buffer of given capacity,
when capacity
is CONFLATED -- uses class ConflatedBroadcastChannel
that conflates back-to-back sends;
otherwise -- throws IllegalArgumentException.
Note: By default, the coroutine does not start until the first subscriber appears via BroadcastChannel.openSubscription
as start parameter has a value of CoroutineStart.LAZY by default.
This ensures that the first subscriber does not miss any sent elements.
However, later subscribers may miss elements.
See newCoroutineContext for a description of debugging facilities that are available for newly created coroutine.
context
- additional to CoroutineScope.getCoroutineContext
context of the coroutine.capacity
- capacity of the channel's buffer (1 by default).start
- coroutine start option. The default value is CoroutineStart.LAZY.onCompletion
- optional completion handler for the producer coroutine (see Job.invokeOnCompletion
).block
- the coroutine code.interface BroadcastChannel
,
BroadcastChannel.openSubscription
,
interface ProducerScope
,
interface CoroutineScope
,
interface SendChannel
,
SendChannel.send
,
SendChannel.close
,
interface CoroutineScope
,
ContinuationInterceptor,
Dispatchers.getDefault
,
interface CoroutineScope
,
coroutineContext,
class ConflatedBroadcastChannel
,
IllegalArgumentException,
BroadcastChannel.openSubscription