|
|
@@ -6,7 +6,8 @@
|
|
|
[frontend.worker.search :as search]
|
|
|
[frontend.worker.state :as worker-state]
|
|
|
[frontend.worker.util :as worker-util]
|
|
|
- [promesa.core :as p]))
|
|
|
+ [promesa.core :as p]
|
|
|
+ [frontend.worker.batch-tx :as batch-tx]))
|
|
|
|
|
|
|
|
|
(defn- entity-datoms=>attr->datom
|
|
|
@@ -35,28 +36,27 @@
|
|
|
|
|
|
(defmethod listen-db-changes :sync-db-to-main-thread
|
|
|
[_ {:keys [tx-meta repo conn] :as tx-report}]
|
|
|
- (let [{:keys [pipeline-replace? from-disk?]} tx-meta]
|
|
|
- (when-not pipeline-replace?
|
|
|
- (let [result (worker-pipeline/invoke-hooks repo conn tx-report (worker-state/get-context))
|
|
|
- tx-report' (:tx-report result)]
|
|
|
- (when result
|
|
|
- (let [data (merge
|
|
|
- {:request-id (:request-id tx-meta)
|
|
|
- :repo repo
|
|
|
- :tx-data (:tx-data tx-report')
|
|
|
- :tx-meta tx-meta}
|
|
|
- (dissoc result :tx-report))]
|
|
|
- (worker-util/post-message :sync-db-changes data))
|
|
|
+ (let [{:keys [from-disk?]} tx-meta
|
|
|
+ result (worker-pipeline/invoke-hooks repo conn tx-report (worker-state/get-context))
|
|
|
+ tx-report' (:tx-report result)]
|
|
|
+ (when result
|
|
|
+ (let [data (merge
|
|
|
+ {:request-id (:request-id tx-meta)
|
|
|
+ :repo repo
|
|
|
+ :tx-data (:tx-data tx-report')
|
|
|
+ :tx-meta tx-meta}
|
|
|
+ (dissoc result :tx-report))]
|
|
|
+ (worker-util/post-message :sync-db-changes data))
|
|
|
|
|
|
- (when-not from-disk?
|
|
|
- (p/do!
|
|
|
- (let [{:keys [blocks-to-remove-set blocks-to-add]} (search/sync-search-indice repo tx-report')
|
|
|
- ^js wo (worker-state/get-worker-object)]
|
|
|
- (when wo
|
|
|
- (when (seq blocks-to-remove-set)
|
|
|
- (.search-delete-blocks wo repo (bean/->js blocks-to-remove-set)))
|
|
|
- (when (seq blocks-to-add)
|
|
|
- (.search-upsert-blocks wo repo (bean/->js blocks-to-add))))))))))))
|
|
|
+ (when-not from-disk?
|
|
|
+ (p/do!
|
|
|
+ (let [{:keys [blocks-to-remove-set blocks-to-add]} (search/sync-search-indice repo tx-report')
|
|
|
+ ^js wo (worker-state/get-worker-object)]
|
|
|
+ (when wo
|
|
|
+ (when (seq blocks-to-remove-set)
|
|
|
+ (.search-delete-blocks wo repo (bean/->js blocks-to-remove-set)))
|
|
|
+ (when (seq blocks-to-add)
|
|
|
+ (.search-upsert-blocks wo repo (bean/->js blocks-to-add))))))))))
|
|
|
|
|
|
|
|
|
(defn listen-db-changes!
|
|
|
@@ -67,16 +67,31 @@
|
|
|
(prn :listen-db-changes! (keys handlers))
|
|
|
(d/unlisten! conn ::listen-db-changes!)
|
|
|
(d/listen! conn ::listen-db-changes!
|
|
|
- (fn [{:keys [tx-data] :as args}]
|
|
|
- (let [datom-vec-coll (map vec tx-data)
|
|
|
- id->same-entity-datoms (group-by first datom-vec-coll)
|
|
|
- id-order (distinct (map first datom-vec-coll))
|
|
|
- same-entity-datoms-coll (map id->same-entity-datoms id-order)
|
|
|
- id->attr->datom (update-vals id->same-entity-datoms entity-datoms=>attr->datom)
|
|
|
- args* (assoc args
|
|
|
- :repo repo
|
|
|
- :conn conn
|
|
|
- :id->attr->datom id->attr->datom
|
|
|
- :same-entity-datoms-coll same-entity-datoms-coll)]
|
|
|
- (doseq [[k handler-fn] handlers]
|
|
|
- (handler-fn k args*)))))))
|
|
|
+ (fn [{:keys [tx-data db-before db-after tx-meta] :as tx-report}]
|
|
|
+ (let [pipeline-replace? (:pipeline-replace? tx-meta)
|
|
|
+ batch-processing? (> (:editor/counter (d/entity db-after :logseq.kv/tx-batch-counter)) 0)]
|
|
|
+ (when-not pipeline-replace?
|
|
|
+ (if batch-processing?
|
|
|
+ (batch-tx/conj-batch-txs! tx-data)
|
|
|
+ (let [exiting-batch-mode? (> (:editor/counter (d/entity db-before :logseq.kv/tx-batch-counter)) 0)
|
|
|
+ db-before (if exiting-batch-mode?
|
|
|
+ (batch-tx/get-batch-db-before)
|
|
|
+ (:db-before tx-report))
|
|
|
+ tx-data (if exiting-batch-mode?
|
|
|
+ (batch-tx/get-batch-txs)
|
|
|
+ (concat (:tx-data tx-report) tx-data))
|
|
|
+ tx-report (assoc tx-report
|
|
|
+ :db-before db-before
|
|
|
+ :tx-data tx-data)
|
|
|
+ datom-vec-coll (map vec tx-data)
|
|
|
+ id->same-entity-datoms (group-by first datom-vec-coll)
|
|
|
+ id-order (distinct (map first datom-vec-coll))
|
|
|
+ same-entity-datoms-coll (map id->same-entity-datoms id-order)
|
|
|
+ id->attr->datom (update-vals id->same-entity-datoms entity-datoms=>attr->datom)
|
|
|
+ args* (assoc tx-report
|
|
|
+ :repo repo
|
|
|
+ :conn conn
|
|
|
+ :id->attr->datom id->attr->datom
|
|
|
+ :same-entity-datoms-coll same-entity-datoms-coll)]
|
|
|
+ (doseq [[k handler-fn] handlers]
|
|
|
+ (handler-fn k args*))))))))))
|