Skip to content

Commit

Permalink
fix: reactive queries
Browse files Browse the repository at this point in the history
Each block should have its own :query-result atom
  • Loading branch information
tiensonqin authored and logseq-cldwalker committed Jun 20, 2023
1 parent 096447d commit 8ab497c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
28 changes: 13 additions & 15 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2734,9 +2734,7 @@
blocks-container-id (:blocks-container-id config)
config (update config :block merge block)
;; Each block might have multiple queries, but we store only the first query's result
config (if (nil? (:query-result config))
(assoc config :query-result (atom nil))
config)
config (assoc config :query-result (atom nil))
config (if ref? (block-handler/attach-order-list-state config block) config)
heading? (:heading properties)
*control-show? (get state ::control-show?)
Expand Down Expand Up @@ -2771,18 +2769,18 @@
(state/sub-block-selected? blocks-container-id uuid))]
[:div.ls-block
(cond->
{:id block-id
:data-refs data-refs
:data-refs-self data-refs-self
:data-collapsed (and collapsed? has-child?)
:class (str uuid
(when pre-block? " pre-block")
(when (and card? (not review-cards?)) " shadow-md")
(when selected? " selected noselect")
(when order-list? " is-order-list")
(when (string/blank? content) " is-blank"))
:blockid (str uuid)
:haschild (str (boolean has-child?))}
{:id block-id
:data-refs data-refs
:data-refs-self data-refs-self
:data-collapsed (and collapsed? has-child?)
:class (str uuid
(when pre-block? " pre-block")
(when (and card? (not review-cards?)) " shadow-md")
(when selected? " selected noselect")
(when order-list? " is-order-list")
(when (string/blank? content) " is-blank"))
:blockid (str uuid)
:haschild (str (boolean has-child?))}

level
(assoc :level level)
Expand Down
12 changes: 5 additions & 7 deletions src/main/frontend/components/query.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@
(when-not (or built-in? dsl-query?)
(when collapsed?
(editor-handler/collapse-block! current-block-uuid))))
state)}
(rum/local nil ::query-result)
{:init (fn [state] (assoc state :query-error (atom nil)))}
(assoc state :query-error (atom nil)))}
[state config {:keys [title builder query view collapsed? table-view?] :as q} *query-triggered?]
(let [*query-error (:query-error state)
built-in? (built-in-custom-query? title)
Expand Down Expand Up @@ -208,13 +206,13 @@
(if table?
[:a.flex.ml-1.fade-link {:title "Switch to list view"
:on-click (fn [] (editor-property/set-block-property! current-block-uuid
"query-table"
false))}
"query-table"
false))}
(ui/icon "list" {:style {:font-size 20}})]
[:a.flex.ml-1.fade-link {:title "Switch to table view"
:on-click (fn [] (editor-property/set-block-property! current-block-uuid
"query-table"
true))}
"query-table"
true))}
(ui/icon "table" {:style {:font-size 20}})]))

[:a.flex.ml-1.fade-link
Expand Down
43 changes: 21 additions & 22 deletions src/main/frontend/components/query/result.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,25 @@

(defn get-query-result
[state config *query-error *query-triggered? current-block-uuid q options]
(or (when-let [*result (:query-result config)] @*result)
(let [query-atom (trigger-custom-query! state *query-error *query-triggered?)
query-result (and query-atom (rum/react query-atom))
(let [query-atom (trigger-custom-query! state *query-error *query-triggered?)
query-result (and query-atom (rum/react query-atom))
;; exclude the current one, otherwise it'll loop forever
remove-blocks (if current-block-uuid [current-block-uuid] nil)
transformed-query-result (when query-result
(let [result (db/custom-query-result-transform query-result remove-blocks q)]
(if (and query-result (coll? result) (:block/uuid (first result)))
(cond-> result
(get q :remove-block-children? true)
tree/filter-top-level-blocks)
result)))
group-by-page? (get-group-by-page q options)
result (if (and group-by-page? (:block/uuid (first transformed-query-result)))
(let [result (db-utils/group-by-page transformed-query-result)]
(if (map? result)
(dissoc result nil)
result))
transformed-query-result)]
(when-let [query-result (:query-result config)]
(reset! query-result result))
(when query-atom
(util/safe-with-meta result (meta @query-atom))))))
remove-blocks (if current-block-uuid [current-block-uuid] nil)
transformed-query-result (when query-result
(let [result (db/custom-query-result-transform query-result remove-blocks q)]
(if (and query-result (coll? result) (:block/uuid (first result)))
(cond-> result
(get q :remove-block-children? true)
tree/filter-top-level-blocks)
result)))
group-by-page? (get-group-by-page q options)
result (if (and group-by-page? (:block/uuid (first transformed-query-result)))
(let [result (db-utils/group-by-page transformed-query-result)]
(if (map? result)
(dissoc result nil)
result))
transformed-query-result)]
(when-let [query-result (:query-result config)]
(reset! query-result result))
(when query-atom
(util/safe-with-meta result (meta @query-atom)))))

0 comments on commit 8ab497c

Please sign in to comment.