Skip to content

Commit 7a3286f

Browse files
author
dnolen
committed
CLJS-1079: add way to execute arbitrary fn upon watch build completion
`cljs.closure/watch` - Move try/catch logic into local `buildf`, switch to catch `Throwable`. Support `:watch-fn` option.
1 parent 958580b commit 7a3286f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/clj/cljs/closure.clj

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,8 @@ should contain the source for the given namespace name."
14501450
(defn watch
14511451
"Given a source directory, produce runnable JavaScript. Watch the source
14521452
directory for changes rebuliding when necessary. Takes the same arguments as
1453-
cljs.closure/build."
1453+
cljs.closure/build in addition to :watch-fn, a function of no arguments to
1454+
run after a successful build."
14541455
([source opts]
14551456
(watch source opts
14561457
(if-not (nil? env/*compiler*)
@@ -1461,11 +1462,16 @@ should contain the source for the given namespace name."
14611462
fs (.getFileSystem path)
14621463
service (.newWatchService fs)]
14631464
(letfn [(buildf []
1464-
(let [start (System/nanoTime)]
1465-
(build source opts compiler-env)
1466-
(println "... done. Elapsed"
1467-
(/ (unchecked-subtract (System/nanoTime) start) 1e9) "seconds")
1468-
(flush)))
1465+
(try
1466+
(let [start (System/nanoTime)]
1467+
(build source opts compiler-env)
1468+
(println "... done. Elapsed"
1469+
(/ (unchecked-subtract (System/nanoTime) start) 1e9) "seconds")
1470+
(flush))
1471+
(when-let [f (:watch-fn opts)]
1472+
(f))
1473+
(catch Throwable e
1474+
(.printStackTrace e))))
14691475
(watch-all [^Path root]
14701476
(Files/walkFileTree root
14711477
(reify
@@ -1501,10 +1507,7 @@ should contain the source for the given namespace name."
15011507
(seq (.pollEvents key)))
15021508
(println "Change detected, recompiling...")
15031509
(flush)
1504-
(try
1505-
(buildf)
1506-
(catch Exception e
1507-
(.printStackTrace e))))
1510+
(buildf))
15081511
(recur key))))))))
15091512

15101513
(comment
@@ -1514,7 +1517,10 @@ should contain the source for the given namespace name."
15141517
:output-dir "samples/hello/out"
15151518
:cache-analysis true
15161519
:source-map true
1517-
:verbose true})
1520+
:verbose true
1521+
:watch-fn
1522+
(fn []
1523+
(println "Success!"))})
15181524
)
15191525

15201526
;; =============================================================================

0 commit comments

Comments
 (0)