Skip to content

Instantly share code, notes, and snippets.

@Maartyl
Created October 15, 2020 15:40
Show Gist options
  • Save Maartyl/b40a07e6cb265dd9037a8d5745ebc0bd to your computer and use it in GitHub Desktop.
Save Maartyl/b40a07e6cb265dd9037a8d5745ebc0bd to your computer and use it in GitHub Desktop.
flow operator that adds pausing upstream
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Temporary, to not use it by accident", level = DeprecationLevel.ERROR)
@ExperimentalCoroutinesApi
fun <T> Flow<T>.pausedWhen(isPaused: StateFlow<Boolean>) = flow {
//WRONG! this impl changes context of a safe flow - violates invariant
// - would need to be specially, correctly passed through SafeFlow
withContext(mergePausing(currentCoroutineContext(), isPaused)) {
emitAll(this@pausedWhen)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment