-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
ratom.clj
56 lines (53 loc) · 2.62 KB
/
ratom.clj
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
(ns ^{:mranderson/inlined true} day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.ratom
(:refer-clojure :exclude [run!])
(:require [day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.debug :as d]
[day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.interop :as interop]))
(defmacro reaction [& body]
`(day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.ratom/make-reaction
(fn [] ~@body)))
(defmacro run!
"Runs body immediately, and runs again whenever atoms deferenced in the body change. Body should side effect."
[& body]
`(let [co# (day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.ratom/make-reaction (fn [] ~@body)
:auto-run true)]
(deref co#)
co#))
(defmacro with-let [bindings & body]
(assert (vector? bindings)
(str "with-let bindings must be a vector, not "
(pr-str bindings)))
(let [v (gensym "with-let")
k (keyword v)
init (gensym "init")
bs (into [init `(zero? (alength ~v))]
(map-indexed (fn [i x]
(if (even? i)
x
(let [j (quot i 2)]
`(if ~init
(interop/unchecked-aset ~v ~j ~x)
(interop/unchecked-aget ~v ~j)))))
bindings))
[forms destroy] (let [fin (last body)]
(if (and (list? fin)
(= 'finally (first fin)))
[(butlast body) `(fn [] ~@(rest fin))]
[body nil]))
add-destroy (when destroy
`(let [destroy# ~destroy]
(if (day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.ratom/reactive?)
(when (nil? (.-destroy ~v))
(set! (.-destroy ~v) destroy#))
(destroy#))))
asserting (if *assert* true false)]
`(let [~v (day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.ratom/with-let-values ~k)]
(when ~asserting
(when-some [^clj c# day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.ratom/*ratom-context*]
(when (== (.-generation ~v) (.-ratomGeneration c#))
(d/error "Warning: The same with-let is being used more "
"than once in the same reactive context."))
(set! (.-generation ~v) (.-ratomGeneration c#))))
(let ~bs
(let [res# (do ~@forms)]
~add-destroy
res#)))))