Browse Source

fix: get graphs

Tienson Qin 2 years ago
parent
commit
d593456c1c

+ 3 - 2
src/main/frontend/db/rtc/debug_ui.cljs

@@ -43,9 +43,10 @@
       (ui/button "graph-list"
                  :icon "refresh"
                  :on-click (fn [_]
-                             (let [token (state/get-auth-id-token)
+                             (let [repo (state/get-current-repo)
+                                   token (state/get-auth-id-token)
                                    ^object worker @db-browser/*worker]
-                               (p/let [result (.rtc-get-graphs worker token)
+                               (p/let [result (.rtc-get-graphs worker repo token)
                                        graph-list (bean/->clj result)]
                                  (swap! debug-state assoc :remote-graphs (map :graph-uuid graph-list))))))]
 

+ 4 - 4
src/main/frontend/db_worker.cljs

@@ -412,7 +412,7 @@
    (when-let [conn (state/get-datascript-conn repo)]
      (async/go
        (try
-         (let [state (<! (rtc-core/<init-state token))]
+         (let [state (<! (rtc-core/<init-state repo token))]
            (<! (rtc-updown/<upload-graph state repo conn))
            (rtc-db-listener/listen-db-to-generate-ops repo conn))
          (worker-util/post-message :notification
@@ -431,7 +431,7 @@
   (rtc-download-graph
    [this repo token graph-uuid]
    (async/go
-     (let [state (<! (rtc-core/<init-state token))]
+     (let [state (<! (rtc-core/<init-state repo token))]
        (try
          (<? (rtc-updown/<download-graph state repo graph-uuid))
          (worker-util/post-message :notification
@@ -453,8 +453,8 @@
    nil)
 
   (rtc-get-graphs
-   [_this token]
-   (rtc-core/<get-graphs token))
+   [_this repo token]
+   (rtc-core/<get-graphs repo token))
 
   (rtc-get-block-content-versions
    [_this block-id]

+ 1 - 1
src/main/frontend/handler/events.cljs

@@ -942,7 +942,7 @@
   (state/set-state! :error/multiple-tabs-access-opfs? true)
   (state/set-modal! multi-tabs-dialog {:container-overflow-visible? true}))
 
-(defmethod handle :rtc-sync-state [[_ state]]
+(defmethod handle :rtc/sync-state [[_ state]]
   (swap! rtc-debug-ui/debug-state (fn [old] (merge old state))))
 
 (defn run!

+ 3 - 2
src/main/frontend/handler/worker.cljs

@@ -8,8 +8,9 @@
 
 (defmulti handle identity)
 
-(defmethod handle :write-files [_ {:keys [repo files]}]
-  (file-handler/alter-files repo files {}))
+(defmethod handle :write-files [_ data]
+  (let [{:keys [repo files]} (edn/read-string data)]
+    (file-handler/alter-files repo files {})))
 
 (defmethod handle :notification [_ data]
   (apply notification/show! (edn/read-string data)))

+ 1 - 1
src/main/frontend/worker/file/core.cljs

@@ -154,7 +154,7 @@
         (when-not (and (string/blank? new-content) (not blocks-just-deleted?))
           (let [files [[file-path new-content]]]
             (when (seq files)
-              (util/post-message :write-files {:repo repo :files files})
+              (util/post-message :write-files (pr-str {:repo repo :files files}))
               (swap! *writes disj (:db/id page-block))))))
       ;; In e2e tests, "card" page in db has no :file/path
       (js/console.error "File path from page-block is not valid" page-block tree))))

+ 18 - 15
src/main/frontend/worker/rtc/core.cljs

@@ -833,11 +833,11 @@
 
 
 (defn init-state
-  [ws data-from-ws-chan token]
+  [ws data-from-ws-chan repo token]
   ;; {:post [(m/validate state-schema %)]}
   {:*rtc-state (atom :closed :validator rtc-state-validator)
    :*graph-uuid (atom nil)
-   :*repo (atom nil)
+   :*repo (atom repo)
    :*db-conn (atom nil)
    :*token (atom token)
    :*date-formatter (atom nil)
@@ -865,31 +865,33 @@
        state
        (merge
         {:rtc-state @(:*rtc-state state)
-         :ws-state (ws/get-state @(:*ws state))
-         :auto-push-updates? (and @*state @(:*auto-push-client-ops? state))})))))
+         :ws-state (some-> @(:*ws state) ws/get-state)
+         :auto-push-updates? (when-let [a (:*auto-push-client-ops? state)]
+                               @a)})))))
 
 ;; FIXME: token might be expired
 (defn <init-state
-  [token]
+  [repo token]
   (go
     (let [data-from-ws-chan (chan (async/sliding-buffer 100))
           ws-opened-ch (chan)
           ws (ws/ws-listen token data-from-ws-chan ws-opened-ch)]
       (<! ws-opened-ch)
-      (let [state (init-state ws data-from-ws-chan token)]
+      (let [state (init-state ws data-from-ws-chan repo token)]
         (reset! *state state)
         state))))
 
 (defn <start-rtc
   [repo conn token]
   (go
-    (let [state (<init-state token)
+    (let [state (<init-state repo token)
           config (worker-state/get-config repo)]
       (if-let [graph-uuid (op-mem-layer/get-graph-uuid repo)]
         (<! (<loop-for-rtc state graph-uuid repo conn (common-config/get-date-formatter config)))
-        (worker-util/post-message :notification [[:div
-                                                  [:p "RTC is not supported for this graph"]]
-                                                 :error])))))
+        (worker-util/post-message :notification (pr-str
+                                                 [[:div
+                                                   [:p "RTC is not supported for this graph"]]
+                                                  :error]))))))
 
 (defn <stop-rtc
   []
@@ -903,10 +905,10 @@
     (<toggle-auto-push-client-ops state)))
 
 (defn <get-graphs
-  [token]
+  [repo token]
   (let [d (p/deferred)]
     (go
-     (let [state (or @*state (<! (<init-state token)))
+     (let [state (or @*state (<! (<init-state repo token)))
            graph-list (with-sub-data-from-ws state
                         (<! (ws/<send! state {:req-id (get-req-id)
                                               :action "list-graphs"}))
@@ -917,7 +919,8 @@
 
 (add-watch *state :notify-main-thread
            (fn [_ _ old new]
-             (let [new-state (get-debug-state @(:*repo new) new)
-                   old-state (get-debug-state @(:*repo old) old)]
+             (let [*repo (:*repo new)
+                   new-state (get-debug-state @*repo new)
+                   old-state (get-debug-state @*repo old)]
                (when (not= new-state old-state)
-                 (worker-util/post-message :rtc-sync-state (bean/->js new-state))))))
+                 (worker-util/post-message :rtc-sync-state (pr-str new-state))))))

+ 1 - 1
src/test/frontend/worker/rtc/fixture.cljs

@@ -19,7 +19,7 @@
   []
   (let [data-from-ws-chan (chan (async/sliding-buffer 100))
         ws (rtc-mock/mock-websocket data-from-ws-chan)]
-    (assoc (rtc-core/init-state ws data-from-ws-chan)
+    (assoc (rtc-core/init-state ws data-from-ws-chan test-helper/test-db "")
            :*auto-push-client-ops? (atom false))))
 
 (defn- <start-rtc-loop