From 9a2d632826e5147be6ffe74e06be86f52b56593d Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sun, 4 Oct 2015 11:27:47 +0200 Subject: [PATCH] Refactor to avoid circular dependency in ratom --- src/reagent/impl/batching.cljs | 50 +++------------------------------ src/reagent/impl/component.cljs | 23 ++++++++++----- src/reagent/ratom.cljs | 31 ++++++++++++++++++-- 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/reagent/impl/batching.cljs b/src/reagent/impl/batching.cljs index 0f628e6..6000394 100644 --- a/src/reagent/impl/batching.cljs +++ b/src/reagent/impl/batching.cljs @@ -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)) diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 1e1364a..4ff7a8c 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -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)))) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 617a60c..4cd6cc5 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -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))