From 17561673f940ef95115e34a7b061864b7e4b61da Mon Sep 17 00:00:00 2001 From: Chris Lowe Date: Sat, 7 Dec 2024 10:29:40 +0000 Subject: [PATCH] Remove executor For https://github.com/day8/re-frame/issues/809 --- src/re_frame/interop.clj | 17 +-------- src/re_frame/router.cljc | 81 ++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 56 deletions(-) diff --git a/src/re_frame/interop.clj b/src/re_frame/interop.clj index 1f37e3982..38abfad5e 100644 --- a/src/re_frame/interop.clj +++ b/src/re_frame/interop.clj @@ -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. @@ -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) @@ -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)) - diff --git a/src/re_frame/router.cljc b/src/re_frame/router.cljc index 9caddc607..fd60e62a2 100644 --- a/src/re_frame/router.cljc +++ b/src/re_frame/router.cljc @@ -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]