reduxkotlin / redux-kotlin-compose

Compose Multiplatform integration for Redux-Kotlin
https://reduxkotlin.github.io/redux-kotlin-compose
29 stars 1 forks source link

Compose is skipping state #34

Closed katajona closed 1 year ago

katajona commented 1 year ago

When I trigger two dispatch functions one after anther the first state gets skipped. Here is an example:

data class State(val name: String = "test")
sealed interface Action {
    data class Rename(val name: String) : Action
}

val reducer: Reducer<State> = reducerForActionType<State, Action> { state, action ->
    when (action) {
        is Action.Rename -> state.copy(name = action.name)
    }
}

@Composable
fun App() {
    StoreProvider(createStore(reducer, State())) {
        Component()
    }
}

@Composable
fun Component() {
    val name by selectState<State, String> { this.name }
    val dispatch = rememberDispatcher()
    LaunchedEffect(name){
        println(name)
    }
    LaunchedEffect(Unit){
        dispatch(Action.Rename("a"))
        dispatch(Action.Rename("b"))
    }
    Text(text = name)
}

When running the following code I can see on the console the following:

I/System.out: test
I/System.out: b

What I would expect:

I/System.out: test
I/System.out: a
I/System.out: b

When further having a look into it and adding a delay(1) between the two dispatch events, I can get inconsistent outputs: Output 1:

I/System.out: test
I/System.out: b
I/System.out: b

Output 2:

I/System.out: test
I/System.out: a
I/System.out: b

Output 3:

I/System.out: test
I/System.out: b
mpetuska commented 1 year ago

This is expected behaviour for compose as multiple updates suring recomposition cycle are squashed to the latest before rendering.