-
Notifications
You must be signed in to change notification settings - Fork 21
/
reconciler.cljs
106 lines (87 loc) · 3.04 KB
/
reconciler.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
(ns citrus.reconciler
(:require-macros [citrus.macros :as m]))
(defn- queue-effects! [queue f]
(vswap! queue conj f))
(defn- clear-queue! [queue]
(vreset! queue []))
(defn- schedule-update! [schedule! scheduled? f]
(when-let [id @scheduled?]
(vreset! scheduled? nil)
(js/cancelAnimationFrame id))
(vreset! scheduled? (schedule! f)))
(defprotocol IReconciler
(dispatch! [this controller event args])
(dispatch-sync! [this controller event args])
(broadcast! [this event args])
(broadcast-sync! [this event args]))
(deftype Reconciler [controllers effect-handlers state queue scheduled? batched-updates chunked-updates meta]
Object
(equiv [this other]
(-equiv this other))
IAtom
IMeta
(-meta [_] meta)
IEquiv
(-equiv [this other]
(identical? this other))
IDeref
(-deref [_]
(-deref state))
IWatchable
(-add-watch [this key callback]
(add-watch state (list this key)
(fn [_ _ oldv newv]
(when (not= oldv newv)
(callback key this oldv newv))))
this)
(-remove-watch [this key]
(remove-watch state (list this key))
this)
IHash
(-hash [this] (goog/getUid this))
IPrintWithWriter
(-pr-writer [this writer opts]
(-write writer "#object [citrus.reconciler.Reconciler ")
(pr-writer {:val (-deref this)} writer opts)
(-write writer "]"))
IReconciler
(dispatch! [this cname event args]
(queue-effects!
queue
[cname #((get controllers cname) event args (get @state cname))])
(schedule-update!
batched-updates
scheduled?
(fn []
(let [effects
(map (fn [[cname ctrl]] [cname (ctrl)]) @queue)]
(clear-queue! queue)
(let [state-effects (filter (comp :state second) effects)
other-effects (->> effects
(map (fn [[cname effect]]
[cname (dissoc effect :state)]))
(filter (comp seq second)))]
(when (seq state-effects)
(swap! state
#(reduce (fn [agg [cname {cstate :state}]]
(assoc agg cname cstate))
% state-effects)))
(when (seq other-effects)
(m/doseq [[cname effects] effects]
(m/doseq [[id effect] effects]
(when-let [handler (get effect-handlers id)]
(handler this cname effect))))))))))
(dispatch-sync! [this cname event args]
(let [effects ((get controllers cname) event args (get @state cname))]
(m/doseq [[id effect] effects]
(let [handler (get effect-handlers id)]
(cond
(= id :state) (swap! state assoc cname effect)
handler (handler this cname effect)
:else nil)))))
(broadcast! [this event args]
(m/doseq [controller (keys controllers)]
(dispatch! this controller event args)))
(broadcast-sync! [this event args]
(m/doseq [controller (keys controllers)]
(dispatch-sync! this controller event args))))