Skip to content

Commit

Permalink
Fix bug arising from multiple non-commutative sequential edits
Browse files Browse the repository at this point in the history
editscript assumes sequential application of their edits
  • Loading branch information
bobby committed Feb 23, 2021
1 parent 2baf7c5 commit 56fa635
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
10 changes: 6 additions & 4 deletions src/converge/edn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
(vary-meta assoc :converge/id entity))))))))

(defn edn
[opset]
(if (= (count opset) 0)
nil
(assemble-values (interpret/interpret opset))))
([opset]
(if (= (count opset) 0)
nil
(assemble-values (interpret/interpret opset))))
([interpretation ops]
(assemble-values (interpret/interpret interpretation ops))))
14 changes: 9 additions & 5 deletions src/converge/interpret.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@
[agg id op]
(assoc-in agg [:elements id] (-> op :data :value)))

(defn interpret
[opset]
(reduce-kv -interpret-op
(->Interpretation {} {})
opset))
(defn interpret
([opset]
(reduce-kv -interpret-op
(->Interpretation {} {})
opset))
([interpretation ops]
(reduce-kv -interpret-op
interpretation
ops)))
24 changes: 13 additions & 11 deletions src/converge/patch.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,21 @@

(defn make-patch
[opset actor old-value new-value]
(let [ops (some->> new-value
(let [ ;; PERF: pass this in from cache?
interpretation
(interpret/interpret opset)

ops (some->> new-value
(editscript/diff old-value)
edit/get-edits
(reduce (fn [{:keys [ops id] :as agg} edit]
(let [new-ops (into ops
(edit-to-ops edit
old-value
actor
id))]
(reduce (fn [{:keys [interp value id ops] :as agg} edit]
(let [new-ops (into ops (edit-to-ops edit value actor id))]
(assoc agg
:ops new-ops
:id (opset/next-id new-ops actor))))
{:ops (avl/sorted-map)
:id (opset/next-id opset actor)})
:value (edn/edn interpretation new-ops)
:id (opset/next-id new-ops actor)
:ops new-ops)))
{:value old-value
:id (opset/next-id opset actor)
:ops (avl/sorted-map)})
:ops)]
(if (seq ops) (->Patch ops))))
31 changes: 0 additions & 31 deletions test/converge/opset_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,3 @@
(opset/make-id c 0) :foo
(opset/make-id c 1) :foo)]
(is (= (opset/make-id c 3) (opset/next-id opset c)))))))

(comment

(def a {:foo [:bar [1 2 3] {:baz [1 2 3 4] :remove :me}]
:bar {:a :b :c :d}
:baz {}
:quux [2]})

(def b {:foo [:bar :doh {:baz [1 3 5] :la {:foo [1 2 3]}}]
:bar [1 2 3]
:baz :quux
:quux [1 2]})

(def r (converge.api/ref a))
@r
(reset! r b)

(def a [{} [] :key {:nested {:key [1 2 3]}} [:foo "bar" 0 {:nested :inalist}] #{1 4 3 2 5}])
(def b [{} [] :key {:nested {:key [1 2 3]}} [:foo "bar" {:nested :inalist}] #{1 4 3 2 5}])

(require '[editscript.core :as editscript]
'[editscript.edit :as edit])

(def es
(some->> b
(editscript/diff a)
edit/get-edits))

(clojure.pprint/pprint es)

)

0 comments on commit 56fa635

Please sign in to comment.