Skip to content

Commit

Permalink
add cursor-in hook
Browse files Browse the repository at this point in the history
  • Loading branch information
roman01la committed Mar 18, 2020
1 parent b66efca commit 97cc8f1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
16 changes: 1 addition & 15 deletions core/dev/uix/recipes/state_hook.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,9 @@
might not be immediately available when dereferenced."
(:require [uix.core.alpha :as uix]))

#?(:cljs
(do
(deftype Cursor [ref path]
IDeref
(-deref [o]
(get-in @ref path))
IReset
(-reset! [o new-value]
(swap! ref update-in path (constantly new-value))))))

(defn derive-state [ref path]
#?(:clj (atom (get-in @ref path))
:cljs (uix/memo #(Cursor. ref path) [ref path])))

(defn recipe []
(let [state* (uix/state {:value "Hello!"})
value* (derive-state state* [:value])]
value* (uix/cursor-in state* [:value])]
[:div
[:input {:value @value*
:on-change #(reset! value* (.. % -target -value))}]
Expand Down
5 changes: 5 additions & 0 deletions core/src/uix/core/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@
[value]
(hooks/state value))

(defn cursor-in
"Takes ref type value and path vector and returns ref type cursor value watching into original ref"
[ref path]
(hooks/cursor-in ref path))

(defn effect!
"Takes a function to be executed in an effect and optional vector of dependencies.
Expand Down
38 changes: 38 additions & 0 deletions core/src/uix/hooks/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,41 @@
(unsubscribe))))
#js [get-current-value subscribe])
ret-value)))

;; == Derived state hook ==
#?(:cljs
(deftype Cursor [ref path]
Object
(equiv [this other]
(-equiv this other))

IHash
(-hash [o] (goog/getUid o))

IDeref
(-deref [o]
(get-in @ref path))

IReset
(-reset! [o new-value]
(swap! ref update-in path (constantly new-value)))

ISwap
(-swap! [o f]
(-reset! o (f (-deref o))))
(-swap! [o f a]
(-reset! o (f (-deref o) a)))
(-swap! [o f a b]
(-reset! o (f (-deref o) a b)))
(-swap! [o f a b xs]
(-reset! o (apply f (-deref o) a b xs)))

IPrintWithWriter
(-pr-writer [o writer opts]
(-write writer "#object [uix.hooks.alpha.Cursor ")
(pr-writer {:val (-deref o)} writer opts)
(-write writer "]"))))

(defn cursor-in [ref path]
#?(:clj (atom (get-in @ref path))
:cljs (memo #(Cursor. ref path) [ref path])))
3 changes: 3 additions & 0 deletions core/test/uix/hooks_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
(deftest test-state
(is (= 1 @(core/state 1))))

(deftest test-cursor-in
(is (= 1 @(core/cursor-in (core/state {:x 1}) [:x]))))

(deftest test-ref
(is (= nil @(core/ref)))
(is (= 1 @(core/ref 1))))
Expand Down
12 changes: 12 additions & 0 deletions core/test/uix/hooks_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
(async done
(t/render [f-state done]))))

(deftest test-cursor-in-hook
(let [f-state (fn [done]
(let [state (core/state {:x 1})
x (core/cursor-in state [:x])]
(is (instance? hooks/Cursor x))
(is (or (== @x 1) (== @x 2)))
(if (== @x 2)
(done)
(swap! x inc))))]
(async done
(t/render [f-state done]))))

(deftest test-state-hook-identity
(let [f-state (fn [done]
(let [xs (core/state [])]
Expand Down

0 comments on commit 97cc8f1

Please sign in to comment.