|
|
@@ -6,7 +6,6 @@
|
|
|
[cljs.core.async :as async :refer [<! >! chan go go-loop]]
|
|
|
[frontend.worker.rtc.const :as rtc-const]
|
|
|
[frontend.worker.rtc.op-mem-layer :as op-mem-layer]
|
|
|
- [frontend.handler.user :as user]
|
|
|
[frontend.worker.rtc.ws :as ws]
|
|
|
[frontend.worker.async-util :include-macros true :refer [<?]]
|
|
|
[datascript.core :as d]
|
|
|
@@ -17,10 +16,15 @@
|
|
|
[:*graph-uuid :any]
|
|
|
[:*repo :any]
|
|
|
[:*db-conn :any]
|
|
|
+ [:*token :any]
|
|
|
+ [:*date-formatter :any]
|
|
|
+ [:*ws :any]
|
|
|
[:*assets-update-state :any]
|
|
|
+ [:data-from-ws-chan :any]
|
|
|
[:data-from-ws-pub :any]
|
|
|
[:*auto-push-assets-update-ops? :any]
|
|
|
- [:toggle-auto-push-assets-update-ops-chan :any]])
|
|
|
+ [:toggle-auto-push-assets-update-ops-chan :any]
|
|
|
+ [:*stop-asset-sync-loop-chan :any]])
|
|
|
|
|
|
(def state-validator
|
|
|
(let [validator (m/validator state-schema)]
|
|
|
@@ -29,6 +33,24 @@
|
|
|
true
|
|
|
(prn (mu/explain-data state-schema data))))))
|
|
|
|
|
|
+(defonce *asset-sync-state (atom nil))
|
|
|
+
|
|
|
+(defn init-state-from-rtc-state
|
|
|
+ [rtc-state]
|
|
|
+ {:post [(m/validate state-schema %)]}
|
|
|
+ {:*graph-uuid (atom nil)
|
|
|
+ :*repo (atom nil)
|
|
|
+ :*db-conn (atom nil)
|
|
|
+ :*token (:*token rtc-state)
|
|
|
+ :*date-formatter (atom nil)
|
|
|
+ :*ws (:*ws rtc-state)
|
|
|
+ :*assets-update-state (atom nil)
|
|
|
+ :data-from-ws-chan (:data-from-ws-chan rtc-state)
|
|
|
+ :data-from-ws-pub (:data-from-ws-pub rtc-state)
|
|
|
+ :*auto-push-assets-update-ops? (atom true :validator boolean?)
|
|
|
+ :toggle-auto-push-assets-update-ops-chan (chan (async/sliding-buffer 1))
|
|
|
+ :*stop-asset-sync-loop-chan (atom nil)})
|
|
|
+
|
|
|
|
|
|
(defn- <push-data-from-ws-handler
|
|
|
[repo push-data-from-ws]
|
|
|
@@ -41,33 +63,38 @@
|
|
|
[state graph-uuid repo conn]
|
|
|
(go-loop []
|
|
|
(when-let [{min-epoch-asset-ops :ops asset-uuid :asset-uuid} (op-mem-layer/get-min-epoch-asset-ops repo)]
|
|
|
- (try
|
|
|
- (doseq [[tp _op] min-epoch-asset-ops]
|
|
|
- (case tp
|
|
|
- :update-asset
|
|
|
- (let [asset-entity (d/pull @conn '[*] [:asset/uuid asset-uuid])
|
|
|
- r (<? (ws/<send&receive state {:action "update-assets" :graph-uuid graph-uuid
|
|
|
- :create [{:asset-uuid asset-uuid
|
|
|
- :asset-name (or (some-> asset-entity :asset/meta :name)
|
|
|
- "default-name")}]}))]
|
|
|
- (when (:ex-data r)
|
|
|
- (throw (ex-info (:ex-message r) (:ex-data r)))))
|
|
|
+ (let [recur?
|
|
|
+ (try
|
|
|
+ (doseq [[tp _op] min-epoch-asset-ops]
|
|
|
+ (case tp
|
|
|
+ :update-asset
|
|
|
+ (let [asset-entity (d/pull @conn '[*] [:asset/uuid asset-uuid])
|
|
|
+ r (<? (ws/<send&receive state {:action "update-assets" :graph-uuid graph-uuid
|
|
|
+ :create [{:asset-uuid asset-uuid
|
|
|
+ :asset-name (or (some-> asset-entity :asset/meta :name)
|
|
|
+ "default-name")}]}))]
|
|
|
+ (when (:ex-data r)
|
|
|
+ (throw (ex-info (:ex-message r) (:ex-data r)))))
|
|
|
|
|
|
- :remove-asset
|
|
|
- (let [r (<? (ws/<send&receive state {:action "update-assets" :graph-uuid graph-uuid
|
|
|
- :delete [asset-uuid]}))]
|
|
|
- (when (:ex-data r)
|
|
|
- (throw (ex-info (:ex-message r) (:ex-data r)))))))
|
|
|
- (op-mem-layer/remove-asset-ops! repo asset-uuid)
|
|
|
- (recur)
|
|
|
- (catch :default e
|
|
|
- (prn ::unknown-ex e))))))
|
|
|
+ :remove-asset
|
|
|
+ (let [r (<? (ws/<send&receive state {:action "update-assets" :graph-uuid graph-uuid
|
|
|
+ :delete [asset-uuid]}))]
|
|
|
+ (when (:ex-data r)
|
|
|
+ (throw (ex-info (:ex-message r) (:ex-data r)))))))
|
|
|
+ (op-mem-layer/remove-asset-ops! repo asset-uuid)
|
|
|
+ :recur
|
|
|
+ (catch :default e
|
|
|
+ (prn ::unknown-ex e)
|
|
|
+ nil))]
|
|
|
+ (when (= :recur recur?)
|
|
|
+ (recur))))))
|
|
|
|
|
|
|
|
|
(defn- <client-op-update-handler
|
|
|
[state]
|
|
|
{:pre [(some? @(:*graph-uuid state))
|
|
|
- (some? @(:*repo state))]}
|
|
|
+ (some? @(:*repo state))
|
|
|
+ (some? @(:*db-conn state))]}
|
|
|
(go
|
|
|
(let [repo @(:*repo state)
|
|
|
conn @(:*db-conn state)
|
|
|
@@ -82,19 +109,21 @@
|
|
|
(go
|
|
|
(<! (async/timeout 2000))
|
|
|
;; TODO: get-unpushed-assets-update-count
|
|
|
- (pos? (op-mem-layer/get-unpushed-block-update-count repo)))))
|
|
|
+ (pos? (op-mem-layer/get-unpushed-asset-update-count repo)))))
|
|
|
|
|
|
(defn <loop-for-assets-sync
|
|
|
- [state graph-uuid repo]
|
|
|
+ [state graph-uuid repo conn]
|
|
|
{:pre [(state-validator state)]}
|
|
|
(go
|
|
|
(reset! (:*repo state) repo)
|
|
|
(reset! (:*graph-uuid state) graph-uuid)
|
|
|
+ (reset! (:*db-conn state) conn)
|
|
|
(let [{:keys [data-from-ws-pub]} state
|
|
|
*auto-push-assets-update-ops? (:*auto-push-assets-update-ops? state)
|
|
|
toggle-auto-push-assets-update-ops-ch (:toggle-auto-push-assets-update-ops-chan state)
|
|
|
push-data-from-ws-ch (chan (async/sliding-buffer 100) (map rtc-const/data-from-ws-coercer))
|
|
|
stop-assets-sync-loop-chan (chan)]
|
|
|
+ (reset! (:*stop-asset-sync-loop-chan state) stop-assets-sync-loop-chan)
|
|
|
(async/sub data-from-ws-pub "push-assets-updates" push-data-from-ws-ch)
|
|
|
(<! (go-loop [push-assets-update-ops-ch
|
|
|
(make-push-assets-update-ops-timeout-ch repo (not @*auto-push-assets-update-ops?))]
|
|
|
@@ -117,8 +146,10 @@
|
|
|
(recur (make-push-assets-update-ops-timeout-ch repo (not @*auto-push-assets-update-ops?))))
|
|
|
|
|
|
client-assets-update
|
|
|
- (let [maybe-exp (<! (user/<wrap-ensure-id&access-token
|
|
|
- (<! (<client-op-update-handler state))))]
|
|
|
+ ;; TODO: <wrap-ensure-id&access-token, ensure token not expired
|
|
|
+ ;; because this ns is running in db-worker now, need to move(or copy) <wrap-ensure-id&access-token
|
|
|
+ ;; to db-worker again
|
|
|
+ (let [maybe-exp (<! (<client-op-update-handler state))]
|
|
|
(if (= :expired-token (:anom (ex-data maybe-exp)))
|
|
|
(prn ::<loop-for-assets-sync "quitting loop" maybe-exp)
|
|
|
(recur (make-push-assets-update-ops-timeout-ch repo (not @*auto-push-assets-update-ops?)))))
|
|
|
@@ -129,3 +160,28 @@
|
|
|
|
|
|
:else nil))))
|
|
|
(async/unsub data-from-ws-pub "push-assets-update" push-data-from-ws-ch))))
|
|
|
+
|
|
|
+(comment
|
|
|
+ (go-loop []
|
|
|
+ (when-let [{min-epoch-asset-ops :ops asset-uuid :asset-uuid} (op-mem-layer/get-min-epoch-asset-ops repo)]
|
|
|
+ (try
|
|
|
+ (doseq [[tp _op] min-epoch-asset-ops]
|
|
|
+ (case tp
|
|
|
+ :update-asset
|
|
|
+ (let [asset-entity (d/pull @conn '[*] [:asset/uuid asset-uuid])
|
|
|
+ r (<? (ws/<send&receive state {:action "update-assets" :graph-uuid graph-uuid
|
|
|
+ :create [{:asset-uuid asset-uuid
|
|
|
+ :asset-name (or (some-> asset-entity :asset/meta :name)
|
|
|
+ "default-name")}]}))]
|
|
|
+ (when (:ex-data r)
|
|
|
+ (throw (ex-info (:ex-message r) (:ex-data r)))))
|
|
|
+
|
|
|
+ :remove-asset
|
|
|
+ (let [r (<? (ws/<send&receive state {:action "update-assets" :graph-uuid graph-uuid
|
|
|
+ :delete [asset-uuid]}))]
|
|
|
+ (when (:ex-data r)
|
|
|
+ (throw (ex-info (:ex-message r) (:ex-data r)))))))
|
|
|
+ (op-mem-layer/remove-asset-ops! repo asset-uuid)
|
|
|
+ (recur)
|
|
|
+ (catch :default e
|
|
|
+ (prn ::unknown-ex e))))))
|