Skip to content

Instantly share code, notes, and snippets.

@itsusmon
Last active January 30, 2025 08:29
Show Gist options
  • Save itsusmon/960c3764834dcf2244852f8af83d205f to your computer and use it in GitHub Desktop.
Save itsusmon/960c3764834dcf2244852f8af83d205f to your computer and use it in GitHub Desktop.
/**
* Inspired by Jaewoong Eum's flow-operators library (https://github.com/skydoves/flow-operators).
*/
class Restartable {
private val restartFlow = MutableSharedFlow<SharingCommand>(extraBufferCapacity = 2)
fun restart() {
restartFlow.tryEmit(SharingCommand.STOP_AND_RESET_REPLAY_CACHE)
restartFlow.tryEmit(SharingCommand.START)
}
companion object {
@JvmStatic
fun SharingStarted.with(restartable: Restartable) = SharingStarted { subscriptionCount ->
merge(restartable.restartFlow, command(subscriptionCount))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment