Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Last active November 19, 2017 20:13
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganlinn/930c043331c52cb73a98 to your computer and use it in GitHub Desktop.
Save loganlinn/930c043331c52cb73a98 to your computer and use it in GitHub Desktop.
history.cljs
(ns history
"Light wrappers and utils for js/history")
(defn back! [] (.back js/history))
(defn forward! [] (.forward js/history))
(defn go! [idx] (.go js/history idx))
(defn replace-state!
([state]
(replace-state! state (.-title js/document)))
([state title]
(.replaceState js/history state title))
([state title path]
(.replaceState js/history state title path)))
(defn push-state!
([state title]
(.pushState js/history state title))
([state title path]
(.pushState js/history state title path)))
(defn current-state
"Returns current JS value of history.state"
[]
(.-state js/history))
(def state
(let [clj-state #(js->clj (.-state js/history) :keywordize-keys true)]
(reify
IDeref
(-deref [_]
(clj-state))
IReset
(-reset! [_ v]
(replace-state! (clj->js v)))
ISwap
(-swap! [s f]
(-reset! s (f (clj-state))))
(-swap! [s f x]
(-reset! s (f (clj-state) x)))
(-swap! [s f x y]
(-reset! s (f (clj-state) x y)))
(-swap! [s f x y more]
(-reset! s (apply f (clj-state) x y more))))))
@martinklepsch
Copy link

I really liked reading through this. It's remarkable how easy it is/seems to provide some idiomatic wrapper for Clojurescript around bare bones Javascript stuff like this! 👍

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