-
Notifications
You must be signed in to change notification settings - Fork 6
/
watch.clj
31 lines (27 loc) · 1.11 KB
/
watch.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns dynadoc.watch
(:require [clojure.java.io :as io]
[clojure.string :as str]
[org.httpkit.server :refer [with-channel on-receive on-close]]
[hawk.core :as hawk]
[dynadoc.static :as static]
[dynadoc.utils :as u]))
(defonce *channel->uri (atom {}))
(defonce *cljs-info (atom nil))
(defn init-watcher! []
(reset! *cljs-info (static/get-cljs-nses-and-vars))
(hawk/watch! [{:paths [(.getCanonicalPath (io/file "."))]
:handler (fn [ctx {:keys [kind file]}]
(when (#{:create :modify} kind)
(when (str/ends-with? (.getName file) ".cljs")
(try
(swap! *cljs-info #(static/read-cljs-file % file))
(catch Exception _))))
ctx)}]))
(defn watch-request [request]
(with-channel request channel
(on-close channel
(fn [status]
(swap! *channel->uri dissoc channel)))
(on-receive channel
(fn [uri]
(swap! *channel->uri assoc channel uri)))))