Make ratom macros less fancy

This commit is contained in:
Dan Holmsand 2014-01-07 20:32:00 +01:00
parent b93f48ba47
commit 176f7b885c
3 changed files with 26 additions and 34 deletions

View File

@ -1,17 +1,14 @@
(ns cloact.impl.component (ns cloact.impl.component
(:require-macros [cloact.ratom :refer [reaction]]
[cloact.debug :refer [dbg prn]])
(:require [cloact.impl.template :as tmpl] (:require [cloact.impl.template :as tmpl]
[cloact.impl.util :as util] [cloact.impl.util :as util]
[cloact.ratom :as ratom])) [cloact.ratom :as ratom]
[cloact.debug :refer-macros [dbg prn]]))
(def React tmpl/React) (def React tmpl/React)
;;; Accessors ;;; Accessors
(defn replace-state [this new-state] (defn replace-state [this new-state]
;; Don't use React's replaceState, since it doesn't play well ;; Don't use React's replaceState, since it doesn't play well
;; with clojure maps ;; with clojure maps
@ -83,10 +80,11 @@
(assert C) (assert C)
(when (nil? (.-cljsRatom C)) (when (nil? (.-cljsRatom C))
(set! (.-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) #(.forceUpdate C)
identity) identity))))
(do-render C (.-cljsRenderFn C)))))
(ratom/run (.-cljsRatom C))) (ratom/run (.-cljsRatom C)))
(defn custom-wrapper [key f] (defn custom-wrapper [key f]

View File

@ -1,20 +1,13 @@
(ns cloact.ratom) (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] (defmacro reaction [& body]
(let [[opts# main#] (extract-opts body)] `(cloact.ratom/make-reaction
`(cloact.ratom/make-reaction (fn [] ~@body)))
(fn [] ~@main#) ~@opts#)))
(defmacro run! (defmacro run!
"Runs body immediately, and runs again whenever atoms deferenced in the body change. Body should side effect." "Runs body immediately, and runs again whenever atoms deferenced in the body change. Body should side effect."
[& body] [& body]
`(let [co# (reaction :auto-run true ~@body)] `(let [co# (cloact.ratom/make-reaction (fn [] ~@body)
:auto-run true)]
(deref co#) (deref co#)
co#)) co#))

View File

@ -78,7 +78,7 @@
"Counter auto updated") "Counter auto updated")
(dispose co)) (dispose co))
(let [!x (rv/atom 0) (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") (is (= 1 @!co) "CO has correct value on first deref")
(swap! !x inc) (swap! !x inc)
(is (= 2 @!co) "CO auto-updates") (is (= 2 @!co) "CO auto-updates")
@ -171,17 +171,17 @@
disposed-c (rv/atom nil) disposed-c (rv/atom nil)
disposed-cns (rv/atom nil) disposed-cns (rv/atom nil)
count-b (rv/atom 0) count-b (rv/atom 0)
b (reaction b (rv/make-reaction (fn []
:on-dispose #(reset! disposed true) (swap! count-b inc)
(swap! count-b inc) (inc @a))
(inc @a)) :on-dispose #(reset! disposed true))
c (reaction c (rv/make-reaction #(if (< @a 1) (inc @b) (dec @a))
:on-dispose #(reset! disposed-c true) :on-dispose #(reset! disposed-c true))
(if (< @a 1) (inc @b) (dec @a)))
res (rv/atom nil) res (rv/atom nil)
cns (run! cns (rv/make-reaction #(reset! res @c)
:on-dispose #(reset! disposed-cns true) :auto-run true
(reset! res @c))] :on-dispose #(reset! disposed-cns true))]
@cns
(is (= @res 2)) (is (= @res 2))
(is (= (+ 3 runs) (running))) (is (= (+ 3 runs) (running)))
(is (= @count-b 1)) (is (= @count-b 1))
@ -211,10 +211,11 @@
(deftest test-on-set (deftest test-on-set
(let [runs (running) (let [runs (running)
a (rv/atom 0) a (rv/atom 0)
b (run! b (rv/make-reaction #(+ 5 @a)
:on-set (fn [oldv newv] :auto-run true
(reset! a (+ 10 newv))) :on-set (fn [oldv newv]
(+ 5 @a))] (reset! a (+ 10 newv))))]
@b
(is (= 5 @b)) (is (= 5 @b))
(reset! a 1) (reset! a 1)
(is (= 6 @b)) (is (= 6 @b))