Skip to content

Commit

Permalink
Remove executor
Browse files Browse the repository at this point in the history
  • Loading branch information
lowecg committed Dec 7, 2024
1 parent 078ebc1 commit 1756167
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 56 deletions.
17 changes: 2 additions & 15 deletions src/re_frame/interop.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(ns re-frame.interop
(:import [java.util.concurrent Executor ExecutorService Executors TimeUnit]))
(ns re-frame.interop)

;; The purpose of this file is to provide JVM-runnable implementations of the
;; CLJS equivalents in interop.cljs.
Expand All @@ -22,13 +21,10 @@
(defn on-load
[listener]) ;; no-op

(defonce ^:private executor (Executors/newSingleThreadExecutor))

(defonce ^:private on-dispose-callbacks (atom {}))

(defn next-tick [f]
(let [bound-f (bound-fn [& args] (apply f args))]
(.execute ^Executor executor bound-f))
(f)
nil)

(def empty-queue clojure.lang.PersistentQueue/EMPTY)
Expand Down Expand Up @@ -95,12 +91,3 @@
[]
true)

(defn shutdown-executor!
"Cleanly terminates the executor service and waits for pending tasks to complete.
Required when running from CLI, tests or Lambda functions to prevent the JVM from hanging,
since the executor runs on non-daemon threads. The await-timeout-ms parameter sets how long
to wait for task completion in milliseconds. Call reset-executor! to restart execution capabilities."
[await-timeout-ms]
(.shutdown ^ExecutorService executor)
(.awaitTermination ^ExecutorService executor await-timeout-ms TimeUnit/MILLISECONDS))

81 changes: 40 additions & 41 deletions src/re_frame/router.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -123,47 +123,46 @@
;; Given a "trigger", and the existing FSM state, it computes the
;; new FSM state and the transition action (function).

(locking this
(trace/with-trace {:op-type ::fsm-trigger}
(let [[new-fsm-state action-fn]
(case [fsm-state trigger]

;; You should read the following "case" as:
;; [current-FSM-state trigger] -> [new-FSM-state action-fn]
;;
;; So, for example, the next line should be interpreted as:
;; if you are in state ":idle" and a trigger ":add-event"
;; happens, then move the FSM to state ":scheduled" and execute
;; that two-part "do" function.
[:idle :add-event] [:scheduled #(do (-add-event this arg)
(-run-next-tick this))]

;; State: :scheduled (the queue is scheduled to run, soon)
[:scheduled :add-event] [:scheduled #(-add-event this arg)]
[:scheduled :run-queue] [:running #(-run-queue this)]

;; State: :running (the queue is being processed one event after another)
[:running :add-event] [:running #(-add-event this arg)]
[:running :pause] [:paused #(-pause this arg)]
[:running :exception] [:idle #(-exception this arg)]
[:running :finish-run] (if (empty? queue) ;; FSM guard
[:idle]
[:scheduled #(-run-next-tick this)])

;; State: :paused (:flush-dom metadata on an event has caused a temporary pause in processing)
[:paused :add-event] [:paused #(-add-event this arg)]
[:paused :resume] [:running #(-resume this)]

(throw (ex-info (str "re-frame: router state transition not found. " fsm-state " " trigger)
{:fsm-state fsm-state, :trigger trigger})))]

;; The "case" above computed both the new FSM state, and the action. Now, make it happen.

(trace/merge-trace! {:operation [fsm-state trigger]
:tags {:current-state fsm-state
:new-state new-fsm-state}})
(set! fsm-state new-fsm-state)
(when action-fn (action-fn))))))
(trace/with-trace {:op-type ::fsm-trigger}
(let [[new-fsm-state action-fn]
(case [fsm-state trigger]

;; You should read the following "case" as:
;; [current-FSM-state trigger] -> [new-FSM-state action-fn]
;;
;; So, for example, the next line should be interpreted as:
;; if you are in state ":idle" and a trigger ":add-event"
;; happens, then move the FSM to state ":scheduled" and execute
;; that two-part "do" function.
[:idle :add-event] [:scheduled #(do (-add-event this arg)
(-run-next-tick this))]

;; State: :scheduled (the queue is scheduled to run, soon)
[:scheduled :add-event] [:scheduled #(-add-event this arg)]
[:scheduled :run-queue] [:running #(-run-queue this)]

;; State: :running (the queue is being processed one event after another)
[:running :add-event] [:running #(-add-event this arg)]
[:running :pause] [:paused #(-pause this arg)]
[:running :exception] [:idle #(-exception this arg)]
[:running :finish-run] (if (empty? queue) ;; FSM guard
[:idle]
[:scheduled #(-run-next-tick this)])

;; State: :paused (:flush-dom metadata on an event has caused a temporary pause in processing)
[:paused :add-event] [:paused #(-add-event this arg)]
[:paused :resume] [:running #(-resume this)]

(throw (ex-info (str "re-frame: router state transition not found. " fsm-state " " trigger)
{:fsm-state fsm-state, :trigger trigger})))]

;; The "case" above computed both the new FSM state, and the action. Now, make it happen.

(trace/merge-trace! {:operation [fsm-state trigger]
:tags {:current-state fsm-state
:new-state new-fsm-state}})
(set! fsm-state new-fsm-state)
(when action-fn (action-fn)))))

(-add-event
[_ event]
Expand Down

0 comments on commit 1756167

Please sign in to comment.