debug.cljs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. (ns frontend.debug
  2. (:require [cljs.pprint :as pprint]
  3. [frontend.state :as state]
  4. [frontend.util :as util]
  5. [frontend.handler.notification :as notification]))
  6. (defn pprint
  7. [& xs]
  8. (when (state/developer-mode?)
  9. (doseq [x xs]
  10. (pprint/pprint x))))
  11. (defonce ack-wait-timeouts (atom {}))
  12. (defonce default-write-ack-timeout 10000)
  13. ;; For debugging file changes are not saved on disk.
  14. (defn wait-for-write-ack!
  15. [page-title file-path]
  16. (when file-path
  17. (let [requested-at (util/time-ms)]
  18. (state/set-state! [:debug/write-acks file-path :last-requested-at] requested-at)
  19. (when-let [timeout (get @ack-wait-timeouts file-path)]
  20. (js/clearTimeout timeout))
  21. (let [timeout (js/setTimeout (fn []
  22. (let [last-ack-at (get-in @state/state [:debug/write-acks file-path :last-ack-at])]
  23. (when-not (and last-ack-at
  24. (< requested-at last-ack-at (+ requested-at default-write-ack-timeout)))
  25. (let [step (get-in @state/state [:debug/write-acks file-path :step])]
  26. (state/pub-event! [:instrument {:type :debug/write-failed
  27. :payload {:step step}}])
  28. ;; (notification/show!
  29. ;; (str "Logseq failed to save the page "
  30. ;; page-title
  31. ;; " to the file: "
  32. ;; file-path
  33. ;; ". Stop editing this page anymore, and copy all the blocks of this page to another editor to avoid any data-loss.\n"
  34. ;; "Last step: "
  35. ;; step)
  36. ;; :error)
  37. ))))
  38. default-write-ack-timeout)]
  39. (swap! ack-wait-timeouts assoc file-path timeout)))))
  40. (defn ack-file-write!
  41. [file-path]
  42. (let [ack-at (util/time-ms)]
  43. (state/set-state! [:debug/write-acks file-path :last-ack-at] ack-at)))
  44. (defn set-ack-step!
  45. [file-path step]
  46. (state/set-state! [:debug/write-acks file-path :step] step))