Make rendering faster by avoiding apply in more places

This commit is contained in:
Dan Holmsand 2014-02-10 15:52:32 +01:00
parent 8f1c02b273
commit 2901ab6a32
1 changed files with 38 additions and 32 deletions

View File

@ -142,9 +142,6 @@
res) res)
(ratom/run rat)))) (ratom/run rat))))
(defn reactive-render [C]
(run-reactively C #(do-render C) #(queue-render C)))
;;; Function wrapping ;;; Function wrapping
@ -154,42 +151,48 @@
(assert false "getDefaultProps not supported yet") (assert false "getDefaultProps not supported yet")
:getInitialState :getInitialState
(fn [C] (fn []
(when f (this-as C
(aset C cljs-state (merge (state C) (f C))))) (when f
(aset C cljs-state (merge (state C) (f C))))))
:componentWillReceiveProps :componentWillReceiveProps
(fn [C props] (fn [props]
(when f (f C (aget props cljs-argv)))) (this-as C
(when f (f C (aget props cljs-argv)))))
:shouldComponentUpdate :shouldComponentUpdate
(fn [C nextprops nextstate] (fn [nextprops nextstate]
;; Don't care about nextstate here, we use forceUpdate (this-as C
;; when only when state has changed anyway. ;; Don't care about nextstate here, we use forceUpdate
(let [inprops (js-props C) ;; when only when state has changed anyway.
old-argv (aget inprops cljs-argv) (let [inprops (js-props C)
new-argv (aget nextprops cljs-argv)] old-argv (aget inprops cljs-argv)
(if (nil? f) new-argv (aget nextprops cljs-argv)]
(not (util/equal-args old-argv new-argv)) (if (nil? f)
(f C old-argv new-argv)))) (not (util/equal-args old-argv new-argv))
(f C old-argv new-argv)))))
:componentWillUpdate :componentWillUpdate
(fn [C nextprops] (fn [nextprops]
(let [next-argv (aget nextprops cljs-argv)] (this-as C
(f C next-argv))) (let [next-argv (aget nextprops cljs-argv)]
(f C next-argv))))
:componentDidUpdate :componentDidUpdate
(fn [C oldprops] (fn [oldprops]
(let [old-argv (aget oldprops cljs-argv)] (this-as C
(f C old-argv))) (let [old-argv (aget oldprops cljs-argv)]
(f C old-argv))))
:componentWillUnmount :componentWillUnmount
(fn [C] (fn []
(let [ratom (.-cljsRatom C)] (this-as C
(if-not (nil? ratom) (let [ratom (.-cljsRatom C)]
(ratom/dispose! ratom))) (if-not (nil? ratom)
(set! (.-cljsIsDirty C) false) (ratom/dispose! ratom)))
(when f (f C))) (set! (.-cljsIsDirty C) false)
(when f (f C))))
nil)) nil))
@ -199,7 +202,7 @@
(this-as C (apply f C args))) (this-as C (apply f C args)))
f)) f))
(def dont-wrap #{:cljsRender}) (def dont-wrap #{:cljsRender :render})
(defn get-wrapper [key f name] (defn get-wrapper [key f name]
(if (dont-wrap key) (if (dont-wrap key)
@ -209,7 +212,7 @@
(when (and wrap f) (when (and wrap f)
(assert (fn? f) (assert (fn? f)
(str "Expected function in " name key " but got " f))) (str "Expected function in " name key " but got " f)))
(default-wrapper (or wrap f))))) (or wrap (default-wrapper f)))))
(def obligatory {:shouldComponentUpdate nil (def obligatory {:shouldComponentUpdate nil
:componentWillUnmount nil}) :componentWillUnmount nil})
@ -224,7 +227,10 @@
(defn add-render [fun-map render-f] (defn add-render [fun-map render-f]
(assoc fun-map (assoc fun-map
:cljsRender render-f :cljsRender render-f
:render reactive-render)) :render (fn []
(this-as C
(run-reactively
C #(do-render C) #(queue-render C))))))
(defn wrap-funs [fun-map] (defn wrap-funs [fun-map]
(let [render-fun (or (:componentFunction fun-map) (let [render-fun (or (:componentFunction fun-map)