Skip to content

Commit 8ca5296

Browse files
authored
Make SharingStarted a fun interface (#2397)
It was a part of the original design but was forgotten because the prototype was developeв before Kotlin 1.4.0. It makes implementing custom SharingStarted strategies more concise.
1 parent 598b861 commit 8ca5296

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

kotlinx-coroutines-core/common/src/flow/SharingStarted.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@ public enum class SharingCommand {
3838
/**
3939
* A strategy for starting and stopping the sharing coroutine in [shareIn] and [stateIn] operators.
4040
*
41-
* This interface provides a set of built-in strategies: [Eagerly], [Lazily], [WhileSubscribed], and
41+
* This functional interface provides a set of built-in strategies: [Eagerly], [Lazily], [WhileSubscribed], and
4242
* supports custom strategies by implementing this interface's [command] function.
4343
*
4444
* For example, it is possible to define a custom strategy that starts the upstream only when the number
4545
* of subscribers exceeds the given `threshold` and make it an extension on [SharingStarted.Companion] so
4646
* that it looks like a built-in strategy on the use-site:
4747
*
4848
* ```
49-
* fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int): SharingStarted =
50-
* object : SharingStarted {
51-
* override fun command(subscriptionCount: StateFlow<Int>): Flow<SharingCommand> =
52-
* subscriptionCount
53-
* .map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
49+
* fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int) =
50+
* SharingStarted { subscriptionCount: StateFlow<Int> ->
51+
* subscriptionCount.map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
5452
* }
5553
* ```
5654
*
@@ -74,7 +72,7 @@ public enum class SharingCommand {
7472
* The completion of the `command` flow normally has no effect (the upstream flow keeps running if it was running).
7573
* The failure of the `command` flow cancels the sharing coroutine and the upstream flow.
7674
*/
77-
public interface SharingStarted {
75+
public fun interface SharingStarted {
7876
public companion object {
7977
/**
8078
* Sharing is started immediately and never stops.

kotlinx-coroutines-core/common/test/flow/sharing/ShareInTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ class ShareInTest : TestBase() {
187187
}
188188

189189
@Suppress("TestFunctionName")
190-
private fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int): SharingStarted =
191-
object : SharingStarted {
192-
override fun command(subscriptionCount: StateFlow<Int>): Flow<SharingCommand> =
193-
subscriptionCount
194-
.map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
190+
private fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int) =
191+
SharingStarted { subscriptionCount ->
192+
subscriptionCount.map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
195193
}
196194

197195
private class FlowState {

0 commit comments

Comments
 (0)