|
1 | 1 | (ns frontend.worker.pipeline |
2 | 2 | "Pipeline work after transaction" |
3 | | - (:require [clojure.string :as string] |
| 3 | + (:require [clojure.set :as set] |
| 4 | + [clojure.string :as string] |
4 | 5 | [datascript.core :as d] |
5 | 6 | [frontend.worker-common.util :as worker-util] |
6 | 7 | [frontend.worker.commands :as commands] |
|
35 | 36 | (:undo? tx-meta) (:redo? tx-meta))))) |
36 | 37 |
|
37 | 38 | (defn- rebuild-block-refs |
38 | | - [repo {:keys [tx-meta db-after]} blocks] |
| 39 | + [repo {:keys [tx-meta db-after db-before]} blocks] |
39 | 40 | (when (or (and (:outliner-op tx-meta) (refs-need-recalculated? tx-meta)) |
40 | 41 | (:rtc-tx? tx-meta)) |
41 | | - (mapcat (fn [block] |
42 | | - (when (d/entity db-after (:db/id block)) |
43 | | - (let [date-formatter (worker-state/get-date-formatter repo) |
44 | | - refs (outliner-core/rebuild-block-refs repo db-after date-formatter block)] |
45 | | - ;; Always retract because if refs is empty then a delete action has occurred |
46 | | - (cond-> [[:db/retract (:db/id block) :block/refs]] |
47 | | - (seq refs) |
48 | | - (conj {:db/id (:db/id block) |
49 | | - :block/refs refs}))))) |
50 | | - blocks))) |
| 42 | + (let [db-based? (entity-plus/db-based-graph? db-after)] |
| 43 | + (mapcat (fn [block] |
| 44 | + (when (d/entity db-after (:db/id block)) |
| 45 | + (let [date-formatter (worker-state/get-date-formatter repo) |
| 46 | + refs (->> (outliner-core/rebuild-block-refs repo db-after date-formatter block) set)] |
| 47 | + (if db-based? |
| 48 | + (let [old-refs (->> (:block/refs (d/entity db-before (:db/id block))) |
| 49 | + (map :db/id) |
| 50 | + set) |
| 51 | + added-refs (when (and (seq refs) (not= refs old-refs)) |
| 52 | + (set/difference refs old-refs)) |
| 53 | + retracted-refs (when (and (seq old-refs) (not= refs old-refs)) |
| 54 | + (set/difference old-refs refs))] |
| 55 | + (concat |
| 56 | + (map (fn [id] |
| 57 | + [:db/retract (:db/id block) :block/refs id]) |
| 58 | + retracted-refs) |
| 59 | + (map (fn [id] |
| 60 | + [:db/add (:db/id block) :block/refs id]) |
| 61 | + added-refs))) |
| 62 | + ;; retract all refs for file graphs because we can't ensure `refs` are all db ids |
| 63 | + (cond-> [[:db/retract (:db/id block) :block/refs]] |
| 64 | + (seq refs) |
| 65 | + (conj {:db/id (:db/id block) |
| 66 | + :block/refs refs})))))) |
| 67 | + blocks)))) |
51 | 68 |
|
52 | 69 | (defn- insert-tag-templates |
53 | 70 | [repo tx-report] |
|
0 commit comments