Skip to content

Commit

Permalink
enhance(rtc): support :profile for api-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed Dec 26, 2024
1 parent f72ec58 commit 559443e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
15 changes: 12 additions & 3 deletions src/main/frontend/db/rtc/debug_ui.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
[]
Expand All @@ -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))
Expand Down Expand Up @@ -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))))}
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/main/frontend/worker/db_worker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/main/frontend/worker/rtc/client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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)))]
Expand Down
7 changes: 6 additions & 1 deletion src/main/frontend/worker/rtc/const.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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)))))

Expand All @@ -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]]
Expand Down
41 changes: 29 additions & 12 deletions src/main/frontend/worker/rtc/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))]
Expand Down Expand Up @@ -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
Expand All @@ -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))))
Expand Down

0 comments on commit 559443e

Please sign in to comment.