From 6e0e9ed5647ddc4dd041afaebdf6012ba5a911d2 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sun, 1 Feb 2015 19:43:26 +0100 Subject: [PATCH] Make wrap watchable --- src/reagent/ratom.cljs | 28 ++++++++++++++++++------ test/testcursor.cljs | 48 ++++++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index e3adb10..c2d57fa 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -315,7 +315,8 @@ ;;; wrap -(deftype Wrapper [^:mutable state callback ^:mutable changed] +(deftype Wrapper [^:mutable state callback ^:mutable changed + ^:mutable watches] IAtom @@ -324,10 +325,13 @@ IReset (-reset! [this newval] - (set! changed true) - (set! state newval) - (callback newval) - state) + (let [oldval state] + (set! changed true) + (set! state newval) + (when-not (nil? watches) + (-notify-watches this oldval newval)) + (callback newval) + newval)) ISwap (-swap! [a f] @@ -349,6 +353,17 @@ (= state (.-state other)) (= callback (.-callback other)))) + IWatchable + (-notify-watches [this oldval newval] + (reduce-kv (fn [_ key f] + (f key this oldval newval) + nil) + nil watches)) + (-add-watch [this key f] + (set! watches (assoc watches key f))) + (-remove-watch [this key] + (set! watches (dissoc watches key))) + IPrintWithWriter (-pr-writer [_ writer opts] (-write writer "#