Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xiana hotreload/livereload #287

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/xiana/hotreload.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(ns xiana.hotreload
(:require
[clojure.core.async :refer [go-loop <! timeout]]
[ns-tracker.core :refer [ns-tracker]]))

;; reloader function from ring.middleware.reload

(defn- reloader
"Reload namespaces of modified files before the request is passed to the
supplied handler.

Accepts the following options:

:dirs - A list of directories that contain the source files.
Defaults to [\"src\"].
:retry? - If true, keep attempting to reload namespaces
that have compile errors. Defaults to true."
[dirs retry?]
(let [modified-namespaces (ns-tracker dirs)
load-queue (java.util.concurrent.LinkedBlockingDeque.)]
(fn []
(locking load-queue
(doseq [ns-sym (reverse (modified-namespaces))]
(.push load-queue ns-sym))
(loop []
(when-let [ns-sym (.peek load-queue)]
(if retry?
(do (require ns-sym :reload) (.remove load-queue))
(do (.remove load-queue) (require ns-sym :reload)))
(recur)))))))

(defn hotreload
"Function to hotreload the system. Add it after server-start at configuration phase \"->system\".
(-> (config/config app-cfg)
...
xiana.webserver/start
xiana.hotreload/hotreload)
If a :xiana/hotreload config key is provided it needs:
:restart-fn \"the function to restart the system\" if none 'user/start-dev-system
:tracker {:dirs \"the directories vector list to search for changes\" :reload-compile-errors? \"true\"}."
[cfg]
(let [{:keys [restart-fn tracker]} (:xiana/hotreload cfg)
dirs (:dirs tracker ["src"])
retry? (:reload-compile-errors? tracker true)
track-fn (ns-tracker dirs)
reload! (reloader dirs retry?)
restart-fn (when restart-fn (resolve restart-fn))]
(when restart-fn
(go-loop []
(<! (timeout 1000))
(if (track-fn)
(do
(reload!)
(restart-fn))
(recur))))
cfg))
1 change: 0 additions & 1 deletion test/xiana/interceptor/kebab_camel_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@
leave (:leave kc/interceptor)
result (leave state)]
(is (= expected result)))))

1 change: 0 additions & 1 deletion test/xiana/route_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,3 @@
expected helpers/not-found]
;; verify if action has the expected value
(is (= action expected))))

Loading