1
\$\begingroup\$

Where should you store state and how should it be managed in a ClojureScript application? Take the following code for example - it's a "game" where you travel either down or left.

(ns cljsfiddle)
; initialise the state
(set! (.-x js/window) 0) 
(set! (.-y js/window) 0)
; state modifiers
(defn inc-x! [value] (set! (.-x js/window) (inc (.-x js/window)))) 
(defn inc-y! [value] (set! (.-y js/window) (inc (.-y js/window))))
(.addEventListener js/window "keyup" 
 (fn [event]
 (let [key-code (.-which event)]
 (cond
 (= key-code 49) (inc-x!)
 (= key-code 50) (inc-y!)))
 (print (.-x js/window) (.-y js/window))))

I'm transitioning to ClojureScript from JavaScript and I realize this is written in a fairly JavaScript-y way. In addition, any advice on how to rewrite this in a more Clojure-esque way is welcomed.

asked Apr 20, 2015 at 14:43
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

State management is a pretty complex topic currently in the cljs-sphere. Not because it is so hard to do, but because people are still trying to figure out what the simplest approach to it might be. And this causes some very interesting solutions.

However, as you are new I will start with the simplest and most accepted one. Use an atom.

;initialize state
(def xy-atom (atom {:x 0 :y 0}))
(defn inc-x! [i]
 (swap! xy-atom update-in [:x] + i))
(defn inc-y! [i]
 (swap! xy-atom update-in [:y] + i))
; or use a method to update any of the keys
(defn inc-any! [key i]
 (swap! xy-atom update-in [key] + i))

Your addEventListener function is ok, just change:

 (print (.-x js/window) (.-y js/window))

to

(print (:x @xy-atom) (:y @xy-atom)) ;or
(print @xy-atom)

If you're curious about more sophisticated approaches to handling state in cljs look here:

Both of them are very interesting, just from the concept point of view. However, to get started, take it slow and play around with the atom thing.

answered Apr 23, 2015 at 13:23
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.