mirror of https://github.com/status-im/reagent.git
Render without allocating ratom unless necessary
This commit is contained in:
parent
51ac2be955
commit
8f1c02b273
|
@ -128,16 +128,22 @@
|
||||||
(do-render C))
|
(do-render C))
|
||||||
res)))))
|
res)))))
|
||||||
|
|
||||||
|
(defn run-reactively [C run on-dirty]
|
||||||
|
(let [rat (.-cljsRatom C)]
|
||||||
|
(if (nil? rat)
|
||||||
|
(let [res (ratom/capture-derefed run C)
|
||||||
|
derefed (.-captured C)]
|
||||||
|
(when (and (not (nil? derefed))
|
||||||
|
tmpl/isClient)
|
||||||
|
(set! (.-cljsRatom C)
|
||||||
|
(ratom/make-reaction run
|
||||||
|
:auto-run on-dirty
|
||||||
|
:derefed derefed)))
|
||||||
|
res)
|
||||||
|
(ratom/run rat))))
|
||||||
|
|
||||||
(defn reactive-render [C]
|
(defn reactive-render [C]
|
||||||
(assert C)
|
(run-reactively C #(do-render C) #(queue-render C)))
|
||||||
(when (nil? (.-cljsRatom C))
|
|
||||||
(set! (.-cljsRatom C)
|
|
||||||
(ratom/make-reaction
|
|
||||||
#(do-render C)
|
|
||||||
:auto-run (if tmpl/isClient
|
|
||||||
#(queue-render C)
|
|
||||||
identity))))
|
|
||||||
(ratom/run (.-cljsRatom C)))
|
|
||||||
|
|
||||||
|
|
||||||
;;; Function wrapping
|
;;; Function wrapping
|
||||||
|
@ -179,7 +185,9 @@
|
||||||
|
|
||||||
:componentWillUnmount
|
:componentWillUnmount
|
||||||
(fn [C]
|
(fn [C]
|
||||||
(ratom/dispose! (.-cljsRatom C))
|
(let [ratom (.-cljsRatom C)]
|
||||||
|
(if-not (nil? ratom)
|
||||||
|
(ratom/dispose! ratom)))
|
||||||
(set! (.-cljsIsDirty C) false)
|
(set! (.-cljsIsDirty C) false)
|
||||||
(when f (f C)))
|
(when f (f C)))
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,13 @@
|
||||||
|
|
||||||
(declare ^:dynamic *ratom-context*)
|
(declare ^:dynamic *ratom-context*)
|
||||||
|
|
||||||
|
(def debug false)
|
||||||
|
|
||||||
(def -running (clojure.core/atom 0))
|
(def -running (clojure.core/atom 0))
|
||||||
|
|
||||||
(defn running [] @-running)
|
(defn running [] @-running)
|
||||||
|
|
||||||
(defn- capture-derefed [f obj]
|
(defn capture-derefed [f obj]
|
||||||
(set! (.-captured obj) nil)
|
(set! (.-captured obj) nil)
|
||||||
(binding [*ratom-context* obj]
|
(binding [*ratom-context* obj]
|
||||||
(f)))
|
(f)))
|
||||||
|
@ -115,7 +117,7 @@
|
||||||
(when (not= derefed watching)
|
(when (not= derefed watching)
|
||||||
(-update-watching this derefed))
|
(-update-watching this derefed))
|
||||||
(when-not active?
|
(when-not active?
|
||||||
(swap! -running inc)
|
(when debug (swap! -running inc))
|
||||||
(set! active? true))
|
(set! active? true))
|
||||||
(set! dirty? false)
|
(set! dirty? false)
|
||||||
(set! state res)
|
(set! state res)
|
||||||
|
@ -142,7 +144,7 @@
|
||||||
(set! state nil)
|
(set! state nil)
|
||||||
(set! dirty? true)
|
(set! dirty? true)
|
||||||
(when active?
|
(when active?
|
||||||
(swap! -running dec)
|
(when debug (swap! -running dec))
|
||||||
(set! active? false))
|
(set! active? false))
|
||||||
(when on-dispose
|
(when on-dispose
|
||||||
(on-dispose)))
|
(on-dispose)))
|
||||||
|
@ -159,8 +161,14 @@
|
||||||
IHash
|
IHash
|
||||||
(-hash [this] (goog/getUid this)))
|
(-hash [this] (goog/getUid this)))
|
||||||
|
|
||||||
(defn make-reaction [f & {:keys [auto-run on-set on-dispose]}]
|
(defn make-reaction [f & {:keys [auto-run on-set on-dispose derefed]}]
|
||||||
(let [runner (if (= auto-run true) run auto-run)]
|
(let [runner (if (= auto-run true) run auto-run)
|
||||||
(Reaction. f nil true false
|
active (not (nil? derefed))
|
||||||
nil {}
|
dirty (not active)
|
||||||
runner on-set on-dispose)))
|
reaction (Reaction. f nil dirty active
|
||||||
|
nil {}
|
||||||
|
runner on-set on-dispose)]
|
||||||
|
(when-not (nil? derefed)
|
||||||
|
(when debug (swap! -running inc))
|
||||||
|
(-update-watching reaction derefed))
|
||||||
|
reaction))
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
(:require [cemerick.cljs.test :as t]
|
(:require [cemerick.cljs.test :as t]
|
||||||
[reagent.ratom :as rv]))
|
[reagent.ratom :as rv]))
|
||||||
|
|
||||||
|
(set! rv/debug true)
|
||||||
|
|
||||||
(defn running [] (rv/running))
|
(defn running [] (rv/running))
|
||||||
(defn dispose [v] (rv/dispose! v))
|
(defn dispose [v] (rv/dispose! v))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue