|
1 | | -(ns rum |
2 | | - (:require-macros rum) |
| 1 | +(ns rum.core |
| 2 | + (:require-macros rum.core) |
3 | 3 | (:require |
4 | 4 | [cljsjs.react] |
5 | 5 | [sablono.core])) |
6 | 6 |
|
7 | | -#_(enable-console-print!) |
8 | | - |
9 | 7 | (let [last-id (volatile! 0)] |
10 | 8 | (defn next-id [] |
11 | 9 | (vswap! last-id inc))) |
|
14 | 12 | (aget (.-state comp) ":rum/state")) |
15 | 13 |
|
16 | 14 | (defn id [comp] |
17 | | - (::id @(state comp))) |
| 15 | + (:rum/id @(state comp))) |
18 | 16 |
|
19 | 17 | (defn- collect [fn-key classes] |
20 | 18 | (->> classes |
|
53 | 51 | (fn [] |
54 | 52 | (this-as this |
55 | 53 | (let [props (.-props this) |
56 | | - state (-> { ::react-component this |
57 | | - ::id (next-id) } ;; assign id on mount? |
| 54 | + state (-> { :rum/react-component this |
| 55 | + :rum/id (next-id) } ;; assign id on mount? |
58 | 56 | (merge (props->state props)))] |
59 | 57 | #js { ":rum/state" (volatile! state) }))) |
60 | 58 | :componentWillMount |
|
71 | 69 | (fn [next-props] |
72 | 70 | (this-as this |
73 | 71 | (let [old-state @(state this) |
74 | | - next-state (-> { ::react-component this |
75 | | - ::id (::id old-state) } |
| 72 | + next-state (-> { :rum/react-component this |
| 73 | + :rum/id (:rum/id old-state) } |
76 | 74 | (merge (props->state next-props))) |
77 | 75 | next-state (reduce #(%2 old-state %1) next-state transfer-state)] |
78 | 76 | (.setState this #js {":rum/state" (volatile! next-state)})))) |
|
156 | 154 | ;; initialization |
157 | 155 |
|
158 | 156 | (defn render->mixin [render-fn] |
159 | | - { :render (fn [state] [(apply render-fn (::args state)) state]) }) |
| 157 | + { :render (fn [state] [(apply render-fn (:rum/args state)) state]) }) |
160 | 158 |
|
161 | 159 | (defn render-state->mixin [render-fn] |
162 | | - { :render (fn [state] [(apply render-fn state (::args state)) state]) }) |
| 160 | + { :render (fn [state] [(apply render-fn state (:rum/args state)) state]) }) |
163 | 161 |
|
164 | 162 | (defn render-comp->mixin [render-fn] |
165 | | - { :render (fn [state] [(apply render-fn (:rum/react-component state) (::args state)) state]) }) |
| 163 | + { :render (fn [state] [(apply render-fn (:rum/react-component state) (:rum/args state)) state]) }) |
166 | 164 |
|
167 | 165 | (defn args->state [args] |
168 | | - {::args args}) |
| 166 | + {:rum/args args}) |
169 | 167 |
|
170 | 168 | (defn element [class state & [props]] |
171 | 169 | (let [props (or props #js {})] |
172 | 170 | (aset props ":rum/initial-state" state) |
173 | 171 | (js/React.createElement class props))) |
174 | 172 |
|
175 | 173 | (defn ctor->class [ctor] |
176 | | - (::class (meta ctor))) |
| 174 | + (:rum/class (meta ctor))) |
177 | 175 |
|
178 | 176 | (defn with-key [element key] |
179 | 177 | (js/React.cloneElement element #js { "key" key } nil)) |
|
186 | 184 | (def static { |
187 | 185 | :should-update |
188 | 186 | (fn [old-state new-state] |
189 | | - (not= (::args old-state) (::args new-state))) |
| 187 | + (not= (:rum/args old-state) (:rum/args new-state))) |
190 | 188 | }) |
191 | 189 |
|
192 | 190 | ;; local mixin |
|
215 | 213 | (def ^:dynamic *reactions*) |
216 | 214 |
|
217 | 215 | (defn- reactive-key [state] |
218 | | - (str ":rum/reactive-" (::id state))) |
| 216 | + (str ":rum/reactive-" (:rum/id state))) |
219 | 217 |
|
220 | 218 | (def reactive { |
221 | 219 | :transfer-state |
222 | 220 | (fn [old new] |
223 | | - (assoc new ::refs (::refs old))) |
| 221 | + (assoc new :rum/refs (:rum/refs old))) |
224 | 222 | :wrap-render |
225 | 223 | (fn [render-fn] |
226 | 224 | (fn [state] |
227 | 225 | (binding [*reactions* (volatile! #{})] |
228 | | - (let [comp (::react-component state) |
229 | | - old-reactions (::refs state #{}) |
| 226 | + (let [comp (:rum/react-component state) |
| 227 | + old-reactions (:rum/refs state #{}) |
230 | 228 | [dom next-state] (render-fn state) |
231 | 229 | new-reactions @*reactions* |
232 | 230 | key (reactive-key state)] |
|
238 | 236 | (add-watch ref key |
239 | 237 | (fn [_ _ _ _] |
240 | 238 | (request-render comp))))) |
241 | | - [dom (assoc next-state ::refs new-reactions)])))) |
| 239 | + [dom (assoc next-state :rum/refs new-reactions)])))) |
242 | 240 | :will-unmount |
243 | 241 | (fn [state] |
244 | 242 | (let [key (reactive-key state)] |
245 | | - (doseq [ref (::refs state)] |
| 243 | + (doseq [ref (:rum/refs state)] |
246 | 244 | (remove-watch ref key))) |
247 | | - (dissoc state ::refs)) |
| 245 | + (dissoc state :rum/refs)) |
248 | 246 | }) |
249 | 247 |
|
250 | 248 | (defn react [ref] |
|
325 | 323 | (def cursored { |
326 | 324 | :transfer-state |
327 | 325 | (fn [old new] |
328 | | - (assoc new ::om-args (::om-args old))) |
| 326 | + (assoc new :rum/om-args (:rum/om-args old))) |
329 | 327 | :should-update |
330 | 328 | (fn [old-state new-state] |
331 | | - (not= (::om-args old-state) (deref-args (::args new-state)))) |
| 329 | + (not= (:rum/om-args old-state) (deref-args (:rum/args new-state)))) |
332 | 330 | :wrap-render |
333 | 331 | (fn [render-fn] |
334 | 332 | (fn [state] |
335 | 333 | (let [[dom next-state] (render-fn state)] |
336 | | - [dom (assoc next-state ::om-args (deref-args (::args state)))]))) |
| 334 | + [dom (assoc next-state :rum/om-args (deref-args (:rum/args state)))]))) |
337 | 335 | }) |
338 | 336 |
|
339 | 337 | (defn- cursored-key [state] |
340 | | - (str ":rum/cursored-" (::id state))) |
| 338 | + (str ":rum/cursored-" (:rum/id state))) |
341 | 339 |
|
342 | 340 | (def cursored-watch { |
343 | 341 | :did-mount |
344 | 342 | (fn [state] |
345 | | - (doseq [arg (::args state) |
| 343 | + (doseq [arg (:rum/args state) |
346 | 344 | :when (satisfies? IWatchable arg)] |
347 | 345 | (add-watch arg (cursored-key state) |
348 | | - (fn [_ _ _ _] (request-render (::react-component state))))) |
| 346 | + (fn [_ _ _ _] (request-render (:rum/react-component state))))) |
349 | 347 | state) |
350 | 348 | :will-unmount |
351 | 349 | (fn [state] |
352 | | - (doseq [arg (::args state) |
| 350 | + (doseq [arg (:rum/args state) |
353 | 351 | :when (satisfies? IWatchable arg)] |
354 | 352 | (remove-watch arg (cursored-key state))) |
355 | 353 | state) |
|
0 commit comments