Prechádzať zdrojové kódy

wip: rtc debug ui refactor

Tienson Qin 1 rok pred
rodič
commit
d5ea7b56c0

+ 24 - 67
src/main/frontend/db/rtc/debug_ui.cljs

@@ -4,44 +4,19 @@
    [frontend.worker.rtc.macro :refer [with-sub-data-from-ws get-req-id get-result-ch]])
   (:require [cljs.core.async :as async :refer [<! go]]
             [fipp.edn :as fipp]
-            [frontend.worker.async-util :include-macros true :refer [<? go-try]]
             [frontend.db :as db]
-            [frontend.db.conn :as conn]
             [frontend.worker.rtc.core :as rtc-core]
-            [frontend.worker.rtc.db-listener :as db-listener]
-            [frontend.worker.rtc.full-upload-download-graph :as full-upload-download-graph]
             [frontend.worker.rtc.op-mem-layer :as op-mem-layer]
             [frontend.worker.rtc.ws :as ws]
-            [frontend.handler.notification :as notification]
             [frontend.handler.user :as user]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.util :as util]
             [rum.core :as rum]
-            [frontend.config :as config]))
+            [frontend.persist-db.browser :as db-browser]))
 
 (defonce debug-state (atom nil))
 
-(defn- <start-rtc
-  ([]
-   (go
-     (let [state (<! (rtc-core/<init-state (state/get-auth-id-token) config/RTC-WS-URL))]
-       (<! (<start-rtc state)))))
-  ([state]
-   (go
-     (if (= :expired-token (:anom (ex-data state)))
-       (prn ::<start-rtc state)
-       (let [repo (state/get-current-repo)]
-         (<! (<start-rtc state repo))))))
-  ([state repo]
-   (go
-     (if-let [graph-uuid (op-mem-layer/get-graph-uuid repo)]
-       (do (reset! debug-state state)
-           (<! (rtc-core/<loop-for-rtc state graph-uuid repo (db/get-db repo false) (state/get-date-formatter)))
-           state)
-       (do (notification/show! "not a rtc-graph" :error false)
-           nil)))))
-
 (defn- stop
   []
   (async/close! @(:*stop-rtc-loop-chan @debug-state))
@@ -51,21 +26,6 @@
   []
   (async/put! (:force-push-client-ops-chan @debug-state) true))
 
-(defn- <download-graph
-  [repo graph-uuid]
-  (go-try
-   (let [state (<! (rtc-core/<init-state (state/get-auth-id-token) config/RTC-WS-URL))]
-     (<? (full-upload-download-graph/<download-graph state repo graph-uuid)))))
-
-(defn- <upload-graph
-  []
-  (go
-    (let [state (<! (rtc-core/<init-state (state/get-auth-id-token) config/RTC-WS-URL))
-          repo (state/get-current-repo)]
-      (<! (full-upload-download-graph/<upload-graph state repo (conn/get-db repo)))
-      (let [conn (conn/get-db repo false)]
-        (db-listener/listen-db-to-generate-ops repo conn)))))
-
 (rum/defcs ^:large-vars/cleanup-todo rtc-debug-ui <
   rum/reactive
   (rum/local nil ::graph-uuid)
@@ -100,7 +60,7 @@
                  :icon "refresh"
                  :on-click (fn [_]
                              (go
-                               (let [s (or s (<! (rtc-core/<init-state (state/get-auth-id-token) config/RTC-WS-URL)))
+                               (let [s (or s (<! (rtc-core/<init-state (state/get-auth-id-token))))
                                      graph-list (with-sub-data-from-ws s
                                                   (<! (ws/<send! s {:req-id (get-req-id)
                                                                     :action "list-graphs"}))
@@ -127,9 +87,9 @@
        (ui/button "start" {:class "my-2"
                            :on-click (fn []
                                        (prn :start-rtc)
-                                       (if s
-                                         (<start-rtc s)
-                                         (<start-rtc)))})
+                                       (let [token (state/get-auth-id-token)
+                                             ^object worker @db-browser/*worker]
+                                         (.rtc-start worker (state/get-current-repo) token)))})
 
        [:div.my-2.flex
         [:div.mr-2 (ui/button (str "send pending ops")
@@ -150,14 +110,13 @@
         (ui/button "grant graph access to"
                    {:class "mr-2"
                     :on-click (fn []
-                                (go
-                                  (let [user-uuid (some-> @(::grant-access-to-user state) parse-uuid)
-                                        user-email (when-not user-uuid @(::grant-access-to-user state))]
-                                    (when-let [graph-uuid @(::graph-uuid state)]
-                                      (<! (rtc-core/<grant-graph-access-to-others
-                                           s graph-uuid
-                                           :target-user-uuids (some-> user-uuid vector)
-                                           :target-user-emails (some-> user-email vector)))))))})
+                                (let [user-uuid (some-> @(::grant-access-to-user state) parse-uuid)
+                                      user-email (when-not user-uuid @(::grant-access-to-user state))]
+                                  (when-let [graph-uuid @(::graph-uuid state)]
+                                    (let [^object worker @db-browser/*worker]
+                                      (.rtc-grant-graph-access worker graph-uuid
+                                                               (some-> user-uuid vector)
+                                                               (some-> user-email vector))))))})
 
         [:input.form-input.my-2
          {:on-change (fn [e] (reset! (::grant-access-to-user state) (util/evalue e)))
@@ -170,16 +129,12 @@
       (ui/button (str "download graph to")
                  {:class "mr-2"
                   :on-click (fn []
-                              (go
-                                (when-let [repo @(::download-graph-to-repo state)]
-                                  (when-let [graph-uuid @(::graph-uuid-to-download state)]
-                                    (prn :download-graph graph-uuid :to repo)
-                                    (try
-                                      (<? (<download-graph repo graph-uuid))
-                                      (notification/show! "download graph successfully")
-                                      (catch :default e
-                                        (notification/show! "download graph failed" :error)
-                                        (prn ::download-graph-failed e)))))))})
+                              (when-let [repo @(::download-graph-to-repo state)]
+                                (when-let [graph-uuid @(::graph-uuid-to-download state)]
+                                  (prn :download-graph graph-uuid :to repo)
+                                  (let [token (state/get-auth-id-token)
+                                        ^object worker @db-browser/*worker]
+                                    (.rtc-download-graph worker repo token graph-uuid)))))})
       [:div.flex.flex-col
        [:select
         {:on-change (fn [e]
@@ -197,7 +152,9 @@
                                (set! (.-value (.-target e)) ""))))
          :default-value "repo name here"}]]]
      [:div.flex.my-2
-      (ui/button (str "upload current repo") {:on-click (fn []
-                                                          (go
-                                                            (<! (<upload-graph))
-                                                            (notification/show! "upload graph successfully")))})]]))
+      (ui/button (str "upload current repo")
+                 {:on-click (fn []
+                              (let [repo (state/get-current-repo)
+                                    token (state/get-auth-id-token)
+                                    ^js worker @db-browser/*worker]
+                                (.rtc-upload-graph worker repo token)))})]]))

+ 77 - 5
src/main/frontend/db_worker.cljs

@@ -18,7 +18,12 @@
             [frontend.worker.file :as file]
             [logseq.db :as ldb]
             [frontend.worker.rtc.op-mem-layer :as op-mem-layer]
-            [frontend.worker.rtc.db-listener :as rtc-db-listener]))
+            [frontend.worker.rtc.db-listener :as rtc-db-listener]
+            [frontend.worker.rtc.full-upload-download-graph :as rtc-updown]
+            [frontend.worker.rtc.core :as rtc-core]
+            [clojure.core.async :as async]
+            [frontend.worker.async-util :include-macros true :refer [<?]]
+            [frontend.worker.util :as worker-util]))
 
 (defonce *sqlite state/*sqlite)
 (defonce *sqlite-conns state/*sqlite-conns)
@@ -128,7 +133,7 @@
   [repo conn]
   (d/unlisten! conn :gen-ops)
   (when (op-mem-layer/rtc-db-graph? repo)
-      (rtc-db-listener/listen-db-to-generate-ops repo conn)))
+    (rtc-db-listener/listen-db-to-generate-ops repo conn)))
 
 (defn- create-or-open-db!
   [repo]
@@ -200,7 +205,7 @@
 
 
 #_:clj-kondo/ignore
-(defclass SQLiteDB
+(defclass DBWorker
   (extends js/Object)
 
   (constructor
@@ -215,7 +220,8 @@
      (.-version sqlite)))
 
   (init
-   [_this]
+   [_this rtc-ws-url]
+   (reset! state/*rtc-ws-url rtc-ws-url)
    (init-sqlite-module!))
 
   (listDB
@@ -372,6 +378,72 @@
      (state/set-new-state! new-state)
      nil))
 
+  ;; RTC
+  (rtc-start
+   [this repo token]
+   (when-let [conn (state/get-datascript-conn repo)]
+     (rtc-core/<start-rtc repo conn token)
+     nil))
+
+  (rtc-stop
+   [this]
+   (rtc-core/<stop-rtc)
+   nil)
+
+  (rtc-toggle-sync
+   [this repo]
+   (rtc-core/<toggle-sync)
+   nil)
+
+  (rtc-grant-graph-access
+   [this graph-uuid target-user-uuids target-user-emails]
+   (when-let [state @rtc-core/*state]
+     (rtc-core/<grant-graph-access-to-others
+      state graph-uuid
+      :target-user-uuids target-user-uuids
+      :target-user-emails target-user-emails))
+   nil)
+
+  (rtc-upload-graph
+   [this repo token]
+   (when-let [conn (state/get-datascript-conn repo)]
+     (async/go
+       (try
+         (let [state (<! (rtc-core/<init-state token))]
+          (<! (rtc-updown/<upload-graph state repo conn))
+          (rtc-db-listener/listen-db-to-generate-ops repo conn))
+         (worker-util/post-message :notification
+                                   (pr-str
+                                    [[:div
+                                      [:p "Upload graph successfully"]]]))
+         (catch :default e
+           (worker-util/post-message :notification
+                                     (pr-str
+                                      [[:div
+                                        [:p "upload graph failed"]]
+                                       :error]))
+           (prn ::download-graph-failed e))))
+     nil))
+
+  (rtc-download-graph
+   [this repo token graph-uuid]
+   (async/go
+     (let [state (<! (rtc-core/<init-state token))]
+       (try
+         (<? (rtc-updown/<download-graph state repo graph-uuid))
+         (worker-util/post-message :notification
+                                   (pr-str
+                                    [[:div
+                                      [:p "download graph successfully"]]]))
+         (catch :default e
+           (worker-util/post-message :notification
+                                     (pr-str
+                                      [[:div
+                                        [:p "download graph failed"]]
+                                       :error]))
+           (prn ::download-graph-failed e)))))
+   nil)
+
   (dangerousRemoveAllDbs
    [this repo]
    (p/let [dbs (.listDB this)]
@@ -380,7 +452,7 @@
 (defn init
   "web worker entry"
   []
-  (let [^js obj (SQLiteDB.)]
+  (let [^js obj (DBWorker.)]
     (file/<ratelimit-file-writes!)
     (Comlink/expose obj)))
 

+ 1 - 1
src/main/frontend/persist_db/browser.cljs

@@ -48,7 +48,7 @@
           wrapped-worker (Comlink/wrap worker)]
       (worker-handler/handle-message! worker)
       (reset! *worker wrapped-worker)
-      (-> (p/let [_ (.init wrapped-worker)
+      (-> (p/let [_ (.init wrapped-worker config/RTC-WS-URL)
                   _ (.sync-app-state wrapped-worker
                                      (pr-str
                                       {:git/current-repo (state/get-current-repo)

+ 33 - 7
src/main/frontend/worker/rtc/core.cljs

@@ -23,7 +23,8 @@
             [logseq.db :as ldb]
             [frontend.worker.rtc.const :as rtc-const]
             [frontend.worker.rtc.op-mem-layer :as op-mem-layer]
-            [frontend.worker.rtc.ws :as ws]))
+            [frontend.worker.rtc.ws :as ws]
+            [frontend.state :as state]))
 
 
 ;;                     +-------------+
@@ -825,13 +826,12 @@
 
 
 (defn init-state
-  [ws data-from-ws-chan token ws-addr]
+  [ws data-from-ws-chan token]
   ;; {:post [(m/validate state-schema %)]}
   {:*rtc-state (atom :closed :validator rtc-state-validator)
    :*graph-uuid (atom nil)
    :*repo (atom nil)
    :*db-conn (atom nil)
-   :ws-addr ws-addr
    :*token (atom token)
    :*date-formatter (atom nil)
    :data-from-ws-chan data-from-ws-chan
@@ -842,12 +842,38 @@
    :force-push-client-ops-chan (chan (async/sliding-buffer 1))
    :*ws (atom ws)})
 
+(defonce *state (atom nil))
+
+;; FIXME: token might be expired
 (defn <init-state
-  [auth-id-token ws-addr]
+  [token]
   (go
     (let [data-from-ws-chan (chan (async/sliding-buffer 100))
           ws-opened-ch (chan)
-          token auth-id-token
-          ws (ws/ws-listen ws-addr token data-from-ws-chan ws-opened-ch)]
+          ws (ws/ws-listen token data-from-ws-chan ws-opened-ch)]
       (<! ws-opened-ch)
-      (init-state ws data-from-ws-chan token ws-addr))))
+      (let [state (init-state ws data-from-ws-chan token)]
+        ;; (worker-util/post-message :rtc/sync-state state)
+        (reset! *state state)
+        state))))
+
+(defn <start-rtc
+  [repo conn token]
+  (go
+    (let [state (<init-state token)]
+      (if-let [graph-uuid (op-mem-layer/get-graph-uuid repo)]
+        (<! (<loop-for-rtc state graph-uuid repo conn (state/get-date-formatter)))
+        (worker-util/post-message :notification [[:div
+                                                  [:p "RTC is not supported for this graph"]]
+                                                 :error])))))
+
+(defn <stop-rtc
+  []
+  (when-let [state @*state]
+    (when-let [*chan (:*stop-rtc-loop-chan state)]
+     (async/close! @*chan))))
+
+(defn <toggle-sync
+  []
+  (when-let [state @*state]
+    (<toggle-auto-push-client-ops state)))

+ 2 - 2
src/main/frontend/worker/rtc/full_upload_download_graph.cljs

@@ -37,12 +37,12 @@
 
 (defn <upload-graph
   "Upload current repo to remote, return remote {:req-id xxx :graph-uuid <new-remote-graph-uuid>}"
-  [state repo db]
+  [state repo conn]
   (go
     (let [{:keys [url key all-blocks-str]}
           (with-sub-data-from-ws state
             (<! (<send! state {:req-id (get-req-id) :action "presign-put-temp-s3-obj"}))
-            (let [all-blocks (export-as-blocks db)
+            (let [all-blocks (export-as-blocks @conn)
                   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}))

+ 4 - 3
src/main/frontend/worker/rtc/ws.cljs

@@ -6,6 +6,7 @@
             [cljs.core.async :as async :refer [<! chan offer!]]
             [frontend.worker.async-util :include-macros true :refer [<? go-try]]
             [frontend.worker.rtc.const :as rtc-const]
+            [frontend.worker.state :as state]
             [goog.string :as gstring]))
 
 (def WebSocketOPEN (if (= *target* "nodejs")
@@ -13,8 +14,8 @@
                      js/WebSocket.OPEN))
 
 (defn ws-listen
-  [ws-addr token data-from-ws-chan ws-opened-ch]
-  (let [ws (js/WebSocket. (gstring/format ws-addr token))]
+  [token data-from-ws-chan ws-opened-ch]
+  (let [ws (js/WebSocket. (gstring/format @state/*rtc-ws-url token))]
     (set! (.-onopen ws) (fn [_e] (async/close! ws-opened-ch)))
     (set! (.-onmessage ws) (fn [e]
                              (let [data (js->clj (js/JSON.parse (.-data e)) :keywordize-keys true)]
@@ -39,7 +40,7 @@
      (when (or (nil? ws)
                (> (.-readyState ws) WebSocketOPEN))
        (let [ws-opened-ch (chan)
-             ws* (ws-listen (:ws-addr state) @(:*token state) (:data-from-ws-chan state) ws-opened-ch)]
+             ws* (ws-listen @(:*token state) (:data-from-ws-chan state) ws-opened-ch)]
          (<! ws-opened-ch)
          (reset! (:*ws state) ws*)
          (when-let [graph-uuid @(:*graph-uuid state)]

+ 2 - 0
src/main/frontend/worker/state.cljs

@@ -8,6 +8,8 @@
                        :config {}
                        :git/current-repo nil}))
 
+(defonce *rtc-ws-url (atom nil))
+
 (defonce *sqlite (atom nil))
 ;; repo -> {:db conn :search conn}
 (defonce *sqlite-conns (atom nil))