From 8cf5f7be80aa1d909c2c5b3de63673bb6acc5553 Mon Sep 17 00:00:00 2001 From: Mikkel Gravgaard Date: Tue, 20 Sep 2016 09:02:28 +0200 Subject: [PATCH] add reaction that allows the user to explicitly notify users --- src/reagent/ratom.cljs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 8f50bb8f..757f362a 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -493,6 +493,25 @@ reaction)) +(defn make-reaction-notify [f & args] + (let [reaction (apply make-reaction f args)] + ;; override _run method of Reaction type so that it calls f with the `notify` function + (set! (.-_run reaction) (fn [check] + (let [oldstate (.-state reaction) + notify (fn [res] (when-not (.-nocache? reaction) + (set! (.-state reaction) res) + ;; Use = to determine equality from reactions, since + ;; they are likely to produce new data structures. + (when-not (or (nil? (.-watches reaction)) + (= oldstate res)) + (notify-w reaction oldstate res)))) + res (if check + (._try-capture reaction (partial f notify)) + (deref-capture (partial f notify) reaction))] + + res))) + reaction)) + (def ^:private temp-reaction (make-reaction nil))