Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active November 28, 2022 06:53
Show Gist options
  • Save manuelvicnt/127d88744c7f265328df9998c8d92a3b to your computer and use it in GitHub Desktop.
Save manuelvicnt/127d88744c7f265328df9998c8d92a3b 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