Skip to content

Instantly share code, notes, and snippets.

@kahunamoore
Forked from bhauman/cljs-react-reload.cljs
Last active August 29, 2015 14:24
Show Gist options
  • Save kahunamoore/c0707d84a390fb5f2e0a to your computer and use it in GitHub Desktop.
Save kahunamoore/c0707d84a390fb5f2e0a to your computer and use it in GitHub Desktop.
(ns cljs-react-reload.core
(:require
[cljs.env :as env]))
(defmacro def-react-class [vname body & {:keys [redefine]}]
(let [vname-proxy# (symbol (str vname "-proxied"))]
(if (and env/*compiler*
(let [{:keys [optimizations]} (get env/*compiler* :options)]
(or (nil? optimizations) (= optimizations :none))))
`(do
(.log js/console (str "Proxying a react class for live dev: " ~(name vname)))
(def ~vname-proxy# ~body)
(~(if redefine 'def 'defonce)
~vname
(let [base# (js/Object.)]
(aset base#
"shouldComponentUpdate"
(fn [next-props# next-state#]
(if (.. ~vname-proxy# -shouldComponentUpdate)
(cljs.core/this-as
this#
(.call (.. ~vname-proxy# -shouldComponentUpdate) this# next-props# next-state#))
true)))
(doseq [property# (map
name
'(componentWillReceiveProps
componentWillMount
componentDidMount
componentWillUpdate
componentDidUpdate
componentWillUnmount
render))]
(aset base#
property#
(fn [& args#]
(when (aget ~vname-proxy# property#)
(cljs.core/this-as
this#
(.apply (aget ~vname-proxy# property#)
this#
(to-array args#)))))))
;; static stuff that happens once on load
(doseq [property# (map name '(propTypes
mixins
statics
displayName
getInitialState
getDefaultProps))]
(when-let [v# (aget ~vname-proxy# property#)]
(aset base# property# v#)))
(js/React.createClass base#))))
`(defonce ~vname (js/React.createClass ~body)))))
(defmacro redef-react-class [vname body]
`(def-react-class ~vname ~body :redefine true))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment