From 559443e2660f6604543d70986accd1ec190aaf95 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Thu, 26 Dec 2024 19:09:04 +0800 Subject: [PATCH] enhance(rtc): support :profile for api-calls --- src/main/frontend/db/rtc/debug_ui.cljs | 15 +++++++-- src/main/frontend/worker/db_worker.cljs | 4 +++ src/main/frontend/worker/rtc/client.cljs | 7 ++-- src/main/frontend/worker/rtc/const.cljs | 7 +++- src/main/frontend/worker/rtc/core.cljs | 41 +++++++++++++++++------- 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/main/frontend/db/rtc/debug_ui.cljs b/src/main/frontend/db/rtc/debug_ui.cljs index cdfffc7854e..06aa4f487f0 100644 --- a/src/main/frontend/db/rtc/debug_ui.cljs +++ b/src/main/frontend/db/rtc/debug_ui.cljs @@ -3,6 +3,7 @@ (:require [fipp.edn :as fipp] [frontend.common.missionary-util :as c.m] [frontend.db :as db] + [frontend.handler.db-based.rtc-flows :as rtc-flows] [frontend.handler.user :as user] [frontend.persist-db.browser :as db-browser] [frontend.state :as state] @@ -15,7 +16,6 @@ [rum.core :as rum])) (defonce debug-state (:rtc/state @state/state)) -(defonce rtc-log-flow (m/watch (:rtc/log @state/state))) (defn- stop [] @@ -37,7 +37,7 @@ logs)] (reset! (get state ::logs) logs*) logs*)) - nil rtc-log-flow) + nil rtc-flows/rtc-log-flow) ::sub-logs)] (reset! (get state ::sub-log-canceler) canceler) state)) @@ -102,6 +102,7 @@ :remote-graphs (:remote-graphs debug-state*) :online-users (:online-users debug-state*) :auto-push? (:auto-push? debug-state*) + :remote-profile? (:remote-profile? debug-state*) :current-page (state/get-current-page) :blocks-count (when-let [page (state/get-current-page)] (count (:block/_page (db/get-page page))))} @@ -126,7 +127,15 @@ {:on-click (fn [] (let [^object worker @db-browser/*worker] - (.rtc-toggle-auto-push worker (state/get-current-repo))))})] + (.rtc-toggle-auto-push worker)))})] + [:div.mr-2 (ui/button (str "Toggle remote profile(" + (if (:remote-profile? debug-state*) + "ON" "OFF") + ")") + {:on-click + (fn [] + (let [^object worker @db-browser/*worker] + (.rtc-toggle-remote-profile worker)))})] [:div (shui/button {:variant :outline :class "text-red-rx-09 border-red-rx-08 hover:text-red-rx-10" diff --git a/src/main/frontend/worker/db_worker.cljs b/src/main/frontend/worker/db_worker.cljs index a55d727ec7b..03eaa3bdf7c 100644 --- a/src/main/frontend/worker/db_worker.cljs +++ b/src/main/frontend/worker/db_worker.cljs @@ -755,6 +755,10 @@ [this] (rtc-core/rtc-toggle-auto-push)) + (rtc-toggle-remote-profile + [this] + (rtc-core/rtc-toggle-remote-profile)) + (rtc-grant-graph-access [this token graph-uuid target-user-uuids-str target-user-emails-str] (let [target-user-uuids (ldb/read-transit-str target-user-uuids-str) diff --git a/src/main/frontend/worker/rtc/client.cljs b/src/main/frontend/worker/rtc/client.cljs index ba4f77ce9e3..e9f69f81730 100644 --- a/src/main/frontend/worker/rtc/client.cljs +++ b/src/main/frontend/worker/rtc/client.cljs @@ -326,7 +326,7 @@ (defn new-task--push-local-ops "Return a task: push local updates" - [repo conn graph-uuid date-formatter get-ws-create-task add-log-fn] + [repo conn graph-uuid date-formatter get-ws-create-task *remote-profile? add-log-fn] (m/sp (let [block-ops-map-coll (client-op/get&remove-all-block-ops repo)] (when-let [block-uuid->remote-ops (not-empty (gen-block-uuid->remote-ops @conn block-ops-map-coll))] @@ -336,8 +336,9 @@ (let [local-tx (client-op/get-local-tx repo) r (try (m/? (ws-util/send&recv get-ws-create-task - {:action "apply-ops" :graph-uuid graph-uuid - :ops ops-for-remote :t-before (or local-tx 1)})) + (cond-> {:action "apply-ops" :graph-uuid graph-uuid + :ops ops-for-remote :t-before (or local-tx 1)} + (true? @*remote-profile?) (assoc :profile true)))) (catch :default e (rollback repo block-ops-map-coll) (throw e)))] diff --git a/src/main/frontend/worker/rtc/const.cljs b/src/main/frontend/worker/rtc/const.cljs index 749686b3a4e..674f64a1d7a 100644 --- a/src/main/frontend/worker/rtc/const.cljs +++ b/src/main/frontend/worker/rtc/const.cljs @@ -78,6 +78,7 @@ "TODO: split this mix schema to multiple ones" [:map [:req-id :string] + [:profile {:optional true} :map] [:t {:optional true} :int] [:t-before {:optional true} :int] [:failed-ops {:optional true} [:sequential to-ws-op-schema]] @@ -172,7 +173,9 @@ (fn [api-schema] (let [[api-name [type']] api-schema] (if (= :map type') - [api-name (vec (concat (second api-schema) [[:req-id :string] [:action :string]]))] + [api-name (vec (concat (second api-schema) [[:req-id :string] + [:action :string] + [:profile {:optional true} :boolean]]))] api-schema))) api-schema-seq))))) @@ -190,12 +193,14 @@ [:map [:req-id :string] [:action :string] + [:profile {:optional true} :boolean] [:graph-uuid :string] [:ops [:sequential to-ws-op-schema]] [:t-before :int]] [:map [:req-id :string] [:action :string] + [:profile {:optional true} :boolean] [:s3-key :string]]]] ["presign-put-temp-s3-obj" [:map]] diff --git a/src/main/frontend/worker/rtc/core.cljs b/src/main/frontend/worker/rtc/core.cljs index 86be9b016a1..033dd9d04cb 100644 --- a/src/main/frontend/worker/rtc/core.cljs +++ b/src/main/frontend/worker/rtc/core.cljs @@ -180,6 +180,7 @@ & {:keys [auto-push? debug-ws-url] :or {auto-push? true}}] (let [ws-url (or debug-ws-url (ws-util/get-ws-url token)) *auto-push? (atom auto-push?) + *remote-profile? (atom false) *last-calibrate-t (atom nil) *online-users (atom nil) *assets-sync-loop-canceler (atom nil) @@ -195,10 +196,11 @@ (r.asset/create-assets-sync-loop repo get-ws-create-task graph-uuid conn *auto-push?) mixed-flow (create-mixed-flow repo get-ws-create-task *auto-push? *online-users)] (assert (some? *current-ws)) - {:rtc-state-flow (create-rtc-state-flow (create-ws-state-flow *current-ws)) - :*rtc-auto-push? *auto-push? - :*online-users *online-users - :onstarted-task started-dfv + {:rtc-state-flow (create-rtc-state-flow (create-ws-state-flow *current-ws)) + :*rtc-auto-push? *auto-push? + :*rtc-remote-profile? *remote-profile? + :*online-users *online-users + :onstarted-task started-dfv :rtc-loop-task (holding-rtc-lock started-dfv @@ -225,7 +227,7 @@ :local-update-check (m/? (r.client/new-task--push-local-ops repo conn graph-uuid date-formatter - get-ws-create-task add-log-fn)) + get-ws-create-task *remote-profile? add-log-fn)) :online-users-updated (reset! *online-users (:online-users (:value event))) @@ -246,16 +248,21 @@ (when @*assets-sync-loop-canceler (@*assets-sync-loop-canceler))))))})) (def ^:private empty-rtc-loop-metadata - {:graph-uuid nil + {:repo nil + :graph-uuid nil :user-uuid nil :rtc-state-flow nil :*rtc-auto-push? nil + :*rtc-remote-profile? nil :*online-users nil :*rtc-lock nil :canceler nil :*last-stop-exception nil}) -(defonce ^:private *rtc-loop-metadata (atom empty-rtc-loop-metadata)) +(defonce ^:private *rtc-loop-metadata (atom empty-rtc-loop-metadata + :validator + (fn [v] (= (set (keys empty-rtc-loop-metadata)) + (set (keys v)))))) ;;; ================ API ================ (defn new-task--rtc-start @@ -268,7 +275,7 @@ (let [user-uuid (:sub (worker-util/parse-jwt token)) config (worker-state/get-config repo) date-formatter (common-config/get-date-formatter config) - {:keys [rtc-state-flow *rtc-auto-push? rtc-loop-task *online-users onstarted-task]} + {:keys [rtc-state-flow *rtc-auto-push? *rtc-remote-profile? rtc-loop-task *online-users onstarted-task]} (create-rtc-loop graph-uuid repo conn date-formatter token) *last-stop-exception (atom nil) canceler (c.m/run-task rtc-loop-task :rtc-loop-task @@ -283,6 +290,7 @@ :user-uuid user-uuid :rtc-state-flow rtc-state-flow :*rtc-auto-push? *rtc-auto-push? + :*rtc-remote-profile? *rtc-remote-profile? :*online-users *online-users :*rtc-lock *rtc-lock :canceler canceler @@ -303,6 +311,11 @@ (when-let [*auto-push? (:*rtc-auto-push? @*rtc-loop-metadata)] (swap! *auto-push? not))) +(defn rtc-toggle-remote-profile + [] + (when-let [*rtc-remote-profile? (:*rtc-remote-profile? @*rtc-loop-metadata)] + (swap! *rtc-remote-profile? not))) + (defn new-task--get-graphs [token] (let [{:keys [get-ws-create-task]} (gen-get-ws-create-map--memoized (ws-util/get-ws-url token))] @@ -356,14 +369,15 @@ (let [rtc-loop-metadata-flow (m/watch *rtc-loop-metadata)] (m/ap (let [{rtc-lock :*rtc-lock - :keys [repo graph-uuid user-uuid rtc-state-flow *rtc-auto-push? *online-users - *last-stop-exception]} + :keys [repo graph-uuid user-uuid rtc-state-flow *rtc-auto-push? *rtc-remote-profile? + *online-users *last-stop-exception]} (m/?< rtc-loop-metadata-flow)] (try (when (and repo rtc-state-flow *rtc-auto-push? rtc-lock) (m/?< (m/latest - (fn [rtc-state rtc-auto-push? rtc-lock online-users pending-local-ops-count local-tx remote-tx] + (fn [rtc-state rtc-auto-push? rtc-remote-profile? + rtc-lock online-users pending-local-ops-count local-tx remote-tx] {:graph-uuid graph-uuid :user-uuid user-uuid :unpushed-block-update-count pending-local-ops-count @@ -372,9 +386,12 @@ :rtc-state rtc-state :rtc-lock rtc-lock :auto-push? rtc-auto-push? + :remote-profile? rtc-remote-profile? :online-users online-users :last-stop-exception-ex-data (some-> *last-stop-exception deref ex-data)}) - rtc-state-flow (m/watch *rtc-auto-push?) (m/watch rtc-lock) (m/watch *online-users) + rtc-state-flow + (m/watch *rtc-auto-push?) (m/watch *rtc-remote-profile?) + (m/watch rtc-lock) (m/watch *online-users) (client-op/create-pending-block-ops-count-flow repo) (rtc-log-and-state/create-local-t-flow graph-uuid) (rtc-log-and-state/create-remote-t-flow graph-uuid))))