reduxkotlin/redux-kotlin-compose

Compose is skipping state

katajona opened this issue · 1 comments

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

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