Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SharingStarted a fun interface #2397

Merged
merged 1 commit into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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