What to do if two Flux stores need to depend on each other

One of the goals of Flux is to make the application more predictable by reducing crazy messy dependencies. With the help of the Manager, you can determine the strict procedure for updating the Stores. This creates a good hierarchy of tree hierarchy. This is a theory. Consider the following situation:

I have a game. A store located at the top of the hierarchy, StateStore , which contains only the current state of the game, i. e. play, pause, end. It is updated using actions such as PAUSE or RESUME . All other stores depend on this. Therefore, when the store processes some update action (for example, MOVE_LEFT ), it first checks the StateStore, and if the game is paused or completed, it ignores the action.

Now let's say that there is an action that will trigger the game. He updates some stores, and the store decides that the game should not continue ("the character of the game moves to the left and falls into the trap"). Thus, the state in the StateStore must change to the end. How to do it?

Theoretically, it should look like this:
  • This store is updated first and reaches the point of the game on top
  • Then the StateStore is updated (it is waiting for another store), checks another store and switches the state to more.

Unfortunately, another store also needs to access the StateStore to check the current state of the game, to see if it needs to be updated at all (that is, the game does not pause). They clearly depend on each other.

Possible solutions:

  • Combine these stores into one store. This will likely cause my entire application to crash into one store, which will raise the question of whether Flux is a good idea in this case.
  • Distinguish update order settings and read-only accessibility. All stores will be updated in strict order, but they can read from each other at random. Thus, the StateStore for each action checks all existing stores, and if any of them points to a game, this will change the state to more, effectively preventing the update of all other stores.

What do you think?

+6
source share
2 answers

In Flux, stores should be as independent of each other as possible and should not read from each other. The only way to change your condition is through action.

In your case, if any store decided that the game was over, you should update the StateStore from ActionCreator. You can do this by calling HaltGameActionCreator from the store or by sending the HALT_GAME action from ActionCreator, which caused the repository change in the first place.

+2
source

For those who have the same problem, you can read here about the actual application that I came across and how I approached it. In short, I allowed all the stores to read randomly from each other (proposed solution No. 2).

Note that ES6 modules allow you to use circular dependencies to simplify implementation.

However, looking back, I am not sure if this was the right decision. If part of the business logic inherently contains a circular dependency, we should not try to apply a solution that does not actually support it just because someone says so. Flux is just one template, there are many other ways to structure your code. Therefore, perhaps I would recommend folding all the logic into one store and using one of the other ways to implement the store itself (for example, standard OOP methods).

I would also consider using redux with reselect instead of Flux. The problem with the original example is related to the StateStore, which depends on two different inputs. It can be changed either by the user explicitly pausing / resuming the game, or by the game situation reaching the game. The advantage of this approach is that you only need to check one store to get the current state of the game.

With reduction / reselection, you will have one pause / resume operation and another gear controlling the game situation. Then you will have a selector combining these two pieces of information into the final state of the game. Most of the business logic will be transferred from stores to action creators, i.e. In the action creator moveLeft() , you must use this selector to check the state of the game, and only then will you send the MOVE_LEFT action.

Please note that this is just an approximate idea, and I do not know if it is viable.

+1
source

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


All Articles