Stefan Oltmann
05/06/2024, 9:31 AMAppState
is part of a redux pattern and will be copied often using appState.copy()
Only when the input properties change this needs to be calculated again.
Just like remember(allPhotos, openSimilarityGroups) { }
in a Composable context.
Here I don't have that.
( Previously I calculated the new list in my AppStore
, but there are so many places this is necessary that it got confusing. So I want the AppState to always have a valid value without this being set from the outside. )
ChatGPT's solution is using a mutableMap, but that thing will of course grow.
I feel there must be something neat built into Kotlin that I don't find right now.
data class AppState(
val allPhotos: ImmutableList<Photo> = persistentListOf(),
val openSimilarityGroups: ImmutableSet<Int> = persistentSetOf()
// .. lots of other properties ...
) : State {
val allPhotosCollapsed: ImmutableList<Photo> by lazy {
createCollapsed(allPhotos, openSimilarityGroups)
}
// ... other computed properties ...
companion object {
private fun createCollapsed(allPhotos: List<Photo>, openedSimilarityGroups: Set<Int>): ImmutableList<Photo> {
// TODO should return remembered result list if parameters did not change.
// should be thread safe and only remember one result
return resultOfExpensiveCalculation
}
}
}
Lukáš Kúšik
05/06/2024, 11:08 AMTim McCormack
05/06/2024, 11:37 AM@functools.lru_cache
. You'll likely need a library (or just write a version with a global mutable map and some concurrency-safe access to it.) Just make sure to consider whether you want cache eviction in your solution.Stefan Oltmann
05/06/2024, 11:44 AMStefan Oltmann
05/06/2024, 11:54 AMephemient
05/06/2024, 1:11 PMStefan Oltmann
05/06/2024, 2:03 PM