Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return;
}
let docOld = undefined;
const isUpdate = map[id + ''] && map[id + ''].merge;
if (isUpdate) {
docOld = Automerge.load(map[id + ''].merge);
updated.push(id);
} else {
docOld = Automerge.init();
}
const docInt =
typeof changes === 'string' ? Automerge.load(changes) : undefined;
const docNew = docInt
? Automerge.merge(docOld, docInt)
: Automerge.applyChanges(docOld, changes);
if (Object.keys(Automerge.getMissingDeps(docNew)).length) {
unsuccessful.push(id);
return;
}
/* if (!history.length) {
return {
...snapshot,
actor: undefined,
id,
merge: Automerge.save(docNew)
};
}*/
const history = Automerge.getHistory(docNew);
const { change, snapshot } = history[history.length - 1];
return {
it(`2a. Alice's edit gets synced over to Bob's canvas`, () => {
const aliceChanges2a = Automerge.getChanges(alice1, alice2)
bob2a = Automerge.applyChanges(bob2, aliceChanges2a)
alice2a = alice2
assert.deepEqual(bob2a, {
_objectId: '00000000-0000-0000-0000-000000000000',
x0y0: 'r',
x0y1: 'w',
x1y0: 'w',
x1y1: 'w'
})
assert.deepEqual(bob2a._conflicts, {})
})
it(`3a. Bob's edit gets synced to Alice's canvas`, () => {
const bobChanges3a = Automerge.getChanges(bob2, bob3)
alice3a = Automerge.applyChanges(alice3, bobChanges3a)
bob3a = bob3
assert.deepEqual(alice3a, {
_objectId: '00000000-0000-0000-0000-000000000000',
x0y0: 'r',
x0y1: 'w',
x1y0: 'w',
x1y1: 'b'
})
assert.deepEqual(alice3a._conflicts, {})
})
it('6. Alice and Bob both go back online, and re-sync', () => {
const aliceChanges6 = Automerge.getChanges(alice3, alice5)
const bobChanges6 = Automerge.getChanges(bob3, bob5)
alice6 = Automerge.applyChanges(alice5, bobChanges6)
bob6 = Automerge.applyChanges(bob5, aliceChanges6)
assert.deepEqual(alice6, {
_objectId: '00000000-0000-0000-0000-000000000000',
x0y0: 'r',
x0y1: 'w',
x1y0: 'g',
x1y1: 'w'
})
assert.deepEqual(alice6._conflicts, {
x1y0: {
alice: 'g'
},
x1y1: {
alice: 'r'
}
})
it('6. Alice and Bob both go back online, and re-sync', () => {
const aliceChanges6 = Automerge.getChanges(alice3, alice5)
const bobChanges6 = Automerge.getChanges(bob3, bob5)
alice6 = Automerge.applyChanges(alice5, bobChanges6)
bob6 = Automerge.applyChanges(bob5, aliceChanges6)
assert.deepEqual(alice6, {
_objectId: '00000000-0000-0000-0000-000000000000',
x0y0: 'r',
x0y1: 'w',
x1y0: 'g',
x1y1: 'w'
})
assert.deepEqual(alice6._conflicts, {
x1y0: {
alice: 'g'
},
x1y1: {
alice: 'r'
}
})
assert.deepEqual(bob6, {
log('_applyChanges', docId)
if (changes.length > 0) {
const oldDoc = this.find(docId)
const filteredChanges = []
changes.forEach((change) => {
if (!this.appliedSeqs[change.actor]) {
this.appliedSeqs[change.actor] = {}
}
if (this.appliedSeqs[change.actor][change.seq]) {
log('_applyChanges.skipDuplicate', change.actor, change.seq)
} else {
filteredChanges.push(change)
this.appliedSeqs[change.actor][change.seq] = true
}
})
const newDoc = Automerge.applyChanges(oldDoc, filteredChanges)
this._setRemote(docId, newDoc)
}
}
_applyChanges (docId, changes) {
return this._setRemote(Automerge.applyChanges(this.find(docId), changes))
}
_applyChanges (docId, changes) {
return changes.length > 0
? this._setRemote(Automerge.applyChanges(this.find(docId), changes))
: this.find(docId)
}