watcher_handler.cljs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (ns frontend.fs.watcher-handler
  2. (:require [clojure.core.async :as async]
  3. [lambdaisland.glogi :as log]
  4. [frontend.handler.file :as file-handler]
  5. [frontend.handler.page :as page-handler]
  6. [frontend.handler.notification :as notification]
  7. [frontend.handler.route :as route-handler]
  8. [cljs-time.coerce :as tc]
  9. [frontend.config :as config]
  10. [frontend.db :as db]
  11. [frontend.state :as state]
  12. [clojure.string :as string]
  13. [frontend.encrypt :as encrypt]))
  14. (defn handle-changed!
  15. [type {:keys [dir path content stat] :as payload}]
  16. (when dir
  17. (let [repo (config/get-local-repo dir)
  18. {:keys [mtime]} stat]
  19. (when (and content (not (encrypt/content-encrypted? content)))
  20. (cond
  21. (= "add" type)
  22. (when-not (db/file-exists? repo path)
  23. (let [_ (file-handler/alter-file repo path content {:re-render-root? true
  24. :from-disk? true})]
  25. (db/set-file-last-modified-at! repo path mtime)))
  26. (and (= "change" type)
  27. (nil? (db/get-file path)))
  28. (js/console.warn "Can't get file in the db: " path)
  29. (and (= "change" type)
  30. (not= content (db/get-file path))
  31. (when-let [last-modified-at (db/get-file-last-modified-at repo path)]
  32. (> mtime last-modified-at)))
  33. (let [_ (file-handler/alter-file repo path content {:re-render-root? true
  34. :from-disk? true})]
  35. (db/set-file-last-modified-at! repo path mtime))
  36. (contains? #{"add" "change" "unlink"} type)
  37. nil
  38. :else
  39. (log/error :fs/watcher-no-handler {:type type
  40. :payload payload}))))))