From fdef5e6c9f9423f19eac1460435432a57c25c3cd Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Mon, 14 Sep 2015 15:03:39 +0200 Subject: [PATCH] Make cursor used cached reactions --- demo/reagentdemo/news/news050.cljs | 13 +++++++++++++ src/reagent/ratom.cljs | 15 ++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/demo/reagentdemo/news/news050.cljs b/demo/reagentdemo/news/news050.cljs index 0fbf016..895a0d8 100644 --- a/demo/reagentdemo/news/news050.cljs +++ b/demo/reagentdemo/news/news050.cljs @@ -29,10 +29,23 @@ [:input {:value @val :on-change #(reset! val (.-target.value %))}]]) +(defn name-part [key] + (get-in @person [:name key])) + +(def monitor reagent.ratom/monitor) + +(defn foo []) + (defn name-edit [n] (let [{:keys [first-name last-name]} @n] [:div [:p "I'm editing " first-name " " last-name "."] + [:p "I'm editing " @(monitor name-part :first-name) " " + @(monitor name-part :last-name) "."] + [:p "I'm editing " @(monitor name-part :first-name) " " + @(monitor name-part :last-name) "."] + [:p "I'm editing " @(monitor name-part :first-name) " " + @(monitor name-part :last-name) "."] [input "First name: " (r/wrap first-name swap! n assoc :first-name)] diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index c848c73..4d8552e 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -166,14 +166,6 @@ (= ratom (.-ratom other)))) Object - (_reaction [this] - (if (nil? reaction) - (set! reaction - (if (satisfies? IDeref ratom) - (make-reaction #(get-in @ratom path)) - (make-reaction #(ratom path)))) - reaction)) - (_peek [this] (binding [*ratom-context* nil] (-deref this))) @@ -187,7 +179,12 @@ IDeref (-deref [this] (let [oldstate state - newstate (-deref (._reaction this))] + newstate (if-some [r reaction] + (-deref r) + (let [f (if (satisfies? IDeref ratom) + #(get-in @ratom path) + #(ratom path))] + (cached-reaction f [ratom path ::cursor] this)))] (._set-state this oldstate newstate) newstate))