Skip to content

Commit

Permalink
Make SharingStarted a fun interface (#2397)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elizarov committed Nov 17, 2020
1 parent 598b861 commit 8ca5296
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
12 changes: 5 additions & 7 deletions kotlinx-coroutines-core/common/src/flow/SharingStarted.kt
Expand Up @@ -38,19 +38,17 @@ public enum class SharingCommand {
/**
* A strategy for starting and stopping the sharing coroutine in [shareIn] and [stateIn] operators.
*
* This interface provides a set of built-in strategies: [Eagerly], [Lazily], [WhileSubscribed], and
* This functional interface provides a set of built-in strategies: [Eagerly], [Lazily], [WhileSubscribed], and
* supports custom strategies by implementing this interface's [command] function.
*
* For example, it is possible to define a custom strategy that starts the upstream only when the number
* of subscribers exceeds the given `threshold` and make it an extension on [SharingStarted.Companion] so
* that it looks like a built-in strategy on the use-site:
*
* ```
* fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int): SharingStarted =
* object : SharingStarted {
* override fun command(subscriptionCount: StateFlow<Int>): Flow<SharingCommand> =
* subscriptionCount
* .map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
* fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int) =
* SharingStarted { subscriptionCount: StateFlow<Int> ->
* subscriptionCount.map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
* }
* ```
*
Expand All @@ -74,7 +72,7 @@ public enum class SharingCommand {
* The completion of the `command` flow normally has no effect (the upstream flow keeps running if it was running).
* The failure of the `command` flow cancels the sharing coroutine and the upstream flow.
*/
public interface SharingStarted {
public fun interface SharingStarted {
public companion object {
/**
* Sharing is started immediately and never stops.
Expand Down
Expand Up @@ -187,11 +187,9 @@ class ShareInTest : TestBase() {
}

@Suppress("TestFunctionName")
private fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int): SharingStarted =
object : SharingStarted {
override fun command(subscriptionCount: StateFlow<Int>): Flow<SharingCommand> =
subscriptionCount
.map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
private fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int) =
SharingStarted { subscriptionCount ->
subscriptionCount.map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP }
}

private class FlowState {
Expand Down

0 comments on commit 8ca5296

Please sign in to comment.