Skip to content

Instantly share code, notes, and snippets.

@belinwu
Forked from manuelvicnt/LaunchAndCollectIn.kt
Created November 28, 2022 06:53
Show Gist options
  • Save belinwu/f7f03cae52bc9f3cc3ef19571ba27a6a to your computer and use it in GitHub Desktop.
Save belinwu/f7f03cae52bc9f3cc3ef19571ba27a6a to your computer and use it in GitHub Desktop.
/* Copyright 2022 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
inline fun <T> Flow<T>.launchAndCollectIn(
owner: LifecycleOwner,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
crossinline action: suspend CoroutineScope.(T) -> Unit
) = owner.lifecycleScope.launch {
owner.repeatOnLifecycle(minActiveState) {
collect {
action(it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment