Skip to content

Commit

Permalink
improve(plugin): filter hooks of db block changes for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 authored and tiensonqin committed Jan 5, 2023
1 parent a80a64d commit 9aa626d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/main/frontend/handler/plugin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@
([pid effect]
(js/LSPluginCore.unregisterTheme (name pid) effect)))

(defn get-installed-hooks
[]
(:plugin/installed-hooks @state/state))

(defn plugin-hook-installed?
[pid hook]
(when-let [hooks (and pid (get-installed-hooks))]
(contains? (get hooks hook) (keyword pid))))

(defn db-block-hook-installed?
[uuid]
(when-let [hook (and uuid (str "hook:db:block_" (string/replace (str uuid) "-" "_")))]
(boolean (seq (get (get-installed-hooks) hook)))))

(def *fenced-code-providers (atom #{}))

(defn register-fenced-code-renderer
Expand Down Expand Up @@ -490,8 +504,10 @@

(defn hook-plugin-block-changes
[{:keys [blocks tx-data tx-meta]}]
(let [tx-data' (group-by first tx-data)]
(doseq [b blocks]
(let [tx-data' (group-by first tx-data)
blocks' (filter #(when-let [uuid (:block/uuid %)]
(db-block-hook-installed? uuid)) blocks)]
(doseq [b blocks']
(let [type (str "block:" (:block/uuid b))]
(hook-plugin-db type {:block b :tx-data (get tx-data' (:db/id b)) :tx-meta tx-meta})))))

Expand Down
4 changes: 1 addition & 3 deletions src/main/logseq/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@

(defn ^:export should-exec-plugin-hook
[pid hook]
(let [hooks (:plugin/installed-hooks @state/state)]
(or (nil? (seq hooks))
(contains? (get hooks hook) (keyword pid)))))
(plugin-handler/plugin-hook-installed? pid hook))

;; base
(defn ^:export get_state_from_store
Expand Down

0 comments on commit 9aa626d

Please sign in to comment.