-
-
Save kahunamoore/c0707d84a390fb5f2e0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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