From 0f62d6d043abb6156393fd167f6c1496c5439689 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Sat, 2 Dec 2017 12:34:27 -0800 Subject: [PATCH] FEATURE: configurable wait time for file changes (#625) Looks good! --- .../conf-fig-docs/FigwheelOptions.txt | 7 ++ .../components/cljs_autobuild.clj | 3 +- .../components/file_system_watcher.clj | 3 +- .../src/figwheel_sidecar/schemas/config.clj | 11 ++- sidecar/src/figwheel_sidecar/watching.clj | 92 ++++++++++--------- 5 files changed, 69 insertions(+), 47 deletions(-) diff --git a/sidecar/resources/conf-fig-docs/FigwheelOptions.txt b/sidecar/resources/conf-fig-docs/FigwheelOptions.txt index face803c..578a0198 100644 --- a/sidecar/resources/conf-fig-docs/FigwheelOptions.txt +++ b/sidecar/resources/conf-fig-docs/FigwheelOptions.txt @@ -140,3 +140,10 @@ to prevent ANSI color codes in figwheel output set :ansi-color-output to false. Default: true :ansi-color-output false + +:wait-time-ms + +The number of milliseconds to wait before issuing reloads. Set this higher +to wait longer for changes. Default: 50 + + :wait-time-ms 50 diff --git a/sidecar/src/figwheel_sidecar/components/cljs_autobuild.clj b/sidecar/src/figwheel_sidecar/components/cljs_autobuild.clj index 11515614..1f3056c8 100644 --- a/sidecar/src/figwheel_sidecar/components/cljs_autobuild.clj +++ b/sidecar/src/figwheel_sidecar/components/cljs_autobuild.clj @@ -277,7 +277,8 @@ (watching/watch! (:hawk-options figwheel-server) (source-paths-that-affect-build build-config) - (partial execute-build this))))) + (partial execute-build this) + (:wait-time-ms figwheel-server))))) this)) (stop [this] (when (:file-watcher this) diff --git a/sidecar/src/figwheel_sidecar/components/file_system_watcher.clj b/sidecar/src/figwheel_sidecar/components/file_system_watcher.clj index 99ea4bda..6e87401a 100644 --- a/sidecar/src/figwheel_sidecar/components/file_system_watcher.clj +++ b/sidecar/src/figwheel_sidecar/components/file_system_watcher.clj @@ -36,7 +36,8 @@ (fn [] (utils/bind-logging log-writer - (notification-handler this files)))))))) + (notification-handler this files))))) + (:wait-time-ms figwheel-server-options)))) (do (println "Figwheel: No watch paths configured for" watcher-name) this))) diff --git a/sidecar/src/figwheel_sidecar/schemas/config.clj b/sidecar/src/figwheel_sidecar/schemas/config.clj index ec6dc9dd..cc69ae76 100644 --- a/sidecar/src/figwheel_sidecar/schemas/config.clj +++ b/sidecar/src/figwheel_sidecar/schemas/config.clj @@ -70,7 +70,9 @@ ::builds ::reload-clj-files ::hawk-options - ::cljs-build-fn]) + ::cljs-build-fn + ::wait-time-ms]) + "A Map of options that determine the behavior of the Figwheel system. :figwheel { @@ -310,6 +312,13 @@ be useful for certain docker environments. :hawk-options {:watcher :polling}" ) +(def-key ::wait-time-ms integer? + + "The number of milliseconds to wait before issuing reloads. Set this higher +to wait longer for changes. Default: 50 + + :wait-time-ms 50") + (def-key ::reload-clj-files (s/or :bool boolean? diff --git a/sidecar/src/figwheel_sidecar/watching.clj b/sidecar/src/figwheel_sidecar/watching.clj index 04b95e7c..cfdc17b5 100644 --- a/sidecar/src/figwheel_sidecar/watching.clj +++ b/sidecar/src/figwheel_sidecar/watching.clj @@ -23,8 +23,8 @@ ;;; we are going to have to throttle this ;; so that we can catch more than one file at a time -(defn take-until-timeout [in] - (let [time-out (timeout 50)] +(defn take-until-timeout [in t] + (let [time-out (timeout t)] (go-loop [collect []] (when-let [[v ch] (alts! [in time-out])] (if (= ch time-out) @@ -38,53 +38,57 @@ (merge {:sensitivity :high} hawk-options) hawk-options))) -(defn watch! [hawk-options source-paths callback] - (let [hawk-options (default-hawk-options hawk-options) - throttle-chan (chan) +(defn watch! + ([hawk-options source-paths callback wait-time-ms] + (let [hawk-options (default-hawk-options hawk-options) + wait-time-ms (or wait-time-ms 50) + throttle-chan (chan) - {:keys [files dirs]} (files-and-dirs source-paths) - individual-file-map (single-files files) - canonical-source-dirs (set (map #(.getCanonicalPath %) dirs)) + {:keys [files dirs]} (files-and-dirs source-paths) + individual-file-map (single-files files) + canonical-source-dirs (set (map #(.getCanonicalPath %) dirs)) - source-paths (distinct - (concat (map str dirs) - (map #(.getParent %) files))) + source-paths (distinct + (concat (map str dirs) + (map #(.getParent %) files))) - valid-file? (fn [file] - (and file - (.isFile file) - (not (.isHidden file)) - (let [file-path (.getCanonicalPath file) - n (.getName file)] - (and - (not= \. (first n)) - (not= \# (first n)) - (or - ;; just skip this if - ;; there are no individual-files - (empty? individual-file-map) - ;; if file is on a path that is already being watched we are cool - (some #(is-subdirectory % file-path) canonical-source-dirs) - ;; if not we need to see if its an individually watched file - (when-let [acceptable-paths - (get individual-file-map - (.getCanonicalPath (.getParentFile file)))] - (some #(= (.getCanonicalPath %) file-path) acceptable-paths))))))) - watcher (hawk/watch! hawk-options - [{:paths source-paths - :filter hawk/file? - :handler (fn [ctx e] - (put! throttle-chan e))}])] + valid-file? (fn [file] + (and file + (.isFile file) + (not (.isHidden file)) + (let [file-path (.getCanonicalPath file) + n (.getName file)] + (and + (not= \. (first n)) + (not= \# (first n)) + (or + ;; just skip this if + ;; there are no individual-files + (empty? individual-file-map) + ;; if file is on a path that is already being watched we are cool + (some #(is-subdirectory % file-path) canonical-source-dirs) + ;; if not we need to see if its an individually watched file + (when-let [acceptable-paths + (get individual-file-map + (.getCanonicalPath (.getParentFile file)))] + (some #(= (.getCanonicalPath %) file-path) acceptable-paths))))))) + watcher (hawk/watch! hawk-options + [{:paths source-paths + :filter hawk/file? + :handler (fn [ctx e] + (put! throttle-chan e))}])] - (go-loop [] - (when-let [v (