-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
batching.cljs
125 lines (97 loc) · 3.17 KB
/
batching.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
(ns ^{:mranderson/inlined true} day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.impl.batching
(:refer-clojure :exclude [flush])
(:require [day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.debug :refer-macros [assert-some]]
[day8.re-frame-10x.inlined-deps.reagent.v1v0v0.reagent.impl.util :refer [is-client]]))
;;; Update batching
(defonce mount-count 0)
(defn next-mount-count []
(set! mount-count (inc mount-count)))
(defn fake-raf [f]
(js/setTimeout f 16))
(def next-tick
(if-not is-client
fake-raf
(let [w js/window]
(.bind (or (.-requestAnimationFrame w)
(.-webkitRequestAnimationFrame w)
(.-mozRequestAnimationFrame w)
(.-msRequestAnimationFrame w)
fake-raf)
w))))
(defn compare-mount-order
[^clj c1 ^clj c2]
(- (.-cljsMountOrder c1)
(.-cljsMountOrder c2)))
(defn run-queue [a]
;; sort components by mount order, to make sure parents
;; are rendered before children
(.sort a compare-mount-order)
(dotimes [i (alength a)]
(let [^js/React.Component c (aget a i)]
(when (true? (.-cljsIsDirty c))
(.forceUpdate c)))))
;; Set from ratom.cljs
(defonce ratom-flush (fn []))
(defn run-funs [fs]
(dotimes [i (alength fs)]
((aget fs i))))
(defn enqueue [^clj queue fs f]
(assert-some f "Enqueued function")
(.push fs f)
(.schedule queue))
(deftype RenderQueue [^:mutable ^boolean scheduled?]
Object
(schedule [this]
(when-not scheduled?
(set! scheduled? true)
(next-tick #(.run-queues this))))
(queue-render [this c]
(when (nil? (.-componentQueue this))
(set! (.-componentQueue this) (array)))
(enqueue this (.-componentQueue this) c))
(add-before-flush [this f]
(when (nil? (.-beforeFlush this))
(set! (.-beforeFlush this) (array)))
(enqueue this (.-beforeFlush this) f))
(add-after-render [this f]
(when (nil? (.-afterRender this))
(set! (.-afterRender this) (array)))
(enqueue this (.-afterRender this) f))
(run-queues [this]
(set! scheduled? false)
(.flush-queues this))
(flush-before-flush [this]
(when-some [fs (.-beforeFlush this)]
(set! (.-beforeFlush this) nil)
(run-funs fs)))
(flush-render [this]
(when-some [fs (.-componentQueue this)]
(set! (.-componentQueue this) nil)
(run-queue fs)))
(flush-after-render [this]
(when-some [fs (.-afterRender this)]
(set! (.-afterRender this) nil)
(run-funs fs)))
(flush-queues [this]
(.flush-before-flush this)
(ratom-flush)
(.flush-render this)
(.flush-after-render this)))
(defonce render-queue (->RenderQueue false))
(defn flush []
(.flush-queues render-queue))
(defn flush-after-render []
(.flush-after-render render-queue))
(defn queue-render [^clj c]
(when-not (.-cljsIsDirty c)
(set! (.-cljsIsDirty c) true)
(.queue-render render-queue c)))
(defn mark-rendered [^clj c]
(set! (.-cljsIsDirty c) false))
(defn do-before-flush [f]
(.add-before-flush render-queue f))
(defn do-after-render [f]
(.add-after-render render-queue f))
(defn schedule []
(when (false? (.-scheduled? render-queue))
(.schedule render-queue)))