Skip to content

Commit

Permalink
Made tx processing scheduling have the same semantics in CLJ as CLJS.…
Browse files Browse the repository at this point in the history
… This fixes erratic behavior for running Fulcro in the JVM
  • Loading branch information
awkay committed May 15, 2024
1 parent d474466 commit 481428b
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/main/com/fulcrologic/fulcro/algorithms/scheduling.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@
[clojure.core.async :as async]
[taoensso.timbre :as log]))

(defn defer
"Schedule f to run in `tm` ms."
[f tm]
#?(:cljs (js/setTimeout f tm)
:clj (let [active (atom true)
cancel (fn [] (reset! active false))]
(async/go
(async/<! (async/timeout tm))
(when (and @active f)
(try
(f)
(catch Exception e
(log/error e "Deferred function crash")))))
cancel)))
#?(:cljs
(defn defer
"Schedule f to run in `tm` ms."
[f tm]
(js/setTimeout f tm))
:clj
(do
(defonce timeout-queue (async/chan 100))
(defonce loop (async/go-loop []
(let [{:keys [active f]} (async/<! timeout-queue)]
(when @active
(try
(f)
(catch Exception e
(log/error e "Deferred function crash")))))
(recur)))
(defn defer
"Schedule f to run in `tm` ms."
[f tm]
(let [active (volatile! true)
cancel (fn [] (reset! active false))]
(async/go
(async/<! (async/timeout tm))
(async/>! timeout-queue {:active active
:f f}))
cancel))))

(defn schedule!
"Schedule the processing of a specific action in the runtime atom. This is a no-op if the item is already scheduled.
Expand Down

0 comments on commit 481428b

Please sign in to comment.