Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow referencing existing objects when part of the conflict set #531

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mpareja
Copy link

@mpareja mpareja commented Feb 10, 2023

Issue automerge/automerge#526 demonstrated that it was not possible to resolve a conflict by choosing one of the conflicting objects or arrays. The following change allows setting a nested object to be an existing object reference so long as the referenced object id is currently listed as one of the conflicts for the key.

@@ -229,7 +229,14 @@ class Context {
*/
createNestedObjects(obj, key, value, insert, pred, elemId) {
if (value[OBJECT_ID]) {
throw new RangeError('Cannot create a reference to an existing document object')
const object = this.updated[obj] || this.cache[obj]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This falls down when the array has nested objects.

A makeList operation is added and createNestedObjects is called recursively. The updated and cache maps haven't yet learned about the newly (?) added node.

it('should allow referencing an existing list of objects that is part of the conflict set', () => {
  s2 = Automerge.clone(s1)
  s1 = Automerge.change(s1, doc => doc.list = [{ field: 'a' }])
  s2 = Automerge.change(s2, doc => doc.list = [{ field: 'b' }])

  const result = Automerge.merge(s1, s2)
  const conflicts = Automerge.getConflicts(result, 'list')

  // Should not throw "Cannot create a reference to an existing document object"
  Automerge.change(result, doc => {
    doc.list = Object.values(conflicts)[0]
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant