-
Notifications
You must be signed in to change notification settings - Fork 51
/
hooks.cljc
243 lines (197 loc) · 7.04 KB
/
hooks.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(ns helix.hooks
#?(:clj (:require [helix.impl.analyzer :as hana])
:cljs (:require
["react" :as react]
[goog.object :as gobj]))
#?(:cljs (:require-macros [helix.hooks])))
#?(:cljs
(do (def ^:private raw-use-effect react/useEffect)
(def ^:private raw-use-layout-effect react/useLayoutEffect)
(def ^:private raw-use-memo react/useMemo)
(def ^:private raw-use-callback react/useCallback)
(def ^:private raw-use-imperative-handle react/useImperativeHandle)))
#?(:cljs
(defn use-state
"Like `react/useState`, but the update function returned can be used similar
to `swap!`.
Example:
```
(let [[state set-state] (use-state {:count 0})]
;; ...
(set-state update :count inc))
```"
[initial]
(let [[v u] (react/useState initial)
updater (react/useCallback (fn updater
([x] (u x))
([f & xs]
(updater (fn spread-updater [x]
(apply f x xs)))))
;; `u` is guaranteed to be stable so we elide it
#js [])]
[v updater])))
#?(:cljs
(defn use-ref
"Just like react/useRef. Supports accessing the \"current\" property via
dereference (@) and updating the \"current\" property via `reset!` and `swap!`"
[x]
(let [ref (react/useRef nil)]
(when (nil? (.-current ^js ref))
(set! (.-current ^js ref)
(specify! #js {:current x}
IDeref
(-deref [this]
(.-current ^js this))
IReset
(-reset! [this v]
(gobj/set this "current" v))
ISwap
(-swap!
([this f]
(gobj/set this "current" (f (.-current ^js this))))
([this f a]
(gobj/set this "current" (f (.-current ^js this) a)))
([this f a b]
(gobj/set this "current" (f (.-current ^js this) a b)))
([this f a b xs]
(gobj/set this "current" (apply f (.-current ^js this) a b xs)))))))
(.-current ^js ref))))
#?(:cljs
(defn use-reducer
"Just react/useReducer."
([reducer init-state]
(use-reducer reducer init-state js/undefined))
([reducer init-state init]
(react/useReducer
;; handle ifn, e.g. multi-methods
(react/useMemo
#(if (and (not (fn? reducer)) (ifn? reducer))
(fn wrap-ifn [state action]
(reducer state action))
reducer)
#js [reducer])
init-state
init))))
#?(:cljs
(def use-context
"Just react/useContext"
react/useContext))
;; React `useEffect` expects either a function or undefined to be returned
#?(:cljs
(defn- wrap-fx [f]
(fn wrap-fx-return []
(let [x (f)]
(if (fn? x)
x
js/undefined)))))
(defn simple-body? [body]
(and (= (count body) 1) (symbol? (first body))))
#?(:clj
(defn deps-macro-body [env deps body deps->hook-body]
(cond
;; deps are passed in as a vector
(vector? deps) (deps->hook-body `(cljs.core/array ~@deps)
body)
;; auto deps is passed in
(= deps :auto-deps) (deps->hook-body
`(cljs.core/array ~@(hana/resolve-local-vars env body))
body)
;; always fire it (don't pass any deps in to hook)
(= deps :always) (deps->hook-body body)
;; pass an empty array for things that should only run once
(= deps :once) (deps->hook-body '(cljs.core/array) body))))
(defmacro use-effect
[deps & body]
(deps-macro-body
&env deps body
(fn
([fn-body] `^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body))))
([deps fn-body]
`^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body)) ~deps)))))
#?(:cljs
;; we provide a CLJS defn in the case when we need to pass around the hook
;; as a value. This will be slower, `:auto-deps` won't work and devtools will
;; be harder to read
(defn use-effect*
([f] (react/useEffect (wrap-fx f)))
([f deps]
(when js/goog.DEBUG
(when (= deps :auto-deps)
(throw (js/Error. "Can't use `:auto-deps` with `use-effect*`; use `use-effect` macro for that"))))
(react/useEffect (wrap-fx f) (to-array deps)))))
(defmacro use-layout-effect [deps & body]
(deps-macro-body
&env deps body
(fn
([fn-body] `^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body))))
([deps fn-body]
`^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body)) ~deps)))))
#?(:cljs
(defn use-layout-effect*
([f] (react/useLayoutEffect (wrap-fx f)))
([f deps]
(when js/goog.DEBUG
(when (= deps :auto-deps)
(throw (js/Error. "Can't use `:auto-deps` with `use-layout-effect*`; use `use-layout-effect` macro for that"))))
(react/useLayoutEffect (wrap-fx f) (to-array deps)))))
(defmacro use-memo
[deps & body]
(deps-macro-body
&env deps body
(fn
([fn-body]
(vary-meta
`(raw-use-memo (fn [] ~@fn-body))
merge
{:tag (hana/inferred-type &env fn-body)}))
([deps fn-body]
(vary-meta
`(raw-use-memo (fn [] ~@fn-body)
~deps)
merge
{:tag (hana/inferred-type &env (last fn-body))})))))
#?(:cljs
(defn use-memo*
([f] (react/useMemo f))
([f deps]
(when js/goog.DEBUG
(when (= deps :auto-deps)
(throw (js/Error. "Can't use `:auto-deps` with `use-memo*`; use `use-memo` macro for that"))))
(react/useMemo f (to-array deps)))))
(defmacro use-callback
[deps & body]
(deps-macro-body
&env deps body
(fn
([fn-body] `^function (raw-use-callback ~@fn-body))
([deps fn-body] `^function (raw-use-callback ~@fn-body
~deps)))))
#?(:cljs
(defn use-callback*
([f] (react/useCallback f))
([f deps]
(when js/goog.DEBUG
(when (= deps :auto-deps)
(throw (js/Error. "Can't use `:auto-deps` with `use-callback*`; use `use-callback` macro for that"))))
(react/useCallback f (to-array deps)))))
(defmacro use-imperative-handle
[ref deps & body]
(deps-macro-body
&env deps body
(fn
([fn-body] `(raw-use-imperative-handle ref (fn [] ~@fn-body)))
([deps fn-body] `(raw-use-imperative-handle
(fn [] ~@fn-body)
~deps)))))
#?(:cljs
(defn use-imperative-handle*
([ref f] (react/useImperativeHandle ref f))
([ref f deps]
(when js/goog.DEBUG
(when (= deps :auto-deps)
(throw (js/Error. "Can't use `:auto-deps` with `use-imperative-handle*`; use `use-imperative-handle` macro for that"))))
(react/useImperativeHandle ref f (to-array deps)))))
#?(:cljs
(def use-debug-value
"just react/useDebugValue"
react/debugValue))