554593d39a
Some checks failed
suyu-ci / Check REUSE Specification (push) Has been cancelled
suyu verify / Verify Format (push) Has been cancelled
suyu verify / test build (linux-fresh, clang) (push) Has been cancelled
suyu verify / test build (linux-fresh, linux) (push) Has been cancelled
suyu verify / test build (linux-mingw, windows) (push) Has been cancelled
suyu verify / android (push) Has been cancelled
codespell / Check for spelling errors (push) Successful in 12s
Co-authored-by: palfaiate <syl.paulo.alfaiate@gmail.com> Co-authored-by: SylverDiscord51 <p71468162@gmail.com> Co-committed-by: SylverDiscord51 <p71468162@gmail.com>
39 lines
1.3 KiB
Kotlin
39 lines
1.3 KiB
Kotlin
// SPDX-FileCopyrightText: 2023 yuzu emulator project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
package org.suyu.suyu_emu.utils
|
|
|
|
import androidx.lifecycle.Lifecycle
|
|
import androidx.lifecycle.LifecycleOwner
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.lifecycle.repeatOnLifecycle
|
|
import kotlinx.coroutines.flow.Flow
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
import kotlinx.coroutines.launch
|
|
|
|
/**
|
|
* Collects this [Flow] with a given [LifecycleOwner].
|
|
* @param scope [LifecycleOwner] that this [Flow] will be collected with.
|
|
* @param repeatState When to repeat collection on this [Flow].
|
|
* @param resetState Optional lambda to reset state of an underlying [MutableStateFlow] after
|
|
* [stateCollector] has been run.
|
|
* @param stateCollector Lambda that receives new state.
|
|
*/
|
|
inline fun <reified T> Flow<T>.collect(
|
|
scope: LifecycleOwner,
|
|
repeatState: Lifecycle.State = Lifecycle.State.CREATED,
|
|
crossinline resetState: () -> Unit = {},
|
|
crossinline stateCollector: (state: T) -> Unit
|
|
) {
|
|
scope.apply {
|
|
lifecycleScope.launch {
|
|
repeatOnLifecycle(repeatState) {
|
|
this@collect.collect {
|
|
stateCollector(it)
|
|
resetState()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|