mirror of
https://github.com/logseq/logseq.git
synced 2026-04-30 17:06:23 +00:00
fix: nbb not working in most deps
core.async isn't nbb compatible yet. Moved async transact to its own frontend ns
This commit is contained in:
45
src/main/frontend/db/transact.cljs
Normal file
45
src/main/frontend/db/transact.cljs
Normal file
@@ -0,0 +1,45 @@
|
||||
(ns frontend.db.transact
|
||||
"Provides async transact for use with ldb/transact!"
|
||||
(:require [clojure.core.async :as async]
|
||||
[clojure.core.async.interop :refer [p->c]]
|
||||
[promesa.core :as p]))
|
||||
|
||||
(defonce *request-id (atom 0))
|
||||
(defonce requests (async/chan 1000))
|
||||
(defonce *unfinished-request-ids (atom #{}))
|
||||
|
||||
(defn request-finished?
|
||||
"Whether any DB transaction request has been finished"
|
||||
[]
|
||||
(empty? @*unfinished-request-ids))
|
||||
|
||||
(defn get-next-request-id
|
||||
[]
|
||||
(swap! *request-id inc))
|
||||
|
||||
(defn add-request!
|
||||
[request-id request-f]
|
||||
(let [resp (p/deferred)
|
||||
new-request {:id request-id
|
||||
:request request-f
|
||||
:response resp}]
|
||||
(swap! *unfinished-request-ids conj request-id)
|
||||
(async/go (async/>! requests new-request))
|
||||
resp))
|
||||
|
||||
(defn listen-for-requests []
|
||||
(async/go-loop []
|
||||
(when-let [{:keys [id request response]} (async/<! requests)]
|
||||
(let [result (async/<! (p->c (request)))]
|
||||
(p/resolve! response result)
|
||||
(swap! *unfinished-request-ids disj id))
|
||||
(recur))))
|
||||
|
||||
(defn transact [worker-transact repo tx-data tx-meta]
|
||||
(let [request-id (get-next-request-id)
|
||||
tx-meta' (assoc tx-meta
|
||||
:request-id request-id
|
||||
;; not from remote(rtc)
|
||||
:local-tx? true)]
|
||||
(add-request! request-id (fn async-request []
|
||||
(worker-transact repo tx-data tx-meta')))))
|
||||
Reference in New Issue
Block a user