Skip to content

Commit

Permalink
fix(rtc): delete-graph return a promise now
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed Dec 3, 2024
1 parent a2b3efa commit 632788e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 45 deletions.
87 changes: 51 additions & 36 deletions src/main/frontend/common/async_util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespaces. See also: https://gist.github.com/vvvvalvalval/f1250cec76d3719a8343"
#?(:cljs (:require [cljs.core.async.impl.channels :refer [ManyToManyChannel]]
[clojure.core.async :as async]
[logseq.common.util :as common-util])))
[logseq.common.util :as common-util]
[promesa.core :as p])))

#?(:cljs
(defn throw-err
Expand All @@ -14,6 +15,20 @@
[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 All @@ -23,7 +38,7 @@

#?(:cljs
(defn <ratelimit
"return a channel CH,
"return a channel CH,
ratelimit flush items in in-ch every max-duration(ms),
opts:
- :filter-fn filter item before putting items into returned CH, (filter-fn item)
Expand All @@ -34,42 +49,42 @@
- :chan-buffer buffer of return CH, default use (async/chan 1000)
- :flush-now-ch flush the content in the queue immediately
- :refresh-timeout-ch refresh (timeout max-duration)"
[in-ch max-duration & {:keys [filter-fn flush-fn stop-ch distinct-key-fn chan-buffer flush-now-ch refresh-timeout-ch]}]
(let [ch (if chan-buffer (async/chan chan-buffer) (async/chan 1000))
stop-ch* (or stop-ch (async/chan))
flush-now-ch* (or flush-now-ch (async/chan))
refresh-timeout-ch* (or refresh-timeout-ch (async/chan))]
(async/go-loop [timeout-ch (async/timeout max-duration) coll []]
(let [{:keys [refresh-timeout timeout e stop flush-now]}
(async/alt! refresh-timeout-ch* {:refresh-timeout true}
timeout-ch {:timeout true}
in-ch ([e] {:e e})
stop-ch* {:stop true}
flush-now-ch* {:flush-now true})]
(cond
refresh-timeout
(recur (async/timeout max-duration) coll)
[in-ch max-duration & {:keys [filter-fn flush-fn stop-ch distinct-key-fn chan-buffer flush-now-ch refresh-timeout-ch]}]
(let [ch (if chan-buffer (async/chan chan-buffer) (async/chan 1000))
stop-ch* (or stop-ch (async/chan))
flush-now-ch* (or flush-now-ch (async/chan))
refresh-timeout-ch* (or refresh-timeout-ch (async/chan))]
(async/go-loop [timeout-ch (async/timeout max-duration) coll []]
(let [{:keys [refresh-timeout timeout e stop flush-now]}
(async/alt! refresh-timeout-ch* {:refresh-timeout true}
timeout-ch {:timeout true}
in-ch ([e] {:e e})
stop-ch* {:stop true}
flush-now-ch* {:flush-now true})]
(cond
refresh-timeout
(recur (async/timeout max-duration) coll)

(or flush-now timeout)
(do (async/onto-chan! ch coll false)
(flush-fn coll)
(drain-chan flush-now-ch*)
(recur (async/timeout max-duration) []))
(or flush-now timeout)
(do (async/onto-chan! ch coll false)
(flush-fn coll)
(drain-chan flush-now-ch*)
(recur (async/timeout max-duration) []))

(some? e)
(let [filter-v (filter-fn e)
filter-v* (if (instance? ManyToManyChannel filter-v)
(async/<! filter-v)
filter-v)]
(if filter-v*
(recur timeout-ch (cond->> (conj coll e)
distinct-key-fn (common-util/distinct-by distinct-key-fn)
true vec))
(recur timeout-ch coll)))
(some? e)
(let [filter-v (filter-fn e)
filter-v* (if (instance? ManyToManyChannel filter-v)
(async/<! filter-v)
filter-v)]
(if filter-v*
(recur timeout-ch (cond->> (conj coll e)
distinct-key-fn (common-util/distinct-by distinct-key-fn)
true vec))
(recur timeout-ch coll)))

(or stop
(or stop
;; got nil from in-ch, means in-ch is closed
;; so we stop the whole go-loop
(nil? e))
(async/close! ch))))
ch)))
(nil? e))
(async/close! ch))))
ch)))
19 changes: 10 additions & 9 deletions src/main/frontend/components/repo.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns frontend.components.repo
(:require [cljs.core.async :as async :refer [<! go]]
[clojure.string :as string]
(:require [clojure.string :as string]
[electron.ipc :as ipc]
[frontend.common.async-util :as async-util]
[frontend.config :as config]
[frontend.context.i18n :refer [t]]
[frontend.db :as db]
Expand Down Expand Up @@ -119,14 +119,15 @@
action-confirm-fn! (if only-cloud?
(fn []
(when (or manager? (not db-graph?))
(let [delete-graph (if db-graph?
rtc-handler/<rtc-delete-graph!
file-sync/<delete-graph)]
(let [<delete-graph (if db-graph?
rtc-handler/<rtc-delete-graph!
(fn [graph-uuid]
(async-util/c->p (file-sync/<delete-graph graph-uuid))))]
(state/set-state! [:file-sync/remote-graphs :loading] true)
(go (<! (delete-graph GraphUUID))
(state/delete-repo! repo)
(state/delete-remote-graph! repo)
(state/set-state! [:file-sync/remote-graphs :loading] false)))))
(p/do! (<delete-graph GraphUUID)
(state/delete-repo! repo)
(state/delete-remote-graph! repo)
(state/set-state! [:file-sync/remote-graphs :loading] false)))))
unlink-or-remote-fn!)
confirm-fn!
(fn []
Expand Down

0 comments on commit 632788e

Please sign in to comment.