From 176f7b885cd3f9d16804817bc3f60b344fc79a21 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Tue, 7 Jan 2014 20:32:00 +0100 Subject: [PATCH] Make ratom macros less fancy --- src/cloact/impl/component.cljs | 14 ++++++-------- src/cloact/ratom.clj | 15 ++++----------- test/testratom.cljs | 31 ++++++++++++++++--------------- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/cloact/impl/component.cljs b/src/cloact/impl/component.cljs index 69d236a..c0836d0 100644 --- a/src/cloact/impl/component.cljs +++ b/src/cloact/impl/component.cljs @@ -1,17 +1,14 @@ (ns cloact.impl.component - (:require-macros [cloact.ratom :refer [reaction]] - [cloact.debug :refer [dbg prn]]) (:require [cloact.impl.template :as tmpl] [cloact.impl.util :as util] - [cloact.ratom :as ratom])) + [cloact.ratom :as ratom] + [cloact.debug :refer-macros [dbg prn]])) (def React tmpl/React) - ;;; Accessors - (defn replace-state [this new-state] ;; Don't use React's replaceState, since it doesn't play well ;; with clojure maps @@ -83,10 +80,11 @@ (assert C) (when (nil? (.-cljsRatom C)) (set! (.-cljsRatom C) - (reaction :auto-run (if tmpl/isClient + (ratom/make-reaction + #(do-render C (.-cljsRenderFn C)) + :auto-run (if tmpl/isClient #(.forceUpdate C) - identity) - (do-render C (.-cljsRenderFn C))))) + identity)))) (ratom/run (.-cljsRatom C))) (defn custom-wrapper [key f] diff --git a/src/cloact/ratom.clj b/src/cloact/ratom.clj index 3c163c7..b632cad 100644 --- a/src/cloact/ratom.clj +++ b/src/cloact/ratom.clj @@ -1,20 +1,13 @@ (ns cloact.ratom) -(defn extract-opts [forms] - (let [opts (->> forms - (partition 2) - (take-while #(keyword? (first %))) - (apply concat))] - [opts (drop (count opts) forms)])) - (defmacro reaction [& body] - (let [[opts# main#] (extract-opts body)] - `(cloact.ratom/make-reaction - (fn [] ~@main#) ~@opts#))) + `(cloact.ratom/make-reaction + (fn [] ~@body))) (defmacro run! "Runs body immediately, and runs again whenever atoms deferenced in the body change. Body should side effect." [& body] - `(let [co# (reaction :auto-run true ~@body)] + `(let [co# (cloact.ratom/make-reaction (fn [] ~@body) + :auto-run true)] (deref co#) co#)) diff --git a/test/testratom.cljs b/test/testratom.cljs index 0589188..0838a3a 100644 --- a/test/testratom.cljs +++ b/test/testratom.cljs @@ -78,7 +78,7 @@ "Counter auto updated") (dispose co)) (let [!x (rv/atom 0) - !co (reaction :auto-run true (inc @!x))] + !co (rv/make-reaction #(inc @!x) :auto-run true)] (is (= 1 @!co) "CO has correct value on first deref") (swap! !x inc) (is (= 2 @!co) "CO auto-updates") @@ -171,17 +171,17 @@ disposed-c (rv/atom nil) disposed-cns (rv/atom nil) count-b (rv/atom 0) - b (reaction - :on-dispose #(reset! disposed true) - (swap! count-b inc) - (inc @a)) - c (reaction - :on-dispose #(reset! disposed-c true) - (if (< @a 1) (inc @b) (dec @a))) + b (rv/make-reaction (fn [] + (swap! count-b inc) + (inc @a)) + :on-dispose #(reset! disposed true)) + c (rv/make-reaction #(if (< @a 1) (inc @b) (dec @a)) + :on-dispose #(reset! disposed-c true)) res (rv/atom nil) - cns (run! - :on-dispose #(reset! disposed-cns true) - (reset! res @c))] + cns (rv/make-reaction #(reset! res @c) + :auto-run true + :on-dispose #(reset! disposed-cns true))] + @cns (is (= @res 2)) (is (= (+ 3 runs) (running))) (is (= @count-b 1)) @@ -211,10 +211,11 @@ (deftest test-on-set (let [runs (running) a (rv/atom 0) - b (run! - :on-set (fn [oldv newv] - (reset! a (+ 10 newv))) - (+ 5 @a))] + b (rv/make-reaction #(+ 5 @a) + :auto-run true + :on-set (fn [oldv newv] + (reset! a (+ 10 newv))))] + @b (is (= 5 @b)) (reset! a 1) (is (= 6 @b))