On Friday, April 3, 2015 at 6:36:03 AM UTC-4, Rafał Cieślak wrote: > I'm writing a tic-tac-toe game (hot seat, not over the Internet) using > re-frame and I'm wondering what's the best approach to handling certain > actions that depend on a certain transformations of state. > > A simple example, if a player clicks on a cell, I have to: > > A. Change the owner of that cell to the current player. > B. Change the current player, so that the next one can play. > > Let's say I dispatch the click as an event (obviously) and in its handler I > treat those two mentioned actions as events and dispatch them, too. > > If they both read the current player from the app-db, a certain coupling is > introduced: if I call B before calling A, the owner of the clicked cell is > going to be changed not to the player that clicked on the cell, but to the > next player. > > There are a few ways that I can deal with that: > > 1. Just dispatch them as separate events as I decribed and introduce the > forementioned coupling (I have to remember to call these events in a certain > order). > 2. In the cell click handler, read the current player from the db and pass > it as an argument to those two events. Now I don't have to worry about the > order in which they'll be called. > 3. Instead of calling A and B from within the cell click handler, call only > A in the handler and then call B from within A. I guess that's a pretty bad > solution (I just coupled B with A even more), but it also removes the worry > about the order of calls. > > Honestly, I came up with the second solution during the writing of this post > and I feel it's the most clean one, it feels the most functional from all > three and I guess it's the standard method of dealing with problems like this. > > Anyway, I'd like to hear your opinions, how the second solution may turn out > to be bad in the future, and maybe even get to know different ways to tackle > that problem. > > Also, I wonder what's the preferred approach to handling multiple operations > in a handler, because I can either dispatch many events from within it or > call a bunch of functions on the db in a -> macro, see > https://gist.github.com/ravicious/fb189aa45b7784624485
The way I do this in my connect four game is that the click handler will make a call to the <play> function. <play> takes in a game state and a column (or cell as the case may be for tic-tac-toe), and returns a new state with the <current player>'s token added to the cell, and the <current player> switched to the opposite player. -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/clojurescript.
