piotr-oles/redux-detector

Detectors actions are dispatched recursively and not chronologically.

etienne-dldc opened this issue · 1 comments

Let say you have a counter

const initialState = 0

and you also have a detector

const detector = (prevCounter, nextCounter) => {
    // when counter == 1
    if (prevCounter !== nextCounter && nextCounter === 1) {
        return [incrementBy(1), multiplyBy(2)];
    }
    // when counter == 2
    if (prevCounter !== nextCounter && nextCounter === 2) {
        return incrementBy(1);
    }
}

Then you dispatch increment(1), what is the value of state.counter ?

Answer: it's 6 ! Because the order actions are dispatch is the following:

  • The first increment (0 -> 1),
  • The first action from detector counter === 1 (1 -> 2)
  • The action from detector counter === 2 (2 -> 3)
  • The second action from detector counter === 1 (3 -> 6)

I think this is a bit odd and unexpected (earlier actions should be dispatched first).
Expected answer would be 5 (increment, increment, multiply, increment).

Released as 0.7.0 :)