Lately, in Clojure Developers Barcelona's events, we've been practicing by doing several variations on the Lights Out Kata.
First, we did the kata in ClojureScript using Reagent and Figwheel (described in this previous post).
In the last two meetups (this one and this other one), we redid the kata.
This time instead of doing everything on the client,
we used the Compojure library to develop a back end service that reset and flipped the lights,
and a simple client that talked to it using cljs-http and core.async libraries.
As we've been doing lately, we did mob programming and REPL-driven development.
First, let's see the code we did for the server:
These are the tests:
and this is the definition of the routes and their handlers using Compojure:
We had some problems with the Same-origin policy until we discovered and learned how to use the Ring middleware for Cross-Origin Resource Sharing.
Finally, this is the code that flips and resets the lights (it's more or less the same code we wrote for the client-only version of the kata we did previously):
Now, let's see the code we wrote for the client:
First, the core namespace where everything is initialized (notice the channel creation on line 12):
Then the lights-view namespace which contains the code to render all the components:
The lights-view's code is also very similar to the one we did for the client-only version of the kata.
And finally the lights namespace which is in charge of talking to the back end and updating the lights atom:
Notice how we used the pipe function to take the values that were coming from the channel returned by the call to cljs-httphttps://github.com/r0man/cljs-http's post function and pass them to the channel from which the code that updates the lights state is taking values.
You can find the code we produced in these two GitHub repositories: the server and the client (see the flip-lights-in-backend branch).
As usual it was a great pleasure to do mob programming and learn with the members of Clojure Developers Barcelona.