auto-reload

1.0.3


Reload code in a background thread on file modification.

dependencies

ns-tracker
0.1.1
org.clojure/clojure
1.3.0

dev dependencies

lein-marginalia
0.7.0-SNAPSHOT
org.clojure/clojure
1.3.0
org.clojure/tools.logging
0.2.3
vimclojure/server
2.3.0-SNAPSHOT



(this space intentionally left almost blank)
 

Automatically reload code in a background thread on file modification.

(ns auto-reload.core
  {:author "Naitik Shah"}
  (:use
    [ns-tracker.core :only [ns-tracker]])
  (:import
    [java.nio.file FileSystems StandardWatchEventKinds]))
(defn- auto-reload* [dirs]
  (let [modified-namespaces (ns-tracker dirs)
        fs (FileSystems/getDefault)
        watcher (.newWatchService fs)
        events (into-array [StandardWatchEventKinds/ENTRY_MODIFY])
        strs (into-array String [])]
    (doseq [dir dirs]
      (.register (.getPath fs dir strs) watcher events))
    (while true
      (let [key (.take watcher)
            events (.pollEvents key)]
        (if (not-every? #(= (.kind %) StandardWatchEventKinds/OVERFLOW) events)
          (doseq [ns-sym (modified-namespaces)]
            (require ns-sym :reload)))
        (.reset key)))))

Enables auto-reload for the namespaces loaded from the given seq of directories in a background thread. Returns immediately.

(defn auto-reload
  [dirs]
  (.start (Thread. (partial auto-reload* dirs))))
 

repl helpers

(ns auto-reload.repl
  {:author "Naitik Shah"}
  (:require
    [auto-reload.core]
    [clojure.tools.logging]))
(auto-reload.core/auto-reload ["src"])