Skip to content

Instantly share code, notes, and snippets.

@matthewrkula
Last active April 22, 2024 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewrkula/7700752b19e4cc547bd13edac5089869 to your computer and use it in GitHub Desktop.
Save matthewrkula/7700752b19e4cc547bd13edac5089869 to your computer and use it in GitHub Desktop.
object CancellableScope
interface CancellableUseCase {
context(CancellableScope)
suspend operator fun invoke(): Result
}
suspend fun <T> Flow<T>.collectLatestSafe(action: suspend context(CancellableScope) (value: T) -> Unit) {
collectLatest { item ->
with(CancellableScope) {
action(this, item)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment