forked from reagent-project/reagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatching.cljs
More file actions
114 lines (88 loc) · 2.71 KB
/
batching.cljs
File metadata and controls
114 lines (88 loc) · 2.71 KB
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
(ns reagent.impl.batching
(:refer-clojure :exclude [flush])
(:require [reagent.debug :refer-macros [dbg assert-some]]
[reagent.impl.util :refer [is-client]]
[clojure.string :as string]
[goog.object :as gobj]))
;;; 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]
(or (.-requestAnimationFrame w)
(.-webkitRequestAnimationFrame w)
(.-mozRequestAnimationFrame w)
(.-msRequestAnimationFrame w)
fake-raf))))
(defn compare-mount-order [c1 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 [c (aget a i)]
(when (true? (.-cljsIsDirty c))
(.forceUpdate c)))))
;; Set from ratom.cljs
(defonce ratom-flush (fn []))
;; TODO: Use interop properties instead of gobj
(deftype RenderQueue [^:mutable ^boolean scheduled?]
Object
(enqueue [this k f]
(assert-some f "Enqueued function")
(when (nil? (gobj/get this k))
(gobj/set this k (array)))
(.push (gobj/get this k) f)
(.schedule this))
(run-funs [this k]
(when-some [^array fs (gobj/get this k)]
(gobj/set this k nil)
(dotimes [i (alength fs)]
((gobj/get fs i)))))
(schedule [this]
(when-not scheduled?
(set! scheduled? true)
(next-tick #(.run-queues this))))
(queue-render [this c]
(.enqueue this "componentQueue" c))
(add-before-flush [this f]
(.enqueue this "beforeFlush" f))
(add-after-render [this f]
(.enqueue this "afterRender" f))
(run-queues [this]
(set! scheduled? false)
(.flush-queues this))
(flush-after-render [this]
(.run-funs this "afterRender"))
(flush-queues [this]
(.run-funs this "beforeFlush")
(ratom-flush)
(when-some [cs (gobj/get this "componentQueue")]
(gobj/set this "componentQueue" nil)
(run-queue cs))
(.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 [c]
(when-not (.-cljsIsDirty c)
(set! (.-cljsIsDirty c) true)
(.queue-render render-queue c)))
(defn mark-rendered [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)))