log_and_state.cljs 2.5 KB

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