Selaa lähdekoodia

remove unused code

Tienson Qin 4 vuotta sitten
vanhempi
sitoutus
7a095571f6

+ 24 - 50
src/main/frontend/components/block.cljs

@@ -2865,17 +2865,6 @@
 
 ;; TODO: virtual tree for better UX and memory usage reduce
 
-(defn- get-segment
-  [flat-blocks idx]
-  (let [new-idx (if (< idx block-handler/initial-blocks-length)
-                  block-handler/initial-blocks-length
-                  (+ idx block-handler/step-loading-blocks))
-        max-idx (count flat-blocks)
-        idx (min max-idx new-idx)
-        blocks (util/safe-subvec flat-blocks 0 idx)]
-    [blocks
-     idx]))
-
 (defn- load-more-blocks!
   [config flat-blocks]
   (when-let [db-id (:db/id config)]
@@ -2884,47 +2873,32 @@
 
 (rum/defcs lazy-blocks < rum/reactive
   {:init (fn [state]
-           (assoc state ::id (str (random-uuid))))
-   :did-remount (fn [_old-state new-state]
-                  ;; Loading more when pressing Enter, expand blocks or paste
-                  (let [[config flat-blocks _] (:rum/args new-state)]
-                    (when-not (:db/id config)
-                      (let [*last-idx (::last-idx new-state)
-                           new-idx (if (zero? *last-idx)
-                                     1
-                                     (inc @*last-idx))]
-                       (reset! *last-idx new-idx))))
-                  new-state)}
-  (rum/local 0 ::last-idx)
+           (assoc state ::id (str (random-uuid))))}
   [state config flat-blocks blocks->vec-tree]
-  (let [*last-idx (::last-idx state)
-        db-id (:db/id config)
-        [blocks idx] (if db-id
-                       [flat-blocks 0]
-                       (get-segment flat-blocks @*last-idx))
-        blocks (blocks->vec-tree blocks)
-        last-block-id (:db/id (last flat-blocks))
-        bottom-reached (fn []
-                         (if db-id
-                           (load-more-blocks! config flat-blocks)
-                           (reset! *last-idx idx)))
-        has-more? (if db-id
-                    (and (not= last-block-id (model/get-block-last-child db-id))
-                         (not= last-block-id db-id))
-                    (>= (count flat-blocks) (inc idx)))
-        dom-id (str "lazy-blocks-" (::id state))]
-    [:div {:id dom-id}
-     (ui/infinite-list
-      "main-content-container"
+  (let [db-id (:db/id config)
+        blocks (blocks->vec-tree flat-blocks)]
+    (if (:custom-query? config)
       (block-list config blocks)
-      {:on-load bottom-reached
-       :bottom-reached (fn []
-                         (when-let [node (gdom/getElement dom-id)]
-                           (ui/bottom-reached? node 1000)))
-       :has-more has-more?
-       :more (if (or (:preview? config) (:sidebar? config))
-               "More"
-               (ui/loading "Loading"))})]))
+      (let [last-block-id (:db/id (last flat-blocks))
+            bottom-reached (fn []
+                             (when db-id
+                               (load-more-blocks! config flat-blocks)))
+            has-more? (when db-id
+                        (and (not= last-block-id (model/get-block-last-child db-id))
+                             (not= last-block-id db-id)))
+            dom-id (str "lazy-blocks-" (::id state))]
+        [:div {:id dom-id}
+         (ui/infinite-list
+          "main-content-container"
+          (block-list config blocks)
+          {:on-load bottom-reached
+           :bottom-reached (fn []
+                             (when-let [node (gdom/getElement dom-id)]
+                               (ui/bottom-reached? node 1000)))
+           :has-more has-more?
+           :more (if (or (:preview? config) (:sidebar? config))
+                   "More"
+                   (ui/loading "Loading"))})]))))
 
 (rum/defcs blocks-container <
   {:init (fn [state]

+ 25 - 9
src/main/frontend/db/model.cljs

@@ -514,6 +514,16 @@
         left-id
         (or (get-block-last-child (:db/id left)) left-id)))))
 
+(defn recursive-child?
+  [repo child-id parent-id]
+  (loop [node (db-utils/entity repo child-id)]
+    (if node
+      (let [parent (:block/parent node)]
+        (if (= (:db/id parent) parent-id)
+          true
+          (recur parent)))
+      false)))
+
 (defn get-paginated-blocks
   "Get paginated blocks for a page or a specific block.
    `block?`: if true, returns its children only."
@@ -554,20 +564,26 @@
 
                                     (contains? #{:insert-node :insert-nodes :save-and-insert-node} outliner-op)
                                     (let [cached-ids (set (conj (map :db/id @result) page-id))
-                                          first-insert-id (some #(when (not (contains? cached-ids %)) %) tx-block-ids)
+                                          first-insert-id (some #(when (and (not (contains? cached-ids %))
+                                                                            (recursive-child? repo-url % block-id))
+                                                                   %) tx-block-ids)
                                           start-id (get-prev-open-block first-insert-id)
-                                          previous-blocks (take-while (fn [b] (not= start-id (:db/id b))) @result)
-                                          more (get-paginated-blocks-no-cache start-id {:limit 25
-                                                                                        :include-start? true
-                                                                                        :scoped-block-id scoped-block-id})]
-                                      (concat previous-blocks more))
+                                          start-page? (:block/name (db-utils/entity start-id))]
+                                      (when-not start-page?
+                                        (let [previous-blocks (take-while (fn [b] (not= start-id (:db/id b))) @result)
+                                              more (get-paginated-blocks-no-cache start-id {:limit 25
+                                                                                            :include-start? true
+                                                                                            :scoped-block-id scoped-block-id})]
+                                          (concat previous-blocks more))))
 
                                     ;; TODO: drag && drop, move blocks up/down
 
                                     :else
-                                    (get-paginated-blocks-no-cache block-id {:limit limit
-                                                                             :include-start? (not page?)
-                                                                             :scoped-block-id scoped-block-id}))
+                                    nil)
+                           blocks (or blocks
+                                      (get-paginated-blocks-no-cache block-id {:limit limit
+                                                                               :include-start? (not page?)
+                                                                               :scoped-block-id scoped-block-id}))
                            block-eids (map :db/id blocks)
                            blocks (if (seq tx-id->block)
                                     (map (fn [id]

+ 1 - 4
src/main/frontend/db/outliner.cljs

@@ -1,6 +1,5 @@
 (ns frontend.db.outliner
-  (:require [datascript.core :as d]
-            [clojure.set :as set]))
+  (:require [datascript.core :as d]))
 
 (defn get-by-id
   [conn id]
@@ -8,8 +7,6 @@
     (d/pull @conn '[*] id)
     (catch js/Error _e nil)))
 
-
-
 ;; key [:block/children parent-id]
 
 (def get-by-parent-id

+ 25 - 63
src/main/frontend/db/react.cljs

@@ -69,7 +69,7 @@
 
 (def ^:dynamic *query-component*)
 
-;; key -> components
+;; component -> query-key
 (defonce query-components (atom {}))
 
 (defn set-new-result!
@@ -117,27 +117,20 @@
 
 (defn remove-q!
   [k]
-  (swap! query-state dissoc k)
-  (state/delete-reactive-query-db! k))
+  (swap! query-state dissoc k))
 
 (defn add-query-component!
   [key component]
-  (swap! query-components update key
-         (fn [components]
-           (distinct (conj components component)))))
+  (when (and key component)
+    (swap! query-components assoc component key)))
 
 (defn remove-query-component!
   [component]
-  (reset!
-   query-components
-   (->> (for [[k components] @query-components
-              :let [new-components (remove #(= component %) components)]]
-          (if (empty? new-components) ; no subscribed components
-            (do (remove-q! k)
-                nil)
-            [k new-components]))
-        (keep identity)
-        (into {}))))
+  (when-let [query (get @query-components component)]
+    (let [matched-queries (filter #(= query %) (vals @query-components))]
+      (when (= 1 (count matched-queries))
+        (remove-q! query))))
+  (swap! query-components dissoc component))
 
 ;; TODO: rename :custom to :query/custom
 (defn remove-custom-query!
@@ -161,28 +154,6 @@
          (set! (.-state result-atom) result)
          (add-q! k nil nil result-atom identity identity identity))))))
 
-(defn- new-db
-  [cached-result tx-data old-db k]
-  (when-not (= :custom (second k))
-    (try
-      (let [empty-db (d/empty-db db-schema/schema)
-            db (or old-db
-                   (when (and (sequential? cached-result)
-                              (or (map? (first cached-result))
-                                  (empty? cached-result)))
-                     (let [cached-result (util/remove-nils cached-result)]
-                       (-> empty-db
-                           (d/with cached-result)
-                           (:db-after)))))]
-        (when db
-          (:db-after (d/with db tx-data))))
-      (catch js/Error e
-        (prn "New db: " {:k k
-                         :old-db old-db
-                         :cached-result cached-result})
-        (js/console.error e)
-        old-db))))
-
 (defn get-query-cached-result
   [k]
   (:result (get @query-state k)))
@@ -222,10 +193,7 @@
             (set! (.-state result-atom) result)
             (if disable-reactive?
               result-atom
-              (do
-                (let [db' (new-db result nil nil k)]
-                  (state/set-reactive-query-db! k db'))
-                (add-q! k query inputs result-atom transform-fn query-fn inputs-fn)))))))))
+              (add-q! k query inputs result-atom transform-fn query-fn inputs-fn))))))))
 
 
 ;; TODO: Extract several parts to handlers
@@ -339,27 +307,21 @@
         (let [custom? (= :custom (second k))
               kv? (= :kv (second k))]
           (when (and
-                (= (first k) repo-url)
-                (or (get affected-keys (vec (rest k)))
-                    custom?
-                    kv?))
-           (let [{:keys [query query-fn result]} cache]
-             (when (or query query-fn)
-               (try
-                 (let [db' (when (and (vector? k) (not= (second k) :kv))
-                             (let [query-db (state/get-reactive-query-db k)
-                                   result (new-db @result tx-data query-db k)]
-                               (state/set-reactive-query-db! k result)
-                               result))
-                       db (or db' db)
-                       f #(execute-query! repo-url db k tx cache)]
-                   (if (and custom?
-                            ;; modifying during cards review need to be executed immediately
-                            (not (:cards-query? (meta query))))
-                     (async/put! (state/get-reactive-custom-queries-chan) [f query])
-                     (f)))
-                 (catch js/Error e
-                   (js/console.error e)))))))))))
+                 (= (first k) repo-url)
+                 (or (get affected-keys (vec (rest k)))
+                     custom?
+                     kv?))
+            (let [{:keys [query query-fn result]} cache]
+              (when (or query query-fn)
+                (try
+                  (let [f #(execute-query! repo-url db k tx cache)]
+                    (if (and custom?
+                             ;; modifying during cards review need to be executed immediately
+                             (not (:cards-query? (meta query))))
+                      (async/put! (state/get-reactive-custom-queries-chan) [f query])
+                      (f)))
+                  (catch js/Error e
+                    (js/console.error e)))))))))))
 
 (defn set-key-value
   [repo-url key value]

+ 5 - 5
src/main/frontend/handler/block.cljs

@@ -3,7 +3,7 @@
             [clojure.walk :as walk]
             [frontend.db :as db]
             [frontend.db.model :as db-model]
-            [frontend.db.react :as db-react]
+            [frontend.db.react :as react]
             [frontend.state :as state]
             [frontend.format.block :as block]
             [frontend.util :as util]))
@@ -113,7 +113,7 @@
                  block?
                  (assoc :scoped-block-id db-id))
         more-data (db-model/get-paginated-blocks-no-cache start-id option)]
-    (db-react/swap-new-result! query-k
-                               (fn [result]
-                                 (->> (concat result more-data)
-                                      (util/distinct-by :db/id))))))
+    (react/swap-new-result! query-k
+                            (fn [result]
+                              (->> (concat result more-data)
+                                   (util/distinct-by :db/id))))))

+ 1 - 16
src/main/frontend/state.cljs

@@ -207,8 +207,7 @@
      :srs/mode?                             false
 
      :srs/cards-due-count                   nil
-
-     :reactive/query-dbs                    {}})))
+     })))
 
 ;; block uuid -> {content(String) -> ast}
 (def blocks-ast-cache (atom {}))
@@ -1628,20 +1627,6 @@
   [block-id]
   (sub [:ui/collapsed-blocks (get-current-repo) block-id]))
 
-(defn get-reactive-query-db
-  [ks]
-  (get-in @state [:reactive/query-dbs ks]))
-
-(defn delete-reactive-query-db!
-  [ks]
-  (update-state! :reactive/query-dbs (fn [dbs] (dissoc dbs ks))))
-
-(defn set-reactive-query-db!
-  [ks db-value]
-  (if db-value
-    (set-state! [:reactive/query-dbs ks] db-value)
-    (delete-reactive-query-db! ks)))
-
 (defn get-modal-id
   []
   (:modal/id @state))