2015-03-04 13:00:36 +00:00
|
|
|
(ns re-frame.router
|
|
|
|
(:refer-clojure :exclude [flush])
|
2015-11-02 11:37:46 +00:00
|
|
|
(:require [reagent.core :refer [flush]]
|
|
|
|
[reagent.impl.batching :refer [do-later]]
|
|
|
|
[re-frame.handlers :refer [handle]]
|
|
|
|
[re-frame.utils :refer [warn error]]
|
2015-09-27 08:39:35 +00:00
|
|
|
[goog.async.nextTick]))
|
2015-03-04 13:00:36 +00:00
|
|
|
|
2015-11-02 11:37:46 +00:00
|
|
|
|
|
|
|
;; -- Router Loop ------------------------------------------------------------
|
2015-03-04 13:00:36 +00:00
|
|
|
;;
|
2015-11-02 11:37:46 +00:00
|
|
|
;; Conceptually, the task is to process events in a perpetual loop, one after
|
|
|
|
;; the other, FIFO, calling the right event-handler for each. Being idle when
|
2015-11-02 12:43:19 +00:00
|
|
|
;; ther are no events, and firing up when one arrives, etc. The processing
|
2015-11-02 11:37:46 +00:00
|
|
|
;; of events happens "asynchronously" sometime after an event is dispatched.
|
2015-03-04 13:00:36 +00:00
|
|
|
;;
|
2015-11-02 11:37:46 +00:00
|
|
|
;; In practice, browsers only have a single thread of control and we must be
|
|
|
|
;; careful to not hog the CPU.
|
|
|
|
;; When processing events one after another, we must hand back control to
|
|
|
|
;; the browser regularly, so it can redraw, process websockets, etc. But not
|
2015-11-02 12:43:19 +00:00
|
|
|
;; too regularly! If we are in a de-focused browser tab, then our app
|
2015-11-02 11:37:46 +00:00
|
|
|
;; will be CPU throttled. Each time we get back control, we have to process all
|
|
|
|
;; queued events, or else something like a bursty websocket (producing events)
|
|
|
|
;; might overwhelm the queue. So there's a balance.
|
2015-03-04 13:00:36 +00:00
|
|
|
;;
|
2015-11-02 11:37:46 +00:00
|
|
|
;; The original implementation of this router loop used core.async. It
|
|
|
|
;; was fairly simple, and it mostly worked, but it did not give enough
|
|
|
|
;; control. So now we hand-roll our own, mini finite-state-machine and all.
|
2015-03-04 13:00:36 +00:00
|
|
|
;;
|
2015-11-02 11:37:46 +00:00
|
|
|
;; The strategy is this:
|
|
|
|
;; - maintain a queue of `dispatched` events.
|
2015-11-02 12:43:19 +00:00
|
|
|
;; - when a new event arrives, "schedule" processing of this queue using
|
|
|
|
;; goog.async.nextTick, which means it will happen "very soon".
|
|
|
|
;; - when processing events, do ALL the ones currently queued. Don't stop.
|
|
|
|
;; Don't yield to the browser. Hog that CPU.
|
|
|
|
;; - but if any new events arrive during this cycle of processing,
|
|
|
|
;; don't do them immediately. Leave then queued. Yield first to the
|
|
|
|
;; browser, and do these new events in the next processing cycle.
|
|
|
|
;; That way we drain the queue up to a point, but we
|
2015-11-02 11:37:46 +00:00
|
|
|
;; never hog the CPU forever. In particular, we handle the case
|
2015-11-02 12:43:19 +00:00
|
|
|
;; where handling one event will begat another event. The freshly begated
|
|
|
|
;; event will be handled next cycle, with yielding in between.
|
2015-11-02 11:37:46 +00:00
|
|
|
;; - In some cases, an event should not be run until after the GUI has been
|
|
|
|
;; updated. Ie. after the next reagent animation frame. In such a case,
|
|
|
|
;; the event should be dispatched with :flush-dom metadata like this:
|
|
|
|
;; (dispatch ^:flush-dom [:event-id other params])
|
|
|
|
;; Such an event will block all further processing, because events are
|
|
|
|
;; processed sequentially. We must do one event before we can handle the
|
|
|
|
;; ones behind it.
|
2015-03-04 13:00:36 +00:00
|
|
|
;;
|
2015-11-02 11:37:46 +00:00
|
|
|
;; Implementation
|
2015-11-04 02:33:50 +00:00
|
|
|
;; - queue processing can be in a number of states: scheduled, running, paused
|
|
|
|
;; etc. So it is modeled explicitly as a FSM.
|
|
|
|
;; See "-fsm-trigger" (below) for the states and transitions.
|
2015-11-02 11:37:46 +00:00
|
|
|
;; - 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"
|
2015-11-04 02:33:50 +00:00
|
|
|
;; which will run event processing after the next reagent animation frame.
|
2015-11-02 11:37:46 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
|
|
(defprotocol IEventQueue
|
|
|
|
(enqueue [this event])
|
|
|
|
|
|
|
|
;; Finite State Machine transitions
|
|
|
|
(-fsm-trigger [this trigger arg])
|
|
|
|
|
|
|
|
;; Finite State Machine (FSM) actions
|
|
|
|
(-add-event [this event])
|
|
|
|
(-process-1st-event [this])
|
|
|
|
(-run-next-tick [this])
|
|
|
|
(-run-queue [this])
|
|
|
|
(-pause-run [this])
|
|
|
|
(-exception [this ex])
|
2015-11-04 06:11:42 +00:00
|
|
|
(-begin-resume [this]))
|
2015-11-02 11:37:46 +00:00
|
|
|
|
|
|
|
|
2015-11-02 12:43:19 +00:00
|
|
|
;; Want to understand this? Look at FSM in -fsm-trigger?
|
2015-11-02 11:37:46 +00:00
|
|
|
(deftype EventQueue [^:mutable fsm-state ^:mutable queue]
|
|
|
|
IEventQueue
|
|
|
|
|
|
|
|
(enqueue [this event]
|
|
|
|
(-fsm-trigger this :add-event event))
|
|
|
|
|
|
|
|
;; Finite State Machine "Actions"
|
|
|
|
(-add-event
|
|
|
|
[this event]
|
|
|
|
(set! queue (conj queue event)))
|
|
|
|
|
|
|
|
(-process-1st-event
|
|
|
|
[this]
|
|
|
|
(let [event-v (peek queue)]
|
|
|
|
(try
|
|
|
|
(handle event-v)
|
|
|
|
(catch :default ex
|
|
|
|
(-fsm-trigger this :exception ex)))
|
|
|
|
(set! queue (pop queue))))
|
|
|
|
|
|
|
|
(-run-next-tick
|
|
|
|
[this]
|
|
|
|
(goog.async.nextTick #(-fsm-trigger this :begin-run nil)))
|
|
|
|
|
|
|
|
(-exception
|
|
|
|
[_ ex]
|
|
|
|
(set! queue #queue []) ;; purge the queue
|
|
|
|
(throw ex))
|
|
|
|
|
|
|
|
;; Process all the events currently in the queue, but not any new ones.
|
|
|
|
;; Be aware that events might have metadata which will pause processing.
|
|
|
|
(-run-queue
|
|
|
|
[this]
|
|
|
|
(let [queue-length (count queue)]
|
|
|
|
(loop [n queue-length]
|
|
|
|
(if (zero? n)
|
|
|
|
(-fsm-trigger this :finish-run nil)
|
|
|
|
(let [event-v (peek queue)]
|
|
|
|
(if (some #{:flush-dom :yield} (keys (meta event-v)))
|
|
|
|
(-fsm-trigger this :pause-run nil)
|
|
|
|
(do (-process-1st-event this)
|
|
|
|
(recur (dec n)))))))))
|
|
|
|
|
|
|
|
(-pause-run
|
|
|
|
[this]
|
|
|
|
(let [event-v (peek queue)
|
|
|
|
m (meta event-v)
|
|
|
|
later (cond
|
|
|
|
(:flush-dom m) do-later ;; after next annimation frame
|
|
|
|
(:yield m) goog.async.nextTick)] ;; almost immediately
|
2015-11-04 05:14:11 +00:00
|
|
|
(later #(-fsm-trigger this :begin-resume nil))))
|
2015-11-02 11:37:46 +00:00
|
|
|
|
2015-11-04 06:11:42 +00:00
|
|
|
(-begin-resume
|
2015-11-02 11:37:46 +00:00
|
|
|
[this]
|
|
|
|
(-process-1st-event this) ;; do the event which paused processing
|
2015-11-04 05:14:11 +00:00
|
|
|
(-fsm-trigger this :finish-resume nil)) ;; do the rest of the queued events
|
2015-11-02 11:37:46 +00:00
|
|
|
|
|
|
|
(-fsm-trigger
|
|
|
|
[this trigger arg1]
|
|
|
|
|
|
|
|
;; work out new FSM state and action function for the transition
|
|
|
|
(let [[new-state action-fn]
|
2015-11-04 06:11:42 +00:00
|
|
|
(case [fsm-state trigger]
|
2015-11-02 11:37:46 +00:00
|
|
|
|
|
|
|
; Here is the FSM
|
|
|
|
;[current-state :trigger] [:new-state action-fn]
|
|
|
|
|
|
|
|
[:quiescent :add-event] [:scheduled #(do (-add-event this arg1) (-run-next-tick this))]
|
|
|
|
|
|
|
|
;; processing has been already been scheduled to run in the future
|
|
|
|
[:scheduled :add-event] [:scheduled #(-add-event this arg1)]
|
|
|
|
[:scheduled :begin-run] [:running #(-run-queue this)]
|
|
|
|
|
|
|
|
;; processing one event after another
|
|
|
|
[:running :add-event ] [:running #(-add-event this arg1)]
|
|
|
|
[:running :pause-run ] [:paused #(-pause-run this)]
|
|
|
|
[:running :exception ] [:quiescent #(-exception this arg1)]
|
|
|
|
[:running :finish-run] (if (empty? queue) ;; FSM guard
|
|
|
|
[:quiescent]
|
|
|
|
[:scheduled #(-run-next-tick this)])
|
|
|
|
|
|
|
|
;; event processing is paused - probably by :flush-dom metadata
|
2015-11-04 05:14:11 +00:00
|
|
|
[:paused :add-event ] [:paused #(-add-event this arg1)]
|
2015-11-04 06:11:42 +00:00
|
|
|
[:paused :begin-resume ] [:resuming #(-begin-resume this)]
|
2015-11-02 11:37:46 +00:00
|
|
|
|
|
|
|
;; processing an event which previously caused the queue to be paused
|
2015-11-04 05:14:11 +00:00
|
|
|
[:resuming :add-event ] [:resuming #(-add-event this arg1)]
|
|
|
|
[:resuming :exception ] [:quiescent #(-exception this arg1)]
|
|
|
|
[:resuming :finish-resume] [:running #(-run-queue this)]
|
2015-11-02 11:37:46 +00:00
|
|
|
|
|
|
|
(throw (str "re-frame: state transition not found. " fsm-state " " trigger)))]
|
|
|
|
|
|
|
|
;; change state and run the action fucntion
|
|
|
|
(set! fsm-state new-state)
|
|
|
|
(when action-fn (action-fn)))))
|
|
|
|
|
|
|
|
;; ---------------------------------------------------------------------------
|
|
|
|
;; This is the global queue for events
|
|
|
|
;; When an event is dispatched, it is put into this queue. Later the queue
|
|
|
|
;; will "run" and the event will be "handled" by the registered event handler.
|
2015-03-04 13:00:36 +00:00
|
|
|
;;
|
2015-05-02 00:52:11 +00:00
|
|
|
|
2015-11-02 11:37:46 +00:00
|
|
|
(def event-queue (->EventQueue :quiescent #queue []))
|
|
|
|
|
|
|
|
|
|
|
|
;; ---------------------------------------------------------------------------
|
|
|
|
;; Dispatching
|
|
|
|
;;
|
2015-03-04 13:00:36 +00:00
|
|
|
|
|
|
|
(defn dispatch
|
|
|
|
"Send an event to be processed by the registered handler.
|
|
|
|
|
|
|
|
Usage example:
|
|
|
|
(dispatch [:delete-item 42])
|
|
|
|
"
|
|
|
|
[event-v]
|
|
|
|
(if (nil? event-v)
|
2015-11-02 11:37:46 +00:00
|
|
|
(error "re-frame: \"dispatch\" is ignoring a nil event.") ;; nil would close the channel
|
|
|
|
(enqueue event-queue event-v))
|
|
|
|
nil) ;; Ensure nil return. See https://github.com/Day8/re-frame/wiki/Beware-Returning-False
|
2015-03-04 13:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defn dispatch-sync
|
2015-03-06 01:44:22 +00:00
|
|
|
"Send an event to be processed by the registered handler, but avoid the async-inducing
|
|
|
|
use of core.async/chan.
|
|
|
|
|
|
|
|
Usage example:
|
|
|
|
(dispatch-sync [:delete-item 42])"
|
2015-03-04 13:00:36 +00:00
|
|
|
[event-v]
|
2015-03-06 01:44:22 +00:00
|
|
|
(handle event-v)
|
2015-11-02 11:37:46 +00:00
|
|
|
nil) ;; Ensure nil return. See https://github.com/Day8/re-frame/wiki/Beware-Returning-False
|
2015-03-04 13:00:36 +00:00
|
|
|
|