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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion frontend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
  })
})

const conflicts = object && object[CONFLICTS][key]
const valueInConflicts = conflicts && Object.values(conflicts)
.some(v => v && v[OBJECT_ID] === value[OBJECT_ID])

if (!valueInConflicts) {
throw new RangeError('Cannot create a reference to an existing document object')
}
}
const objectId = this.nextOpId()

Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,20 @@ describe('Automerge', () => {
}, /Cannot create a reference to an existing document object/)
})

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

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

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

it('should handle deletion of properties within a map', () => {
s1 = Automerge.change(s1, 'set style', doc => {
doc.textStyle = {typeface: 'Optima', bold: false, fontSize: 12}
Expand Down Expand Up @@ -773,6 +787,20 @@ describe('Automerge', () => {
}, /Cannot create a reference to an existing document object/)
})

it('should allow referencing an existing list that is part of the conflict set', () => {
s2 = Automerge.clone(s1)
s1 = Automerge.change(s1, doc => doc.list = ['a'])
s2 = Automerge.change(s2, doc => doc.list = ['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]
})
})

it('concurrent edits insert in reverse actorid order if counters equal', () => {
s1 = Automerge.init('aaaa')
s2 = Automerge.init('bbbb')
Expand Down