rcmerci 2 лет назад
Родитель
Сommit
5c605192be

+ 20 - 16
src/main/frontend/db/rtc/full_upload_download_graph.cljs

@@ -1,4 +1,5 @@
 (ns frontend.db.rtc.full-upload-download-graph
+  (:require-macros [frontend.db.rtc.macro :refer [with-sub-data-from-ws get-req-id get-result-ch]])
   (:require [frontend.db.conn :as conn]
             [datascript.core :as d]
             [frontend.db.rtc.ws :refer [send]]
@@ -24,19 +25,22 @@
 (defn- <upload-graph
   [state]
   (go
-    (let [req-id (str (random-uuid))
-          ch (chan 1)
-          data-from-ws-pub (:data-from-ws-pub state)]
-      (async/sub data-from-ws-pub req-id ch)
-      (send (:ws state) {:req-id req-id :action "presign-put-temp-s3-obj" :graph-uuid "not-yet"})
-      (let [all-blocks (export-as-blocks (state/get-current-repo))
-            all-blocks-str (transit/write (transit/writer :json) all-blocks)
-            {:keys [url key]} (<! ch)]
-        (async/unsub data-from-ws-pub req-id ch)
-        (<! (http/put url {:body all-blocks-str}))
-        (let [req-id2 (str (random-uuid))
-              ch2 (chan 1)]
-          (async/sub data-from-ws-pub req-id2 ch2)
-          (send (:ws state) {:req-id req-id2 :action "full-upload-graph" :graph-uuid "not-yet" :s3-key key})
-          (println (<! ch2))
-          (async/unsub data-from-ws-pub req-id2 ch2))))))
+    (let [{:keys [url key all-blocks-str]}
+          (with-sub-data-from-ws state
+            (send (:ws state) {:req-id (get-req-id) :action "presign-put-temp-s3-obj" :graph-uuid "not-yet"})
+            (let [all-blocks (export-as-blocks (state/get-current-repo))
+                  all-blocks-str (transit/write (transit/writer :json) all-blocks)]
+              (merge (<! (get-result-ch)) {:all-blocks-str all-blocks-str})))]
+      (<! (http/put url {:body all-blocks-str}))
+      (with-sub-data-from-ws state
+        (send (:ws state) {:req-id (get-req-id) :action "full-upload-graph" :graph-uuid "not-yet" :s3-key key})
+        (println (<! (get-result-ch)))))))
+
+
+(defn- <download-graph
+  [state graph-uuid]
+  (go
+    (let [r (with-sub-data-from-ws state
+              (send (:ws state) {:req-id (get-req-id) :action "full-download-graph" :graph-uuid graph-uuid})
+              (<! (get-result-ch)))]
+      (prn r))))

+ 20 - 0
src/main/frontend/db/rtc/macro.clj

@@ -0,0 +1,20 @@
+(ns frontend.db.rtc.macro)
+
+
+(defmacro with-sub-data-from-ws
+  "- sub :data-from-ws-pub
+  - run body, use `get-req-id` to get req-id, and `get-result-ch` to get result-ch
+  - unsub :data-from-ws-pub"
+  [state & body]
+  `(let [~'req-id (str (random-uuid))
+         data-from-ws-pub# (:data-from-ws-pub ~state)
+         ~'result-ch (cljs.core.async/chan 1)]
+     (cljs.core.async/sub data-from-ws-pub# ~'req-id ~'result-ch)
+     (try
+       ~@body
+       (finally
+         (cljs.core.async/unsub data-from-ws-pub# ~'req-id ~'result-ch)))))
+
+
+(defmacro get-req-id [] 'req-id)
+(defmacro get-result-ch [] 'result-ch)