Skip to content

Commit

Permalink
enhance(rtc): deprecate <wrap-ensure-id&access-token
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed Dec 3, 2024
1 parent eb273f1 commit a2b3efa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 79 deletions.
21 changes: 3 additions & 18 deletions src/main/frontend/common/async_util.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(ns frontend.common.async-util
"Some cljs.core.async relate macros and fns, used in worker and frontend
namespaces. See also: https://gist.github.com/vvvvalvalval/f1250cec76d3719a8343"
#?(:cljs (:require [promesa.core :as p]
[logseq.common.util :as common-util]
#?(:cljs (:require [cljs.core.async.impl.channels :refer [ManyToManyChannel]]
[clojure.core.async :as async]
[cljs.core.async.impl.channels :refer [ManyToManyChannel]])))
[logseq.common.util :as common-util])))

#?(:cljs
(defn throw-err
Expand All @@ -15,20 +14,6 @@
[port]
`(throw-err (cljs.core.async/<! ~port)))

#?(:cljs
(defn c->p
"Converts a Core.async channel to a Promise"
[chan]
(let [d (p/deferred)]
(if chan
(async/go
(let [result (async/<! chan)]
(if (instance? ExceptionInfo result)
(p/reject! d result)
(p/resolve! d result))))
(p/resolve! d nil))
d)))

#?(:cljs
(defn drain-chan
"drop all stuffs in CH, and return all of them"
Expand Down Expand Up @@ -87,4 +72,4 @@
;; so we stop the whole go-loop
(nil? e))
(async/close! ch))))
ch)))
ch)))
31 changes: 15 additions & 16 deletions src/main/frontend/components/repo.cljs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
(ns frontend.components.repo
(:require [frontend.config :as config]
(:require [cljs.core.async :as async :refer [<! go]]
[clojure.string :as string]
[electron.ipc :as ipc]
[frontend.config :as config]
[frontend.context.i18n :refer [t]]
[frontend.db :as db]
[frontend.handler.repo :as repo-handler]
[frontend.handler.db-based.rtc :as rtc-handler]
[frontend.handler.file-based.nfs :as nfs-handler]
[frontend.handler.file-sync :as file-sync]
[frontend.handler.graph :as graph]
[frontend.handler.notification :as notification]
[frontend.handler.repo :as repo-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.user :as user-handler]
[frontend.mobile.util :as mobile-util]
[frontend.state :as state]
[frontend.ui :as ui]
[frontend.util :as util]
[rum.core :as rum]
[frontend.mobile.util :as mobile-util]
[frontend.util.fs :as fs-util]
[frontend.util.text :as text-util]
[promesa.core :as p]
[electron.ipc :as ipc]
[goog.object :as gobj]
[cljs.core.async :as async :refer [go <!]]
[clojure.string :as string]
[frontend.handler.file-sync :as file-sync]
[frontend.handler.notification :as notification]
[frontend.util.fs :as fs-util]
[frontend.handler.user :as user-handler]
[logseq.shui.ui :as shui]
[frontend.handler.db-based.rtc :as rtc-handler]
[frontend.handler.graph :as graph]
[frontend.common.async-util :as async-util]))
[promesa.core :as p]
[rum.core :as rum]))

(rum/defc normalized-graph-label
[{:keys [url remote? GraphName GraphUUID] :as graph} on-click]
Expand Down Expand Up @@ -432,7 +431,7 @@
(->
(p/do
(state/set-state! :rtc/uploading? true)
(js/Promise. (rtc-handler/new-task--rtc-create-graph! repo))
(rtc-handler/<rtc-create-graph! repo)
(state/set-state! :rtc/uploading? false)
(rtc-handler/<rtc-start! repo))
(p/catch (fn [error]
Expand Down
88 changes: 43 additions & 45 deletions src/main/frontend/handler/db_based/rtc.cljs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
(ns frontend.handler.db-based.rtc
"RTC handler"
(:require [frontend.common.missionary-util :as c.m]
[frontend.config :as config]
(:require [frontend.config :as config]
[frontend.db :as db]
[frontend.handler.notification :as notification]
[frontend.handler.user :as user-handler]
[frontend.state :as state]
[logseq.db :as ldb]
[logseq.db.sqlite.common-db :as sqlite-common-db]
[missionary.core :as m]
[promesa.core :as p]))

(defn new-task--rtc-create-graph!
(defn <rtc-create-graph!
[repo]
(m/sp
(when-let [^js worker @state/*db-worker]
(m/? user-handler/task--ensure-id&access-token)
(let [token (state/get-auth-id-token)
repo-name (sqlite-common-db/sanitize-db-name repo)]
(c.m/<? (.rtc-async-upload-graph worker repo token repo-name))))))
(when-let [^js worker @state/*db-worker]
(p/do!
(js/Promise. user-handler/task--ensure-id&access-token)
(let [token (state/get-auth-id-token)
repo-name (sqlite-common-db/sanitize-db-name repo)]
(.rtc-async-upload-graph worker repo token repo-name)))))

(defn <rtc-delete-graph!
[graph-uuid]
(when-let [^js worker @state/*db-worker]
(user-handler/<wrap-ensure-id&access-token
(p/do!
(js/Promise. user-handler/task--ensure-id&access-token)
(let [token (state/get-auth-id-token)]
(.rtc-delete-graph worker token graph-uuid)))))

(defn <rtc-download-graph!
[graph-name graph-uuid timeout-ms]
(when-let [^js worker @state/*db-worker]
(state/set-state! :rtc/downloading-graph-uuid graph-uuid)
(user-handler/<wrap-ensure-id&access-token
(p/let [token (state/get-auth-id-token)
download-info-uuid* (.rtc-request-download-graph worker token graph-uuid)
download-info-uuid (ldb/read-transit-str download-info-uuid*)
result (.rtc-wait-download-graph-info-ready worker token download-info-uuid graph-uuid timeout-ms)
{:keys [_download-info-uuid
download-info-s3-url
_download-info-tx-instant
_download-info-t
_download-info-created-at]
:as result} (ldb/read-transit-str result)]
(->
(when (not= result :timeout)
(assert (some? download-info-s3-url) result)
(.rtc-download-graph-from-s3 worker graph-uuid graph-name download-info-s3-url))
(p/finally
#(state/set-state! :rtc/downloading-graph-uuid nil)))))))
(p/let [_ (js/Promise. user-handler/task--ensure-id&access-token)
token (state/get-auth-id-token)
download-info-uuid* (.rtc-request-download-graph worker token graph-uuid)
download-info-uuid (ldb/read-transit-str download-info-uuid*)
result (.rtc-wait-download-graph-info-ready worker token download-info-uuid graph-uuid timeout-ms)
{:keys [_download-info-uuid
download-info-s3-url
_download-info-tx-instant
_download-info-t
_download-info-created-at]
:as result} (ldb/read-transit-str result)]
(->
(when (not= result :timeout)
(assert (some? download-info-s3-url) result)
(.rtc-download-graph-from-s3 worker graph-uuid graph-name download-info-s3-url))
(p/finally
#(state/set-state! :rtc/downloading-graph-uuid nil))))))

(defn <rtc-stop!
[]
Expand Down Expand Up @@ -78,23 +77,22 @@

(defn <get-remote-graphs
[]
(let [^js worker @state/*db-worker]
(user-handler/<wrap-ensure-id&access-token
(let [token (state/get-auth-id-token)]
(when worker
(p/let [result (.rtc-get-graphs worker token)
graphs (ldb/read-transit-str result)
result (->> graphs
(remove (fn [graph] (= (:graph-status graph) "deleting")))
(mapv (fn [graph]
(merge
(let [url (str config/db-version-prefix (:graph-name graph))]
{:url url
:GraphName (:graph-name graph)
:GraphUUID (:graph-uuid graph)
:rtc-graph? true})
(dissoc graph :graph-uuid :graph-name)))))]
(state/set-state! :rtc/graphs result)))))))
(when-let [^js worker @state/*db-worker]
(p/let [_ (js/Promise. user-handler/task--ensure-id&access-token)
token (state/get-auth-id-token)
result (.rtc-get-graphs worker token)
graphs (ldb/read-transit-str result)
result (->> graphs
(remove (fn [graph] (= (:graph-status graph) "deleting")))
(mapv (fn [graph]
(merge
(let [url (str config/db-version-prefix (:graph-name graph))]
{:url url
:GraphName (:graph-name graph)
:GraphUUID (:graph-uuid graph)
:rtc-graph? true})
(dissoc graph :graph-uuid :graph-name)))))]
(state/set-state! :rtc/graphs result))))

(defn <rtc-get-users-info
[]
Expand Down
1 change: 1 addition & 0 deletions src/main/frontend/handler/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Macros.")

(defmacro <wrap-ensure-id&access-token
"Deprecated"
[& body]
`(cljs.core.async/go
(if-some [exp# (cljs.core.async/<! (<ensure-id&access-token))]
Expand Down

0 comments on commit a2b3efa

Please sign in to comment.