Browse Source

fix: circular dependency between outliner and frontend

by moving batch-tx into outliner dep. Outliner dep is
now fully independent and we are able to write unit tests
for all outliner namespaces unlike before
Gabriel Horner 1 year ago
parent
commit
b35f30e203

+ 1 - 1
.clj-kondo/config.edn

@@ -131,7 +131,7 @@
              frontend.worker.handler.page worker-page
              frontend.worker.handler.page.rename worker-page-rename
              frontend.worker.handler.file.util wfu
-             frontend.worker.batch-tx batch-tx
+             logseq.outliner.batch-tx batch-tx
              lambdaisland.glogi log
              logseq.common.config common-config
              logseq.common.graph common-graph

+ 1 - 0
deps/outliner/.carve/config.edn

@@ -2,6 +2,7 @@
  :api-namespaces [logseq.outliner.datascript-report
                   logseq.outliner.pipeline
                   logseq.outliner.cli
+                  logseq.outliner.batch-tx
                   logseq.outliner.core
                   logseq.outliner.db-pipeline
                   logseq.outliner.property

+ 69 - 0
deps/outliner/src/logseq/outliner/batch_tx.cljc

@@ -0,0 +1,69 @@
+(ns logseq.outliner.batch-tx
+  "Batch process multiple transactions.
+  When batch-processing, don't refresh ui."
+  #?(:cljs (:require-macros [logseq.outliner.batch-tx]))
+  #?(:cljs (:require [logseq.common.util :as common-util])))
+
+(defmacro with-batch-tx-mode
+  "1. start batch-tx mode
+  2. run body
+  3. exit batch-tx mode"
+  [conn {:keys [additional-tx] :as opts} & body]
+  `(if (some? (logseq.outliner.batch-tx/get-batch-db-before))
+     (do ~@body)
+     (try
+       (let [tx-meta# (assoc (dissoc ~opts :additional-tx :transact-opts)
+                             :batch-tx/batch-tx-mode? true)]
+         (logseq.outliner.batch-tx/set-batch-opts tx-meta#)
+         (logseq.outliner.batch-tx/set-batch-db-before! @~conn)
+         ~@body
+         (when (seq ~additional-tx)
+           (logseq.db/transact! ~conn ~additional-tx {}))
+         (datascript.core/transact! ~conn [] {:batch-tx/exit? true})
+         (logseq.outliner.batch-tx/exit-batch-txs-mode!))
+       (catch :default e#
+         (logseq.outliner.batch-tx/exit-batch-txs-mode!)
+         (throw e#)))))
+
+#?(:cljs
+   (do
+     (defonce ^:private state
+       (atom {;; store all tx-data when batch-processing
+              :batch/txs []
+              ;; store db before batch-tx
+              :batch/db-before nil
+              ;; Opts for with-batch-tx-mode
+              :batch/opts nil}))
+
+     (defn get-batch-txs
+       []
+       (->> (:batch/txs @state)
+            (sort-by :tx)
+            ;; We need all the values for :many properties
+            (common-util/distinct-by-last-wins (fn [[e a v _tx added]] [e a v added]))))
+
+     (defn ^:api set-batch-db-before!
+       [db]
+       (swap! state assoc :batch/db-before db))
+
+     (defn ^:api get-batch-db-before
+       []
+       (:batch/db-before @state))
+
+     (defn ^:api set-batch-opts
+       [opts]
+       (swap! state assoc :batch/opts opts))
+
+     (defn get-batch-opts
+       []
+       (:batch/opts @state))
+
+     (defn conj-batch-txs!
+       [tx-data]
+       (swap! state update :batch/txs (fn [data] ((fnil into []) data tx-data))))
+
+     (defn ^:api exit-batch-txs-mode!
+       []
+       (swap! state assoc :batch/txs [])
+       (swap! state assoc :batch/db-before nil)
+       (swap! state assoc :batch/opts nil))))

+ 1 - 1
deps/outliner/src/logseq/outliner/core.cljs

@@ -17,7 +17,7 @@
             [logseq.db.frontend.property.util :as db-property-util]
             [logseq.db.sqlite.util :as sqlite-util]
             [logseq.db.sqlite.create-graph :as sqlite-create-graph]
-            [frontend.worker.batch-tx :include-macros true :as batch-tx]
+            [logseq.outliner.batch-tx :include-macros true :as batch-tx]
             [logseq.db.frontend.order :as db-order]
             [logseq.outliner.pipeline :as outliner-pipeline]
             [logseq.db.frontend.class :as db-class]))

+ 1 - 1
deps/outliner/src/logseq/outliner/transaction.cljc

@@ -22,7 +22,7 @@
     (delete-blocks! ...))"
   [opts & body]
   `(let [opts# (dissoc ~opts :transact-opts :current-block)]
-     (frontend.worker.batch-tx/with-batch-tx-mode
+     (logseq.outliner.batch-tx/with-batch-tx-mode
       (:conn (:transact-opts ~opts))
       opts#
       ~@body)))

+ 0 - 23
src/main/frontend/worker/batch_tx.clj

@@ -1,23 +0,0 @@
-(ns frontend.worker.batch-tx
-  "Macro for batch-tx fns")
-
-(defmacro with-batch-tx-mode
-  "1. start batch-tx mode
-  2. run body
-  3. exit batch-tx mode"
-  [conn {:keys [additional-tx] :as opts} & body]
-  `(if (some? (frontend.worker.batch-tx/get-batch-db-before))
-     (do ~@body)
-     (try
-       (let [tx-meta# (assoc (dissoc ~opts :additional-tx :transact-opts)
-                             :batch-tx/batch-tx-mode? true)]
-         (frontend.worker.batch-tx/set-batch-opts tx-meta#)
-         (frontend.worker.batch-tx/set-batch-db-before! @~conn)
-         ~@body
-         (when (seq ~additional-tx)
-           (logseq.db/transact! ~conn ~additional-tx {}))
-         (datascript.core/transact! ~conn [] {:batch-tx/exit? true})
-         (frontend.worker.batch-tx/exit-batch-txs-mode!))
-       (catch :default e#
-         (frontend.worker.batch-tx/exit-batch-txs-mode!)
-         (throw e#)))))

+ 0 - 49
src/main/frontend/worker/batch_tx.cljs

@@ -1,49 +0,0 @@
-(ns frontend.worker.batch-tx
-  "Batch process multiple transactions.
-  When batch-processing, don't refresh ui."
-  (:require [frontend.worker.state :as worker-state]
-            [frontend.schema-register :include-macros true :as sr]
-            [logseq.common.util :as common-util]))
-
-
-(sr/defkeyword :batch/txs
-  "store all tx-data when batch-processing")
-
-(sr/defkeyword :batch/db-before
-  "store db before batch-tx.")
-
-(sr/defkeyword :batch/opts
-  "Opts for with-batch-tx-mode")
-
-(defn get-batch-txs
-  []
-  (->> (:batch/txs @worker-state/*state)
-       (sort-by :tx)
-       ;; We need all the values for :many properties
-       (common-util/distinct-by-last-wins (fn [[e a v _tx added]] [e a v added]))))
-
-(defn set-batch-db-before!
-  [db]
-  (swap! worker-state/*state assoc :batch/db-before db))
-
-(defn get-batch-db-before
-  []
-  (:batch/db-before @worker-state/*state))
-
-(defn set-batch-opts
-  [opts]
-  (swap! worker-state/*state assoc :batch/opts opts))
-
-(defn get-batch-opts
-  []
-  (:batch/opts @worker-state/*state))
-
-(defn conj-batch-txs!
-  [tx-data]
-  (swap! worker-state/*state update :batch/txs (fn [data] ((fnil into []) data tx-data))))
-
-(defn exit-batch-txs-mode!
-  []
-  (swap! worker-state/*state assoc :batch/txs [])
-  (swap! worker-state/*state assoc :batch/db-before nil)
-  (swap! worker-state/*state assoc :batch/opts nil))

+ 1 - 1
src/main/frontend/worker/db_listener.cljs

@@ -7,7 +7,7 @@
             [frontend.worker.state :as worker-state]
             [frontend.worker.util :as worker-util]
             [promesa.core :as p]
-            [frontend.worker.batch-tx :as batch-tx]
+            [logseq.outliner.batch-tx :as batch-tx]
             [frontend.schema-register :as sr]))
 
 

+ 1 - 1
src/main/frontend/worker/rtc/remote_update.cljs

@@ -5,7 +5,7 @@
             [clojure.string :as string]
             [datascript.core :as d]
             [frontend.schema-register :as sr]
-            [frontend.worker.batch-tx :as batch-tx]
+            [logseq.outliner.batch-tx :as batch-tx]
             [frontend.worker.handler.page :as worker-page]
             [frontend.worker.rtc.const :as rtc-const]
             [frontend.worker.rtc.op-mem-layer :as op-mem-layer]

+ 0 - 4
src/main/frontend/worker/state.cljs

@@ -19,10 +19,6 @@
                        :config {}
                        :git/current-repo nil
 
-                       :batch/txs []
-                       :batch/db-before nil
-                       :batch/opts nil
-
                        :rtc/downloading-graph? false
 
                        :undo/repo->page-block-uuid->undo-ops (atom {})

+ 1 - 1
src/main/frontend/worker/undo_redo.cljs

@@ -3,7 +3,7 @@
   (:require [clojure.set :as set]
             [datascript.core :as d]
             [frontend.schema-register :include-macros true :as sr]
-            [frontend.worker.batch-tx :include-macros true :as batch-tx]
+            [logseq.outliner.batch-tx :include-macros true :as batch-tx]
             [frontend.worker.db-listener :as db-listener]
             [frontend.worker.state :as worker-state]
             [logseq.common.config :as common-config]