-
Notifications
You must be signed in to change notification settings - Fork 410
/
component.cljs
342 lines (288 loc) · 10 KB
/
component.cljs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
(ns reagent.impl.component
(:require [create-react-class :as create-react-class]
[react :as react]
[reagent.impl.util :as util]
[reagent.impl.batching :as batch]
[reagent.ratom :as ratom]
[reagent.interop :refer-macros [$ $!]]
[reagent.debug :refer-macros [dbg prn dev? warn error warn-unless
assert-callable]]))
(declare ^:dynamic *current-component*)
;;; Argv access
(defn shallow-obj-to-map [o]
(let [ks (js-keys o)
len (alength ks)]
(loop [m {} i 0]
(if (< i len)
(let [k (aget ks i)]
(recur (assoc m (keyword k) (aget o k)) (inc i)))
m))))
(defn extract-props [v]
(let [p (nth v 1 nil)]
(if (map? p) p)))
(defn extract-children [v]
(let [p (nth v 1 nil)
first-child (if (or (nil? p) (map? p)) 2 1)]
(if (> (count v) first-child)
(subvec v first-child))))
(defn props-argv [c p]
(if-some [a ($ p :argv)]
a
[(.-constructor c) (shallow-obj-to-map p)]))
(defn get-argv [c]
(props-argv c ($ c :props)))
(defn get-props [c]
(let [p ($ c :props)]
(if-some [v ($ p :argv)]
(extract-props v)
(shallow-obj-to-map p))))
(defn get-children [c]
(let [p ($ c :props)]
(if-some [v ($ p :argv)]
(extract-children v)
(->> ($ p :children)
(react/Children.toArray)
(into [])))))
(defn ^boolean reagent-class? [c]
(and (fn? c)
(some? (some-> c .-prototype ($ :reagentRender)))))
(defn ^boolean react-class? [c]
(and (fn? c)
(some? (some-> c .-prototype ($ :render)))))
(defn ^boolean reagent-component? [c]
(some? ($ c :reagentRender)))
(defn cached-react-class [c]
($ c :cljsReactClass))
(defn cache-react-class [c constructor]
($! c :cljsReactClass constructor))
;;; State
(defn state-atom [this]
(let [sa ($ this :cljsState)]
(if-not (nil? sa)
sa
($! this :cljsState (ratom/atom nil)))))
;; avoid circular dependency: this gets set from template.cljs
(defonce as-element nil)
;;; Rendering
(defn wrap-render [c]
(let [f ($ c :reagentRender)
_ (assert-callable f)
res (if (true? ($ c :cljsLegacyRender))
(.call f c c)
(let [v (get-argv c)
n (count v)]
(case n
1 (.call f c)
2 (.call f c (nth v 1))
3 (.call f c (nth v 1) (nth v 2))
4 (.call f c (nth v 1) (nth v 2) (nth v 3))
5 (.call f c (nth v 1) (nth v 2) (nth v 3) (nth v 4))
(.apply f c (.slice (into-array v) 1)))))]
(cond
(vector? res) (as-element res)
(ifn? res) (let [f (if (reagent-class? res)
(fn [& args]
(as-element (apply vector res args)))
res)]
($! c :reagentRender f)
(recur c))
:else res)))
(declare comp-name)
(defn do-render [c]
(binding [*current-component* c]
(if (dev?)
;; Log errors, without using try/catch (and mess up call stack)
(let [ok (array false)]
(try
(let [res (wrap-render c)]
(aset ok 0 true)
res)
(finally
(when-not (aget ok 0)
(error (str "Error rendering component"
(comp-name)))))))
(wrap-render c))))
;;; Method wrapping
(def rat-opts {:no-cache true})
(def static-fns
{:render
(fn render []
(this-as c (if util/*non-reactive*
(do-render c)
(let [rat ($ c :cljsRatom)]
(batch/mark-rendered c)
(if (nil? rat)
(ratom/run-in-reaction #(do-render c) c "cljsRatom"
batch/queue-render rat-opts)
(._run rat false))))))})
(defn custom-wrapper [key f]
(case key
:getDefaultProps
(throw (js/Error. "getDefaultProps not supported"))
:getInitialState
(fn getInitialState []
(this-as c (reset! (state-atom c) (.call f c c))))
:componentWillReceiveProps
(fn componentWillReceiveProps [nextprops]
(this-as c (.call f c c (props-argv c nextprops))))
:shouldComponentUpdate
(fn shouldComponentUpdate [nextprops nextstate]
(or util/*always-update*
(this-as c
;; Don't care about nextstate here, we use forceUpdate
;; when only when state has changed anyway.
(let [old-argv ($ c :props.argv)
new-argv ($ nextprops :argv)
noargv (or (nil? old-argv) (nil? new-argv))]
(cond
(nil? f) (or noargv (not= old-argv new-argv))
noargv (.call f c c (get-argv c) (props-argv c nextprops))
:else (.call f c c old-argv new-argv))))))
:componentWillUpdate
(fn componentWillUpdate [nextprops]
(this-as c (.call f c c (props-argv c nextprops))))
:componentDidUpdate
(fn componentDidUpdate [oldprops]
(this-as c (.call f c c (props-argv c oldprops))))
:componentWillMount
(fn componentWillMount []
(this-as c
($! c :cljsMountOrder (batch/next-mount-count))
(when-not (nil? f)
(.call f c c))))
:componentDidMount
(fn componentDidMount []
(this-as c (.call f c c)))
:componentWillUnmount
(fn componentWillUnmount []
(this-as c
(some-> ($ c :cljsRatom)
ratom/dispose!)
(batch/mark-rendered c)
(when-not (nil? f)
(.call f c c))))
:componentDidCatch
(fn componentDidCatch [error info]
(this-as c (.call f c c error info)))
nil))
(defn get-wrapper [key f name]
(let [wrap (custom-wrapper key f)]
(when (and wrap f)
(assert-callable f))
(or wrap f)))
(def obligatory {:shouldComponentUpdate nil
:componentWillMount nil
:componentWillUnmount nil})
(def dash-to-camel (util/memoize-1 util/dash-to-camel))
(defn camelify-map-keys [fun-map]
(reduce-kv (fn [m k v]
(assoc m (-> k dash-to-camel keyword) v))
{} fun-map))
(defn add-obligatory [fun-map]
(merge obligatory fun-map))
(defn wrap-funs [fmap]
(when (dev?)
(let [renders (select-keys fmap [:render :reagentRender :componentFunction])
render-fun (-> renders vals first)]
(assert (pos? (count renders)) "Missing reagent-render")
(assert (== 1 (count renders)) "Too many render functions supplied")
(assert-callable render-fun)))
(let [render-fun (or (:reagentRender fmap)
(:componentFunction fmap))
legacy-render (nil? render-fun)
render-fun (or render-fun
(:render fmap))
name (str (or (:displayName fmap)
(util/fun-name render-fun)))
name (case name
"" (str (gensym "reagent"))
name)
fmap (reduce-kv (fn [m k v]
(assoc m k (get-wrapper k v name)))
{} fmap)]
(assoc fmap
:displayName name
:autobind false
:cljsLegacyRender legacy-render
:reagentRender render-fun
:render (:render static-fns))))
(defn map-to-js [m]
(reduce-kv (fn [o k v]
(doto o
(aset (name k) v)))
#js{} m))
(defn cljsify [body]
(-> body
camelify-map-keys
add-obligatory
wrap-funs
map-to-js))
(defn create-class [body]
{:pre [(map? body)]}
(->> body
cljsify
create-react-class))
(defn fiber-component-path [fiber]
(let [name (some-> fiber
($ :type)
($ :displayName))
parent (some-> fiber
($ :return))
path (some-> parent
fiber-component-path
(str " > "))
res (str path name)]
(when-not (empty? res) res)))
(defn component-path [c]
;; Alternative branch for React 16
;; Try both original name (for UMD foreign-lib) and manged name (property access, for Closure optimized React)
(if-let [fiber (or (some-> c ($ :_reactInternalFiber))
(some-> c (.-_reactInternalFiber)))]
(fiber-component-path fiber)
(let [instance (or (some-> c ($ :_reactInternalInstance))
(some-> c (.-_reactInternalInstance))
c)
elem (or (some-> instance ($ :_currentElement))
(some-> instance (.-_currentElement)))
name (some-> elem
($ :type)
($ :displayName))
owner (or (some-> elem ($ :_owner))
(some-> elem (.-_owner)))
path (some-> owner
component-path
(str " > "))
res (str path name)]
(when-not (empty? res) res))))
(defn comp-name []
(if (dev?)
(let [c *current-component*
n (or (component-path c)
(some-> c .-constructor util/fun-name))]
(if-not (empty? n)
(str " (in " n ")")
""))
""))
(defn fn-to-class [f]
(assert-callable f)
(warn-unless (not (and (react-class? f)
(not (reagent-class? f))))
"Using native React classes directly in Hiccup forms "
"is not supported. Use create-element or "
"adapt-react-class instead: " (let [n (util/fun-name f)]
(if (empty? n) f n))
(comp-name))
(if (reagent-class? f)
(cache-react-class f f)
(let [spec (meta f)
withrender (assoc spec :reagent-render f)
res (create-class withrender)]
(cache-react-class f res))))
(defn as-class [tag]
(if-some [cached-class (cached-react-class tag)]
cached-class
(fn-to-class tag)))
(defn reactify-component [comp]
(if (react-class? comp)
comp
(as-class comp)))