Skip to content

Commit

Permalink
Merge pull request #134 from scgilardi/develop
Browse files Browse the repository at this point in the history
simplify the state machine
  • Loading branch information
mike-thompson-day8 committed Dec 6, 2015
2 parents 780ae49 + e77fc53 commit 0d22487
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/re_frame/router.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
;; etc. So it is modeled explicitly as a FSM.
;; See "-fsm-trigger" (below) for the states and transitions.
;; - the scheduling is done via "goog.async.nextTick" which is pretty quick
;; - when the event has :dom-flush we schedule via "reagent.impl.batching.doLater"
;; - when the event has :flush-dom we schedule via
;; "reagent.impl.batching.do-later"
;; which will run event processing after the next reagent animation frame.
;;

Expand All @@ -72,9 +73,9 @@
(-process-1st-event [this])
(-run-next-tick [this])
(-run-queue [this])
(-pause-run [this later-fn])
(-exception [this ex])
(-begin-resume [this]))
(-pause [this later-fn])
(-resume [this]))


;; Want to understand this? Look at FSM in -fsm-trigger?
Expand All @@ -100,12 +101,7 @@

(-run-next-tick
[this]
(goog.async.nextTick #(-fsm-trigger this :begin-run nil)))

(-exception
[_ ex]
(set! queue #queue []) ;; purge the queue
(throw ex))
(goog.async.nextTick #(-fsm-trigger this :run-queue nil)))

;; Process all the events currently in the queue, but not any new ones.
;; Be aware that events might have metadata which will pause processing.
Expand All @@ -115,18 +111,23 @@
(if (zero? n)
(-fsm-trigger this :finish-run nil)
(if-let [later-fn (some later-fns (-> queue peek meta keys))]
(-fsm-trigger this :pause-run later-fn)
(-fsm-trigger this :pause later-fn)
(do (-process-1st-event this)
(recur (dec n)))))))

(-pause-run
(-exception
[_ ex]
(set! queue #queue []) ;; purge the queue
(throw ex))

(-pause
[this later-fn]
(later-fn #(-fsm-trigger this :begin-resume nil)))
(later-fn #(-fsm-trigger this :resume nil)))

(-begin-resume
(-resume
[this]
(-process-1st-event this) ;; do the event which paused processing
(-fsm-trigger this :finish-resume nil)) ;; do the rest of the queued events
(-process-1st-event this) ;; do the event which paused processing
(-run-queue this)) ;; do the rest of the queued events

(-fsm-trigger
[this trigger arg]
Expand All @@ -142,26 +143,21 @@
[:quiescent :add-event] [:scheduled #(do (-add-event this arg)
(-run-next-tick this))]

;; processing has been already been scheduled to run in the future
;; processing has already been scheduled to run in the future
[:scheduled :add-event] [:scheduled #(-add-event this arg)]
[:scheduled :begin-run] [:running #(-run-queue this)]
[:scheduled :run-queue] [:running #(-run-queue this)]

;; processing one event after another
[:running :add-event ] [:running #(-add-event this arg)]
[:running :pause-run ] [:paused #(-pause-run this arg)]
[:running :pause ] [:paused #(-pause this arg)]
[:running :exception ] [:quiescent #(-exception this arg)]
[:running :finish-run] (if (empty? queue) ;; FSM guard
[:quiescent]
[:scheduled #(-run-next-tick this)])

;; event processing is paused - probably by :flush-dom metadata
[:paused :add-event ] [:paused #(-add-event this arg)]
[:paused :begin-resume] [:resuming #(-begin-resume this)]

;; processing the event that caused the queue to be paused
[:resuming :add-event ] [:resuming #(-add-event this arg)]
[:resuming :exception ] [:quiescent #(-exception this arg)]
[:resuming :finish-resume] [:running #(-run-queue this)]
[:paused :add-event] [:paused #(-add-event this arg)]
[:paused :resume ] [:running #(-resume this)]

(throw (str "re-frame: state transition not found. " fsm-state " " trigger)))]

Expand Down

0 comments on commit 0d22487

Please sign in to comment.