mirror of
https://github.com/status-im/reagent.git
synced 2025-01-14 05:45:06 +00:00
Make wrap watchable
This commit is contained in:
parent
5798a3f056
commit
6e0e9ed564
@ -315,7 +315,8 @@
|
|||||||
|
|
||||||
;;; wrap
|
;;; wrap
|
||||||
|
|
||||||
(deftype Wrapper [^:mutable state callback ^:mutable changed]
|
(deftype Wrapper [^:mutable state callback ^:mutable changed
|
||||||
|
^:mutable watches]
|
||||||
|
|
||||||
IAtom
|
IAtom
|
||||||
|
|
||||||
@ -324,10 +325,13 @@
|
|||||||
|
|
||||||
IReset
|
IReset
|
||||||
(-reset! [this newval]
|
(-reset! [this newval]
|
||||||
(set! changed true)
|
(let [oldval state]
|
||||||
(set! state newval)
|
(set! changed true)
|
||||||
(callback newval)
|
(set! state newval)
|
||||||
state)
|
(when-not (nil? watches)
|
||||||
|
(-notify-watches this oldval newval))
|
||||||
|
(callback newval)
|
||||||
|
newval))
|
||||||
|
|
||||||
ISwap
|
ISwap
|
||||||
(-swap! [a f]
|
(-swap! [a f]
|
||||||
@ -349,6 +353,17 @@
|
|||||||
(= state (.-state other))
|
(= state (.-state other))
|
||||||
(= callback (.-callback 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
|
IPrintWithWriter
|
||||||
(-pr-writer [_ writer opts]
|
(-pr-writer [_ writer opts]
|
||||||
(-write writer "#<wrap: ")
|
(-write writer "#<wrap: ")
|
||||||
@ -358,4 +373,5 @@
|
|||||||
(defn make-wrapper [value callback-fn args]
|
(defn make-wrapper [value callback-fn args]
|
||||||
(Wrapper. value
|
(Wrapper. value
|
||||||
(util/partial-ifn. callback-fn args nil)
|
(util/partial-ifn. callback-fn args nil)
|
||||||
false))
|
false nil))
|
||||||
|
|
||||||
|
@ -11,18 +11,6 @@
|
|||||||
(defn running [] (rv/running))
|
(defn running [] (rv/running))
|
||||||
(defn dispose [v] (rv/dispose! v))
|
(defn dispose [v] (rv/dispose! v))
|
||||||
|
|
||||||
(defn ratom-perf []
|
|
||||||
(dbg "ratom-perf")
|
|
||||||
(let [a (rv/atom {})
|
|
||||||
mid (reaction (inc @a))
|
|
||||||
res (run!
|
|
||||||
(inc @mid))]
|
|
||||||
(time (dotimes [x 100000]
|
|
||||||
(swap! a inc)))
|
|
||||||
(dispose res)))
|
|
||||||
|
|
||||||
;; (ratom-perf)
|
|
||||||
|
|
||||||
(deftest basic-cursor
|
(deftest basic-cursor
|
||||||
(let [runs (running)
|
(let [runs (running)
|
||||||
start-base (rv/atom {:a {:b {:c 0}}})
|
start-base (rv/atom {:a {:b {:c 0}}})
|
||||||
@ -351,7 +339,7 @@
|
|||||||
(is (= runs (running)))))
|
(is (= runs (running)))))
|
||||||
|
|
||||||
|
|
||||||
(deftest atom-behaviors
|
(deftest cursor-atom-behaviors
|
||||||
(let [test-atom (atom {:a {:b {:c {:d 1}}}})
|
(let [test-atom (atom {:a {:b {:c {:d 1}}}})
|
||||||
test-cursor (r/cursor test-atom [:a :b :c :d])
|
test-cursor (r/cursor test-atom [:a :b :c :d])
|
||||||
witness (atom nil)
|
witness (atom nil)
|
||||||
@ -394,3 +382,37 @@
|
|||||||
(is (= (:new @witness) "newer")) ;; shouldn't have changed
|
(is (= (:new @witness) "newer")) ;; shouldn't have changed
|
||||||
(is (= (running) runs))
|
(is (= (running) runs))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(deftest wrap-atom-behaviors
|
||||||
|
(let [test-atom (atom "foo")
|
||||||
|
test-wrap (r/wrap @test-atom reset! test-atom)
|
||||||
|
witness (atom nil)]
|
||||||
|
;; per the description, reset! should return the new values
|
||||||
|
(is (= {}
|
||||||
|
(reset! test-wrap {})))
|
||||||
|
(is (= @test-wrap @test-atom))
|
||||||
|
|
||||||
|
;; per the description, swap! should return the new values
|
||||||
|
(is (= {:z [1 2 3]}
|
||||||
|
(swap! test-wrap assoc :z [1 2 3])))
|
||||||
|
(is (= @test-wrap @test-atom))
|
||||||
|
|
||||||
|
;; watches should behave like with a normal atom
|
||||||
|
(reset! test-wrap "old")
|
||||||
|
(add-watch test-wrap :w #(reset! witness
|
||||||
|
{:key %1 :ref %2 :old %3 :new %4}))
|
||||||
|
(reset! test-wrap "new") ;; this should trigger the watch function
|
||||||
|
(is (= (:key @witness) :w))
|
||||||
|
;; cursor reports that the reaction is the current atom,
|
||||||
|
;; but I guess that's ok
|
||||||
|
(is (= (:old @witness) "old"))
|
||||||
|
(is (= (:new @witness) "new"))
|
||||||
|
(is (= (:ref @witness) test-wrap))
|
||||||
|
(is (= (:new @witness) "new"))
|
||||||
|
|
||||||
|
;; can we remove the watch?
|
||||||
|
(remove-watch test-wrap :w)
|
||||||
|
(reset! test-wrap "removed")
|
||||||
|
(is (= (:new @witness) "new")) ;; shouldn't have changed
|
||||||
|
(is (= @test-wrap @test-atom))
|
||||||
|
))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user