Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.reactive |
| 6 | |
Francesco Vasco | e19ee04 | 2017-11-29 22:12:35 +0100 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.DefaultDispatcher |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.channels.ReceiveChannel |
| 9 | import org.reactivestreams.Publisher |
| 10 | import kotlin.coroutines.experimental.CoroutineContext |
| 11 | |
| 12 | /** |
| 13 | * Converts a stream of elements received from the channel to the hot reactive publisher. |
| 14 | * |
| 15 | * Every subscriber receives values from this channel in **fan-out** fashion. If the are multiple subscribers, |
| 16 | * they'll receive values in round-robin way. |
| 17 | * |
| 18 | * @param context -- the coroutine context from which the resulting observable is going to be signalled |
| 19 | */ |
Francesco Vasco | e19ee04 | 2017-11-29 22:12:35 +0100 | [diff] [blame] | 20 | public fun <T> ReceiveChannel<T>.asPublisher(context: CoroutineContext = DefaultDispatcher): Publisher<T> = publish(context) { |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 21 | for (t in this@asPublisher) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 22 | send(t) |
| 23 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * @suppress **Deprecated**: Renamed to [asPublisher] |
| 27 | */ |
| 28 | @Deprecated(message = "Renamed to `asPublisher`", |
| 29 | replaceWith = ReplaceWith("asPublisher(context)")) |
| 30 | public fun <T> ReceiveChannel<T>.toPublisher(context: CoroutineContext): Publisher<T> = asPublisher(context) |