Access to the state from within the reduction-observable epic

I use redux to create a small fusball manager. This is basically a page that shows the score of both teams and some buttons to enlarge them. I have actions such as { type:"GOAL_UP", team:"red" }for which the gearbox will change the corresponding value (rating) in the state.

Now I want to add an epic that whenever he encounters GOAL_UP, checks to see if one of the two teams won. If the team has reached 8 points, I want her to send the action GAME_WON. My problem is that I have to get access to the state in order to find out the score of each team. I cannot find a way to access the state from the epic.

Is access to state out of scope for contraction? If so, how would you solve this problem?

I think that a new account as a property in action GOAL_UP. Something like that { type:"GOAL_UP", team:"red", newScore:8 }. But somehow it seems wrong. I also have two physical buttons on the fusball table that are associated with web sockets that send the action GOAL_UP. Since they are not called from my ui, they are not aware of the condition.

I could also think of sending an action GAME_WONto the rendering function of my ui component if the evaluation of each of the commands is 8. But this is even worse :)

Entrance is very welcome, thanks

+4
source share
1

, .

, , store.getState().

, , , ( ).

const INCREMENT = 'INCREMENT';
const INCREMENT_IF_ODD = 'INCREMENT_IF_ODD';

const increment = () => ({ type: INCREMENT });
const incrementIfOdd = () => ({ type: INCREMENT_IF_ODD });

const incrementIfOddEpic = (action$, store) =>
  action$.ofType(INCREMENT_IF_ODD)
    .filter(() => store.getState().counter % 2 === 0)
    .map(increment);

// later...
dispatch(incrementIfOdd());

, , , , , - , - . , , , ! , , .

+9

Source: https://habr.com/ru/post/1662554/


All Articles