Use Hawk in cryogen-core.watcher

This is a follow-up to PR #46. It replaces the 300ms loop with a file
watcher. Hawk is a wrapper around java.nio.file.WatchService coming with
proper OS X support.

Now rebuilds should be instantaneous.
This commit is contained in:
Jan Stępień 2016-03-13 16:43:14 +01:00
parent ef1422c42c
commit 0d1149a9c2
2 changed files with 12 additions and 10 deletions

View file

@ -13,4 +13,5 @@
[hiccup "1.0.5"] [hiccup "1.0.5"]
[selmer "1.0.0"] [selmer "1.0.0"]
[pandect "0.5.4"] [pandect "0.5.4"]
[hawk "0.2.10"]
[clj-tagsoup "0.3.0" :exclusions [org.clojure/clojure]]]) [clj-tagsoup "0.3.0" :exclusions [org.clojure/clojure]]])

View file

@ -2,6 +2,7 @@
(:require [clojure.java.io :refer [file]] (:require [clojure.java.io :refer [file]]
[cryogen-core.io :refer [ignore]] [cryogen-core.io :refer [ignore]]
[pandect.algo.md5 :refer [md5]] [pandect.algo.md5 :refer [md5]]
[hawk.core :as hawk]
[clojure.set :as set])) [clojure.set :as set]))
(defn get-assets [path ignored-files] (defn get-assets [path ignored-files]
@ -21,15 +22,15 @@
(when-some [changes (set/difference new-sum-set old-sum-set)] (when-some [changes (set/difference new-sum-set old-sum-set)]
(vals (select-keys new-sums changes))))) (vals (select-keys new-sums changes)))))
(defn watch-assets [root ignored-files action] (defn watch-assets [sums root ignored-files action]
(loop [sums (checksums root ignored-files)] (let [new-sums (checksums root ignored-files)]
(Thread/sleep 300) (when (find-changes @sums new-sums)
(let [new-sums (checksums root ignored-files)] (action)
(when (find-changes sums new-sums) (reset! sums new-sums))))
(action))
(recur new-sums))))
(defn start-watcher! [root ignored-files action] (defn start-watcher! [root ignored-files action]
(doto (Thread. #(watch-assets root ignored-files action)) (let [sums (atom (checksums root ignored-files))
(.setDaemon true) handler (fn [ctx e]
(.start))) (watch-assets sums root ignored-files action))]
(hawk/watch! [{:paths [root]
:handler handler}])))