From aaf1d56edb5c54fdc9291d9c368760fb9ebccab0 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sun, 13 Sep 2015 19:01:58 +0200 Subject: [PATCH] More work on monitor --- src/reagent/ratom.cljs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 863efc4..95273f2 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -109,31 +109,47 @@ (defonce cached-reactions {}) -(defn- cached-reaction [obj key f] - (if-some [r (get cached-reactions [key])] - r +(defn- cached-reaction [f key obj] + (if-some [r (get cached-reactions key)] + (-deref r) (if (some? *ratom-context*) (let [r (make-reaction - f :on-dispose #(set! cached-reactions - (dissoc cached-reactions key))) + f :on-dispose (fn [] + (set! cached-reactions + (dissoc cached-reactions key)) + (set! (.-reaction obj) nil))) v (-deref r)] - (set! cached-reactions (assoc cached-reactions key f)) + (set! cached-reactions (assoc cached-reactions key r)) (set! (.-reaction obj) r) v) (f)))) -(deftype Monitor [f args ^:mutable reaction] +(deftype Monitor [f key ^:mutable reaction] IReactiveAtom IDeref (-deref [this] (if-some [r reaction] (-deref r) - (cached-reaction this [f args] - #(apply f args))))) + (cached-reaction f key this))) + + IEquiv + (-equiv [o other] + (and (instance? Monitor other) + (= key (.-key other)))) + + IHash + (-hash [this] (hash key)) + + IPrintWithWriter + (-pr-writer [a writer opts] + (-write writer (str "#"))) (defn monitor [f & args] - (Monitor. f args nil)) + (Monitor. #(apply f args) [f args] nil)) ;;; cursor