|
|
@@ -4,7 +4,8 @@
|
|
|
["chokidar" :as watcher]
|
|
|
[electron.utils :as utils]
|
|
|
["electron" :refer [app]]
|
|
|
- [electron.window :as window]))
|
|
|
+ [electron.window :as window]
|
|
|
+ ["path" :as path]))
|
|
|
|
|
|
;; TODO: explore different solutions for different platforms
|
|
|
;; 1. https://github.com/Axosoft/nsfw
|
|
|
@@ -29,10 +30,13 @@
|
|
|
|
|
|
(defn- publish-file-event!
|
|
|
[dir path event]
|
|
|
- (let [content (when (and (not= event "unlink")
|
|
|
+ (let [dir-path? (= dir path)
|
|
|
+ content (when (and (not= event "unlink")
|
|
|
+ (not dir-path?)
|
|
|
(utils/should-read-content? path))
|
|
|
(utils/read-file path))
|
|
|
- stat (when (not= event "unlink")
|
|
|
+ stat (when (and (not= event "unlink")
|
|
|
+ (not dir-path?))
|
|
|
(fs/statSync path))]
|
|
|
(send-file-watcher! dir event {:dir (utils/fix-win-path! dir)
|
|
|
:path (utils/fix-win-path! path)
|
|
|
@@ -44,38 +48,53 @@
|
|
|
[_win dir]
|
|
|
(when (and (fs/existsSync dir)
|
|
|
(not (get @*file-watcher dir)))
|
|
|
- (let [watcher (.watch watcher dir
|
|
|
- (clj->js
|
|
|
- {:ignored (fn [path]
|
|
|
- (utils/ignored-path? dir path))
|
|
|
- :ignoreInitial false
|
|
|
- :ignorePermissionErrors true
|
|
|
- :interval polling-interval
|
|
|
- :binaryInterval polling-interval
|
|
|
- :persistent true
|
|
|
- :disableGlobbing true
|
|
|
- :usePolling false
|
|
|
- :awaitWriteFinish true}))
|
|
|
- watcher-del-f #(.close watcher)]
|
|
|
- (swap! *file-watcher assoc dir [watcher watcher-del-f])
|
|
|
+ (let [watcher-opts (clj->js
|
|
|
+ {:ignored (fn [path]
|
|
|
+ (utils/ignored-path? dir path))
|
|
|
+ :ignoreInitial false
|
|
|
+ :ignorePermissionErrors true
|
|
|
+ :interval polling-interval
|
|
|
+ :binaryInterval polling-interval
|
|
|
+ :persistent true
|
|
|
+ :disableGlobbing true
|
|
|
+ :usePolling false
|
|
|
+ :awaitWriteFinish true})
|
|
|
+ dir-watcher (.watch watcher dir watcher-opts)
|
|
|
+ watcher-del-f #(.close dir-watcher)
|
|
|
+ parent-dir (path/join dir "..")
|
|
|
+ parent-watcher (.watch watcher parent-dir watcher-opts)
|
|
|
+ parent-watcher-del-f #(.close parent-watcher)]
|
|
|
+ (swap! *file-watcher assoc dir
|
|
|
+ [dir-watcher watcher-del-f]
|
|
|
+ [parent-watcher parent-watcher-del-f])
|
|
|
;; TODO: batch sender
|
|
|
- (.on watcher "add"
|
|
|
+ (.on parent-watcher "unlinkDir"
|
|
|
+ (fn [path]
|
|
|
+ (when (= dir path)
|
|
|
+ (publish-file-event! dir dir "unlinkDir"))))
|
|
|
+ (.on parent-watcher "addDir"
|
|
|
+ (fn [path]
|
|
|
+ (when (= dir path)
|
|
|
+ (publish-file-event! dir dir "addDir"))))
|
|
|
+ (.on dir-watcher "add"
|
|
|
(fn [path]
|
|
|
(publish-file-event! dir path "add")))
|
|
|
- (.on watcher "change"
|
|
|
+ (.on dir-watcher "change"
|
|
|
(fn [path]
|
|
|
(publish-file-event! dir path "change")))
|
|
|
- (.on watcher "unlink"
|
|
|
+ (.on dir-watcher "unlink"
|
|
|
(fn [path]
|
|
|
(publish-file-event! dir path "unlink")))
|
|
|
- (.on watcher "error"
|
|
|
+ (.on dir-watcher "error"
|
|
|
(fn [path]
|
|
|
(println "Watch error happened: "
|
|
|
{:path path})))
|
|
|
|
|
|
;; electron app extends `EventEmitter`
|
|
|
;; TODO check: duplicated with the logic in "window-all-closed" ?
|
|
|
- (.on app "quit" watcher-del-f)
|
|
|
+ (.on app "quit" (fn []
|
|
|
+ (watcher-del-f)
|
|
|
+ (parent-watcher-del-f)))
|
|
|
|
|
|
true)))
|
|
|
|