Skip to content

Commit bcad782

Browse files
committed
fix(rtc): lost first block's rtc-ops when paste multiple blocks (2)
- add more tests - consider more cases
1 parent 33db791 commit bcad782

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

src/main/frontend/worker/rtc/gen_client_op.cljs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@
77
[logseq.db.frontend.property :as db-property]))
88

99
(defn remove-conflict-same-block-datoms
10-
"remove conflict entity-datoms for same-block(same block/uuid) in same-entity-datoms-coll.
11-
merge
12-
[[[182 :block/uuid block-uuid1 1 false], ...]
13-
[[183 :block/uuid block-uuid1 1 true], ...]]
14-
into
15-
[[[183 :block/uuid block-uuid1 1 true], ...]]
16-
"
10+
"remove conflict entity-datoms for same-block(same block/uuid) in same-entity-datoms-coll."
1711
[same-entity-datoms-coll]
1812
(let [entity-info (map (fn [datoms]
1913
(let [first-datom (first datoms)
2014
e (nth first-datom 0)
2115
t (nth first-datom 3)
22-
uuid (some (fn [d]
16+
uuid-datom (some (fn [d]
2317
(when (keyword-identical? :block/uuid (nth d 1))
24-
(nth d 2)))
25-
datoms)]
26-
{:e e :t t :uuid uuid :datoms datoms}))
18+
d))
19+
datoms)
20+
uuid (when uuid-datom (nth uuid-datom 2))
21+
added? (when uuid-datom (nth uuid-datom 4))]
22+
{:e e :t t :uuid uuid :added? added? :datoms datoms}))
2723
same-entity-datoms-coll)
2824
uuid-groups (group-by :uuid (filter :uuid entity-info))
2925
loser-eids (reduce
@@ -32,7 +28,9 @@
3228
(reduce
3329
(fn [acc* [_t infos*]]
3430
(if (> (count infos*) 1)
35-
(let [sorted-infos (sort-by :e > infos*)
31+
(let [sorted-infos (sort-by (fn [x] [(if (:added? x) 1 0) (:e x)])
32+
(fn [a b] (compare b a))
33+
infos*)
3634
losers (rest sorted-infos)]
3735
(into acc* (map :e losers)))
3836
acc*))

src/test/frontend/worker/rtc/gen_client_op_test.cljs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,13 @@
228228
result (subject/remove-conflict-same-block-datoms same-entity-datoms-coll)]
229229
(is (= 2 (count result)))
230230
(is (= datoms-1 (first result)))
231-
(is (= datoms-3 (second result))))))
231+
(is (= datoms-3 (second result)))))
232+
233+
(testing "remove conflict entity-datoms should prefer add over retract"
234+
(let [block-uuid1 #uuid "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
235+
datoms-1 [[100 :block/uuid block-uuid1 1 true]]
236+
datoms-2 [[101 :block/uuid block-uuid1 1 false]]
237+
same-entity-datoms-coll [datoms-1 datoms-2]
238+
result (subject/remove-conflict-same-block-datoms same-entity-datoms-coll)]
239+
(is (= 1 (count result)))
240+
(is (= datoms-1 (first result))))))

0 commit comments

Comments
 (0)