Skip to content

Commit e8a4ad9

Browse files
author
dnolen
committed
CLJS-1204: cljs.closure/watch cannot take cljs.closure/Compilable
Add cljs.closure/Inputs protocol, extend to strings & files. Change cls.closure/watch to call through the protocol on source argument. Watch all input sources. cljs.build.api/inputs now returns Inputs instance. Add watch to the cljs.build.api ns.
1 parent 5353489 commit e8a4ad9

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/clj/cljs/build/api.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
be passed to build or watch."
102102
[& xs]
103103
(reify
104+
closure/Inputs
105+
(-paths [_]
106+
(map io/file xs))
104107
closure/Compilable
105108
(-compile [_ opts]
106109
(letfn [(compile-input [x]
@@ -117,6 +120,15 @@
117120
([source opts compiler-env]
118121
(closure/build source opts compiler-env)))
119122

123+
(defn watch
124+
"Given a source which can be compiled, watch it for changes to produce."
125+
([source opts]
126+
(closure/watch source opts))
127+
([source opts compiler-env]
128+
(closure/watch source opts compiler-env))
129+
([source opts compiler-env stop]
130+
(closure/watch source opts compiler-env stop)))
131+
120132
(comment
121133

122134
(def test-cenv (atom {}))

src/clj/cljs/closure.clj

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@
313313
;; Compile
314314
;; =======
315315

316+
(defprotocol Inputs
317+
(-paths [this] "Returns the file paths to the source inputs"))
318+
319+
(extend-protocol Inputs
320+
String
321+
(-paths [this] [(io/file this)])
322+
File
323+
(-paths [this] [this]))
324+
316325
(defprotocol Compilable
317326
(-compile [this opts] "Returns one or more IJavaScripts."))
318327

@@ -1512,12 +1521,13 @@
15121521
([source opts compiler-env]
15131522
(watch source opts compiler-env nil))
15141523
([source opts compiler-env quit]
1515-
(let [opts (cond-> opts
1516-
(= (:verbose opts :not-found) :not-found)
1517-
(assoc :verbose true))
1518-
path (Paths/get (.toURI (io/file source)))
1519-
fs (.getFileSystem path)
1520-
service (.newWatchService fs)]
1524+
(let [opts (cond-> opts
1525+
(= (:verbose opts :not-found) :not-found)
1526+
(assoc :verbose true))
1527+
paths (map #(Paths/get (.toURI %)) (-paths source))
1528+
path (first paths)
1529+
fs (.getFileSystem path)
1530+
srvc (.newWatchService fs)]
15211531
(letfn [(buildf []
15221532
(try
15231533
(let [start (System/nanoTime)]
@@ -1537,7 +1547,7 @@
15371547
(preVisitDirectory [_ dir _]
15381548
(let [^Path dir dir]
15391549
(. dir
1540-
(register service
1550+
(register srvc
15411551
(into-array [StandardWatchEventKinds/ENTRY_CREATE
15421552
StandardWatchEventKinds/ENTRY_DELETE
15431553
StandardWatchEventKinds/ENTRY_MODIFY])
@@ -1552,12 +1562,13 @@
15521562
(println "Building ...")
15531563
(flush)
15541564
(buildf)
1555-
(println "Watching path:" source)
1556-
(watch-all path)
1565+
(println "Watching paths:" (apply str (interpose ", " paths)))
1566+
(doseq [path paths]
1567+
(watch-all path))
15571568
(loop [key nil]
15581569
(when (and (or (nil? quit) (not @quit))
15591570
(or (nil? key) (. ^WatchKey key reset)))
1560-
(let [key (. service (poll 300 TimeUnit/MILLISECONDS))]
1571+
(let [key (. srvc (poll 300 TimeUnit/MILLISECONDS))]
15611572
(when (and key
15621573
(some
15631574
(fn [^WatchEvent e]

0 commit comments

Comments
 (0)