optimize and DRY up pause-run handling

This commit is contained in:
Stephen C. Gilardi 2015-11-04 08:34:23 -05:00
parent ceafa00483
commit 08e8b450ee
1 changed files with 11 additions and 11 deletions

View File

@ -56,6 +56,11 @@
;; ;;
;; A map from event metadata keys to the corresponding "run later" functions
(def later-fns
{:flush-dom do-later ;; after next annimation frame
:yield goog.async.nextTick}) ;; almost immediately
(defprotocol IEventQueue (defprotocol IEventQueue
(enqueue [this event]) (enqueue [this event])
@ -67,7 +72,7 @@
(-process-1st-event [this]) (-process-1st-event [this])
(-run-next-tick [this]) (-run-next-tick [this])
(-run-queue [this]) (-run-queue [this])
(-pause-run [this]) (-pause-run [this later-fn])
(-exception [this ex]) (-exception [this ex])
(-begin-resume [this])) (-begin-resume [this]))
@ -111,19 +116,14 @@
(if (zero? n) (if (zero? n)
(-fsm-trigger this :finish-run nil) (-fsm-trigger this :finish-run nil)
(let [event-v (peek queue)] (let [event-v (peek queue)]
(if (some #{:flush-dom :yield} (keys (meta event-v))) (if-let [later-fn (some later-fns (keys (meta event-v)))]
(-fsm-trigger this :pause-run nil) (-fsm-trigger this :pause-run later-fn)
(do (-process-1st-event this) (do (-process-1st-event this)
(recur (dec n))))))))) (recur (dec n)))))))))
(-pause-run (-pause-run
[this] [this later-fn]
(let [event-v (peek queue) (later-fn #(-fsm-trigger this :begin-resume nil)))
m (meta event-v)
later (cond
(:flush-dom m) do-later ;; after next annimation frame
(:yield m) goog.async.nextTick)] ;; almost immediately
(later #(-fsm-trigger this :begin-resume nil))))
(-begin-resume (-begin-resume
[this] [this]
@ -148,11 +148,11 @@
;; processing one event after another ;; processing one event after another
[:running :add-event ] [:running #(-add-event this arg1)] [:running :add-event ] [:running #(-add-event this arg1)]
[:running :pause-run ] [:paused #(-pause-run this)]
[:running :exception ] [:quiescent #(-exception this arg1)] [:running :exception ] [:quiescent #(-exception this arg1)]
[:running :finish-run] (if (empty? queue) ;; FSM guard [:running :finish-run] (if (empty? queue) ;; FSM guard
[:quiescent] [:quiescent]
[:scheduled #(-run-next-tick this)]) [:scheduled #(-run-next-tick this)])
[:running :pause-run ] [:paused #(-pause-run this arg1)]
;; event processing is paused - probably by :flush-dom metadata ;; event processing is paused - probably by :flush-dom metadata
[:paused :add-event ] [:paused #(-add-event this arg1)] [:paused :add-event ] [:paused #(-add-event this arg1)]