mirror of https://github.com/logseq/logseq
fix: db requests could be dropped if some requests not finished yet
parent
74a76f0365
commit
b55e4e7380
|
@ -59,29 +59,46 @@
|
||||||
|
|
||||||
(defn get-deferred-response
|
(defn get-deferred-response
|
||||||
[request-id]
|
[request-id]
|
||||||
(get @*request-id->response request-id))
|
(:response (get @*request-id->response request-id)))
|
||||||
|
|
||||||
|
;; run the next request
|
||||||
|
(add-watch *request-id->response :loop-execute-requests
|
||||||
|
(fn [_ _ _ new]
|
||||||
|
(when-let [request-id (some->> (keys new)
|
||||||
|
sort
|
||||||
|
first)]
|
||||||
|
(when-let [callback (:callback (get new request-id))]
|
||||||
|
(callback)))))
|
||||||
|
|
||||||
(defn transact!
|
(defn transact!
|
||||||
([conn tx-data]
|
([conn tx-data]
|
||||||
(transact! conn tx-data nil))
|
(transact! conn tx-data nil))
|
||||||
([conn tx-data tx-meta]
|
([conn tx-data tx-meta]
|
||||||
(let [tx-data (common-util/fast-remove-nils tx-data)]
|
(let [tx-data (common-util/fast-remove-nils tx-data)
|
||||||
|
request-finished? (request-finished?)]
|
||||||
;; Ensure worker can handle the request sequentially (one by one)
|
;; Ensure worker can handle the request sequentially (one by one)
|
||||||
;; Because UI assumes that the in-memory db has all the data except the last one transaction
|
;; Because UI assumes that the in-memory db has all the data except the last one transaction
|
||||||
(when (and (seq tx-data) (request-finished?))
|
(when (seq tx-data)
|
||||||
|
|
||||||
;; (prn :debug :transact)
|
;; (prn :debug :transact)
|
||||||
;; (cljs.pprint/pprint tx-data)
|
;; (cljs.pprint/pprint tx-data)
|
||||||
|
|
||||||
(let [f (or @*transact-fn d/transact!)
|
(let [f (or @*transact-fn d/transact!)
|
||||||
sync? (= f d/transact!)
|
sync? (= f d/transact!)
|
||||||
request-id (swap! *request-id inc)
|
request-id (when-not sync? (swap! *request-id inc))
|
||||||
tx-meta' (if sync? tx-meta
|
tx-meta' (cond-> tx-meta
|
||||||
(assoc tx-meta :request-id request-id))
|
(not sync?)
|
||||||
result (f conn tx-data tx-meta')]
|
(assoc :request-id request-id))]
|
||||||
(if sync? result
|
(if sync?
|
||||||
|
(f conn tx-data tx-meta')
|
||||||
(let [resp (p/deferred)]
|
(let [resp (p/deferred)]
|
||||||
(swap! *request-id->response assoc request-id resp)
|
(when request-finished?
|
||||||
|
(f conn tx-data tx-meta'))
|
||||||
|
(let [value (if request-finished?
|
||||||
|
{:response resp}
|
||||||
|
{:response resp
|
||||||
|
:callback #(f conn tx-data tx-meta')})]
|
||||||
|
(swap! *request-id->response assoc request-id value))
|
||||||
resp)))))))
|
resp)))))))
|
||||||
|
|
||||||
(defn build-default-pages-tx
|
(defn build-default-pages-tx
|
||||||
|
|
|
@ -124,10 +124,9 @@ Options available:
|
||||||
new?
|
new?
|
||||||
;; TODO: use file system timestamp?
|
;; TODO: use file system timestamp?
|
||||||
(assoc :file/created-at (common-util/time-ms)))])
|
(assoc :file/created-at (common-util/time-ms)))])
|
||||||
tx' (common-util/fast-remove-nils tx)
|
|
||||||
result (if skip-db-transact?
|
result (if skip-db-transact?
|
||||||
tx'
|
tx
|
||||||
(ldb/transact! conn tx' (select-keys options [:new-graph? :from-disk?])))]
|
(ldb/transact! conn tx (select-keys options [:new-graph? :from-disk?])))]
|
||||||
{:tx result
|
{:tx result
|
||||||
:ast ast})))
|
:ast ast})))
|
||||||
|
|
||||||
|
|
|
@ -2828,7 +2828,7 @@
|
||||||
(save-current-block!)
|
(save-current-block!)
|
||||||
(remove-block-own-order-list-type! block))
|
(remove-block-own-order-list-type! block))
|
||||||
(p/let [*edit-block-fn (atom nil)
|
(p/let [*edit-block-fn (atom nil)
|
||||||
_ (delete-block! repo false :*edit-block-fn *edit-block-fn)]
|
result (delete-block! repo false :*edit-block-fn *edit-block-fn)]
|
||||||
(when-let [f @*edit-block-fn]
|
(when-let [f @*edit-block-fn]
|
||||||
(f))))))
|
(f))))))
|
||||||
|
|
||||||
|
|
|
@ -952,13 +952,12 @@
|
||||||
|
|
||||||
;; db-worker -> UI
|
;; db-worker -> UI
|
||||||
(defmethod handle :db/sync-changes [[_ {:keys [request-id] :as data}]]
|
(defmethod handle :db/sync-changes [[_ {:keys [request-id] :as data}]]
|
||||||
(when request-id ; request-id could be nil sometimes
|
|
||||||
(let [repo (state/get-current-repo)]
|
(let [repo (state/get-current-repo)]
|
||||||
(pipeline/invoke-hooks data)
|
(pipeline/invoke-hooks data)
|
||||||
|
|
||||||
(ipc/ipc :db-transact repo (pr-str (:tx-data data)) (pr-str (:tx-meta data)))
|
(ipc/ipc :db-transact repo (pr-str (:tx-data data)) (pr-str (:tx-meta data)))
|
||||||
(state/pub-event! [:search/transact-data repo (:search-indice data)])
|
(state/pub-event! [:search/transact-data repo (:search-indice data)])
|
||||||
nil)))
|
nil))
|
||||||
|
|
||||||
(defn run!
|
(defn run!
|
||||||
[]
|
[]
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
|
|
||||||
(defn invoke-hooks
|
(defn invoke-hooks
|
||||||
[{:keys [request-id tx-meta tx-data deleted-block-uuids affected-keys blocks] :as opts}]
|
[{:keys [request-id tx-meta tx-data deleted-block-uuids affected-keys blocks] :as opts}]
|
||||||
(when request-id
|
;; (prn :debug :request-id request-id)
|
||||||
(let [{:keys [from-disk? new-graph? local-tx? undo? redo?]} tx-meta
|
(let [{:keys [from-disk? new-graph? local-tx? undo? redo?]} tx-meta
|
||||||
repo (state/get-current-repo)
|
repo (state/get-current-repo)
|
||||||
tx-report {:tx-meta tx-meta
|
tx-report {:tx-meta tx-meta
|
||||||
|
@ -94,6 +94,7 @@
|
||||||
(true? (:added datom)))) tx-data) ; editing-block was added back (could be undo or from remote sync)
|
(true? (:added datom)))) tx-data) ; editing-block was added back (could be undo or from remote sync)
|
||||||
(state/set-state! :ui/deleting-block nil)))
|
(state/set-state! :ui/deleting-block nil)))
|
||||||
|
|
||||||
|
(when request-id
|
||||||
(when-let [deferred (ldb/get-deferred-response request-id)]
|
(when-let [deferred (ldb/get-deferred-response request-id)]
|
||||||
(p/resolve! deferred {:tx-meta tx-meta
|
(p/resolve! deferred {:tx-meta tx-meta
|
||||||
:tx-data tx-data})
|
:tx-data tx-data})
|
||||||
|
|
Loading…
Reference in New Issue