log_and_state.cljs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. (ns frontend.worker.rtc.log-and-state
  2. "Fns to generate rtc related logs"
  3. (:require [frontend.common.missionary :as c.m]
  4. [frontend.worker.util :as worker-util]
  5. [logseq.common.defkeywords :refer [defkeywords]]
  6. [malli.core :as ma]
  7. [missionary.core :as m]))
  8. (def ^:private *rtc-log (atom nil))
  9. (def rtc-log-flow
  10. "used by rtc-e2e-test"
  11. (m/watch *rtc-log))
  12. (defkeywords
  13. :rtc.log/upload {:doc "rtc log type for upload-graph."}
  14. :rtc.log/download {:doc "rtc log type for upload-graph."})
  15. (def ^:private rtc-log-type-schema
  16. [:enum
  17. :rtc.log/upload
  18. :rtc.log/download
  19. :rtc.log/cancelled
  20. :rtc.log/apply-remote-update
  21. :rtc.log/push-local-update
  22. :rtc.asset.log/cancelled
  23. :rtc.asset.log/upload-assets
  24. :rtc.asset.log/download-assets
  25. :rtc.asset.log/remove-assets
  26. :rtc.asset.log/initial-download-missing-assets-count])
  27. (def ^:private rtc-log-type-validator (ma/validator rtc-log-type-schema))
  28. (defn rtc-log
  29. [type m]
  30. {:pre [(map? m) (rtc-log-type-validator type)]}
  31. (reset! *rtc-log (assoc m :type type :created-at (js/Date.)))
  32. nil)
  33. ;;; some other states
  34. (def ^:private graph-uuid->t-schema
  35. [:map-of :uuid :int])
  36. (def ^:private graph-uuid->t-validator (let [validator (ma/validator graph-uuid->t-schema)]
  37. (fn [v]
  38. (if (validator v)
  39. true
  40. (do (prn :debug-graph-uuid->t-validator v)
  41. false)))))
  42. (def *graph-uuid->local-t (atom {} :validator graph-uuid->t-validator))
  43. (def *graph-uuid->remote-t (atom {} :validator graph-uuid->t-validator))
  44. (defn- ensure-uuid
  45. [v]
  46. (cond
  47. (uuid? v) v
  48. (string? v) (uuid v)
  49. :else (throw (ex-info "illegal value" {:data v}))))
  50. (defn create-local-t-flow
  51. [graph-uuid]
  52. (->> (m/watch *graph-uuid->local-t)
  53. (m/eduction (keep (fn [m] (get m (ensure-uuid graph-uuid)))))
  54. c.m/continue-flow))
  55. (defn create-remote-t-flow
  56. [graph-uuid]
  57. {:pre [(some? graph-uuid)]}
  58. (->> (m/watch *graph-uuid->remote-t)
  59. (m/eduction (keep (fn [m] (get m (ensure-uuid graph-uuid)))))
  60. c.m/continue-flow))
  61. (defn update-local-t
  62. [graph-uuid local-t]
  63. (swap! *graph-uuid->local-t assoc (ensure-uuid graph-uuid) local-t))
  64. (defn update-remote-t
  65. [graph-uuid remote-t]
  66. (swap! *graph-uuid->remote-t assoc (ensure-uuid graph-uuid) remote-t))
  67. ;;; subscribe-logs, push to frontend
  68. (defn- subscribe-logs
  69. []
  70. (remove-watch *rtc-log :subscribe-logs)
  71. (add-watch *rtc-log :subscribe-logs
  72. (fn [_ _ _ n] (when n (worker-util/post-message :rtc-log n)))))
  73. (subscribe-logs)