Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created February 25, 2016 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/d7f947ed4dfb47848245 to your computer and use it in GitHub Desktop.
Save krisleech/d7f947ed4dfb47848245 to your computer and use it in GitHub Desktop.
Simple example of mapping events to handlers in Clojure

We can set the URL using a string (event dispatch). This will end up calling a function with the same name as the string (an event handler). The function handler can then update the state.

;; state
(def state (atom { :content "" }))
(def session (atom { :page "home" }))

;; handlers
;;
(defn about [] (clojure.core/swap! state assoc :content "About us"))
(defn home [] (clojure.core/swap! state assoc :content "Homepage"))

;; dispatcher
;;
(defn set-url [page] (clojure.core/swap! session assoc :page page))

;; router
;; this basically does something like `(resolve :about)`.
;;
(add-watch session :url-watcher (fn [_ _ _ new_state] ((resolve (symbol (new_state :page))))))

;; usage

@state
;; {:content ""}

(set-url "home")

@state
;; {:content "Homepage"}

(set-url "about")

@state
;; {:content "About us"}

We have two atoms to prevent an infinite loop, since the watcher would otherwise be called when the handlers update the :content key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment