@@ -18,9 +18,9 @@ import kotlin.coroutines.*
18
18
* Transforms the given reactive [Publisher] into [Flow].
19
19
* Use [buffer] operator on the resulting flow to specify the size of the backpressure.
20
20
* More precisely, it specifies the value of the subscription's [request][Subscription.request].
21
- * `1` is used by default.
21
+ * [buffer] default capacity is used by default.
22
22
*
23
- * If any of the resulting flow transformations fails, subscription is immediately cancelled and all in-flights elements
23
+ * If any of the resulting flow transformations fails, subscription is immediately cancelled and all in-flight elements
24
24
* are discarded.
25
25
*
26
26
* This function is integrated with `ReactorContext` from `kotlinx-coroutines-reactor` module,
@@ -40,16 +40,23 @@ public fun <T : Any> Flow<T>.asPublisher(): Publisher<T> = FlowAsPublisher(this)
40
40
private class PublisherAsFlow <T : Any >(
41
41
private val publisher : Publisher <T >,
42
42
context : CoroutineContext = EmptyCoroutineContext ,
43
- capacity : Int = 1
43
+ capacity : Int = Channel . BUFFERED
44
44
) : ChannelFlow<T>(context, capacity) {
45
45
override fun create (context : CoroutineContext , capacity : Int ): ChannelFlow <T > =
46
46
PublisherAsFlow (publisher, context, capacity)
47
47
48
+ /*
49
+ * Suppress for Channel.CHANNEL_DEFAULT_CAPACITY.
50
+ * It's too counter-intuitive to be public and moving it to Flow companion
51
+ * will also create undesired effect.
52
+ */
53
+ @Suppress(" INVISIBLE_MEMBER" , " INVISIBLE_REFERENCE" )
48
54
private val requestSize: Long
49
55
get() = when (capacity) {
50
56
Channel .CONFLATED -> Long .MAX_VALUE // request all and conflate incoming
51
57
Channel .RENDEZVOUS -> 1L // need to request at least one anyway
52
58
Channel .UNLIMITED -> Long .MAX_VALUE // reactive streams way to say "give all" must be Long.MAX_VALUE
59
+ Channel .BUFFERED -> Channel .CHANNEL_DEFAULT_CAPACITY .toLong()
53
60
else -> capacity.toLong().also { check(it >= 1 ) }
54
61
}
55
62
0 commit comments