Refactor to avoid circular dependency in ratom

This commit is contained in:
Dan Holmsand 2015-10-04 11:27:47 +02:00
parent 69e39bf6f7
commit 9a2d632826
3 changed files with 49 additions and 55 deletions

View File

@ -2,7 +2,6 @@
(:refer-clojure :exclude [flush])
(:require [reagent.debug :refer-macros [dbg]]
[reagent.interop :refer-macros [.' .!]]
[reagent.ratom :as ratom]
[reagent.impl.util :refer [is-client]]
[clojure.string :as string]))
@ -44,6 +43,9 @@
(dotimes [i (alength a)]
((aget a i))))
;; Set from ratom.cljs
(defonce ratom-flush identity)
(deftype RenderQueue [^:mutable queue ^:mutable ^boolean scheduled?
^:mutable after-render]
Object
@ -57,7 +59,7 @@
(set! scheduled? true)
(next-tick #(.run-queue this))))
(run-queue [_]
(ratom/flush!)
(ratom-flush)
(let [q queue
aq after-render]
(set! queue (array))
@ -87,47 +89,3 @@
(defn schedule []
(.schedule render-queue))
;; Render helper
(defn is-reagent-component [c]
(some-> c (.' :props) (.' :argv)))
(def rat-opts {:no-cache true})
(defn run-reactively [c run]
(assert (is-reagent-component c))
(mark-rendered c)
(let [rat (.' c :cljsRatom)]
(if (nil? rat)
(ratom/run-in-reaction run c "cljsRatom" queue-render rat-opts)
(._run rat))))
(defn dispose [c]
(some-> (.' c :cljsRatom)
ratom/dispose!)
(mark-rendered c))
(comment
(defn ratom-perf []
(dbg "ratom-perf")
(set! ratom/debug false)
(dotimes [_ 10]
(let [nite 100000
a (ratom/atom 0)
f (fn []
;; (ratom/with-let [x 1])
(quot @a 10))
mid (ratom/make-reaction f)
res (ratom/track! (fn []
;; @(ratom/track f)
(inc @mid)
))]
@res
(time (dotimes [x nite]
(swap! a inc)
(ratom/flush!)))
(ratom/dispose! res))))
(enable-console-print!)
(ratom-perf))

View File

@ -73,12 +73,19 @@
;;; Method wrapping
(def static-fns {:render
(fn []
(this-as c
(if-not *non-reactive*
(batch/run-reactively c #(do-render c))
(do-render c))))})
(def rat-opts {:no-cache true})
(def static-fns
{:render
(fn render []
(this-as c (if *non-reactive*
(do-render c)
(let [rat (.' c :cljsRatom)]
(batch/mark-rendered c)
(if (nil? rat)
(ratom/run-in-reaction #(do-render c) c "cljsRatom"
batch/queue-render rat-opts)
(._run rat))))))})
(defn custom-wrapper [key f]
(case key
@ -129,7 +136,9 @@
:componentWillUnmount
(fn []
(this-as c
(batch/dispose c)
(some-> (.' c :cljsRatom)
ratom/dispose!)
(batch/mark-rendered c)
(when-not (nil? f)
(f c))))

View File

@ -3,6 +3,7 @@
(:require-macros [reagent.ratom :refer [with-let]])
(:require [reagent.impl.util :as util]
[reagent.debug :refer-macros [dbg log warn error dev?]]
[reagent.impl.batching :as batch]
[clojure.set :as s]))
(declare ^:dynamic *ratom-context*)
@ -99,8 +100,7 @@
(defn- rea-enqueue [r]
(when (nil? rea-queue)
(set! rea-queue (array))
;; Get around ugly circular dependency. TODO: Fix.
(js/reagent.impl.batching.schedule))
(batch/schedule))
(.push rea-queue r))
(defn- run-queue [q]
@ -114,6 +114,7 @@
(binding [*ratom-context* empty-context]
(run-queue q))))
(set! batch/ratom-flush flush!)
;;; Atom
@ -555,3 +556,29 @@
(Wrapper. value
(util/partial-ifn. callback-fn args nil)
false nil))
(comment
(defn ratom-perf []
(dbg "ratom-perf")
(set! debug false)
(dotimes [_ 10]
(let [nite 100000
a (atom 0)
f (fn []
;; (ratom/with-let [x 1])
(quot @a 10))
mid (make-reaction f)
res (track! (fn []
;; @(ratom/track f)
(inc @mid)
))]
@res
(time (dotimes [x nite]
(swap! a inc)
(flush!)))
(dispose! res))))
(enable-console-print!)
(ratom-perf))