Skip to content

Instantly share code, notes, and snippets.

@takahirom
Last active January 30, 2025 07:47
Show Gist options
  • Save takahirom/5dbdb83a5796021811b49caf019e3036 to your computer and use it in GitHub Desktop.
Save takahirom/5dbdb83a5796021811b49caf019e3036 to your computer and use it in GitHub Desktop.
Solution for the "Combined StateFlow getValue Not Updated" Problem https://pl.kotl.in/eEOTmPYEI
/**
Copyright 2025 takahirom
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public inline fun <reified T, R> CoroutineScope.buildSingleSourceStateFlow(
sourceStateFlow: StateFlow<T>,
crossinline buildBlock: (T) -> R
): StateFlow<R> {
val getSourceValueArray: () -> Array<*> = {
arrayOf(sourceStateFlow.value)
}
val callBuildBlockByCurrentSource: () -> R = {
buildBlock(sourceStateFlow.value)
}
val convertedStateFlow = sourceStateFlow.map {
getSourceValueArray() to buildBlock(it)
}.stateIn(
this,
DefaultWhileSubscribed,
getSourceValueArray() to callBuildBlockByCurrentSource()
)
return buildStateFlow(
callBuildBlockByCurrentSource = callBuildBlockByCurrentSource,
getSourceValueArray = getSourceValueArray,
convertedStateFlow = convertedStateFlow
)
}
public inline fun <reified T1, T2, R> CoroutineScope.buildSingleSourceStateFlow(
sourceStateFlow1: StateFlow<T1>,
sourceStateFlow2: StateFlow<T2>,
crossinline buildBlock: (T1, T2) -> R
): StateFlow<R> {
val getSourceValueArray: () -> Array<*> = {
arrayOf(sourceStateFlow1.value, sourceStateFlow2.value)
}
val callBuildBlockByCurrentSource: () -> R = {
buildBlock(sourceStateFlow1.value, sourceStateFlow2.value)
}
val convertedStateFlow = combine(sourceStateFlow1, sourceStateFlow2) { s1, s2 ->
getSourceValueArray() to buildBlock(s1, s2)
}.stateIn(
this,
DefaultWhileSubscribed,
getSourceValueArray() to callBuildBlockByCurrentSource()
)
return buildStateFlow(
callBuildBlockByCurrentSource = callBuildBlockByCurrentSource,
getSourceValueArray = getSourceValueArray,
convertedStateFlow = convertedStateFlow
)
}
fun <R> buildStateFlow(
callBuildBlockByCurrentSource: () -> R,
getSourceValueArray: () -> Array<*>,
convertedStateFlow: StateFlow<Pair<Array<*>, R>>
): StateFlow<R> {
return object : StateFlow<R> {
override val replayCache: List<R>
get() {
return listOf(value)
}
override val value: R
get() {
val result = callBuildBlockByCurrentSource()
return result
}
override suspend fun collect(collector: FlowCollector<R>): Nothing {
if (!getSourceValueArray().contentDeepEquals(convertedStateFlow.value.first)) {
convertedStateFlow.first { it.first.contentDeepEquals(getSourceValueArray()) }
}
convertedStateFlow.collect {
collector.emit(it.second)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment