Hi! I have an inline function that is binding the ...
# coroutines
a
Hi! I have an inline function that is binding the state to a lifecycle owner.
Copy code
inline fun <A> Flow<A>.bindState(
    lifecycleOwner: LifecycleOwner,
    crossinline bind: suspend (A) -> Unit,
) =
    lifecycleOwner.lifecycleScope.launchWhenStarted {
        collect(bind)
    }
Recently I've updated the coroutines library to 1.6.4 (from 1.5.2) and looks like
Flow.collect(crossinline action: suspend (value: T) -> Unit)
doesn't work with the newer version of coroutines dependencies.
Flow.collect()
now takes a
FlowCollector
functional interface argument instead of a
suspend (T) -> Unit
. Any suggestions of how I can still keep this as an inline function?
This is what I was thinking of, but not sure if that's the best approach:
Copy code
inline fun <A> Flow<A>.bindState(
    lifecycleOwner: LifecycleOwner,
    crossinline action: suspend (A) -> Unit,
) =
    lifecycleOwner.lifecycleScope.launchWhenStarted {
        collect(object : FlowCollector<A> {
            override suspend fun emit(value: A) = action(value)
        })
    }
s
This is interesting, I hadn’t spotted this had changed! I have often noticed weirdness when trying to import the right
collect
function, though 😞.
Since it’s a fun interface,
FlowCollector(action)
should work instead of the anonymous object