mirror of https://github.com/status-im/reagent.git
Make ratom macros less fancy
This commit is contained in:
parent
b93f48ba47
commit
176f7b885c
|
@ -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]
|
||||
|
|
|
@ -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#))
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue