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

Handle failed object merge #69

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 13 additions & 5 deletions src/simperium/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,19 @@ internal.updateObjectVersion = function( id, version, data, original, patch, ack

// apply the transformed patch and emit the update
if ( transformed ) {
patch = transformed;
update = change_util.apply( transformed, data );
// queue up the new change
change = change_util.modify( id, version, patch );
this.localQueue.queue( change );
try {
patch = transformed;
update = change_util.apply( transformed, data );
// queue up the new change
change = change_util.modify( id, version, patch );
this.localQueue.queue( change );
} catch (error) {
// Rebase failed. Fallback to using the local client's content and sync
// using the latest ghost.
const localObject = change_util.apply( localModifications, original );
const fallbackChange = change_util.buildChange( change_util.type.MODIFY, id, localObject, data);
this.localQueue.queue( fallbackChange );
}
}

notify = this.emit.bind( this, 'update', id, update, original, patch, this.isIndexing );
Expand Down
16 changes: 14 additions & 2 deletions src/simperium/jsondiff/jsondiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,18 @@ const __bind = function(fn, me){ return function(){ return fn.apply(me, argument
a_patches = jsondiff.dmp.patch_make(sk, jsondiff.dmp.diff_fromDelta(sk, aop['v']));
b_patches = jsondiff.dmp.patch_make(sk, jsondiff.dmp.diff_fromDelta(sk, bop['v']));
b_text = (jsondiff.dmp.patch_apply(b_patches, sk))[0];
ab_text = (jsondiff.dmp.patch_apply(a_patches, b_text))[0];

const appliedPatch = jsondiff.dmp.patch_apply(a_patches, b_text);
const patchResults = appliedPatch[1];
// If we encounter bad merges, return null
for (let i=0; i < patchResults.length; i++) {
const result = patchResults[i];
if (result === false) {
return null;
}
}

ab_text = appliedPatch[0];
if (ab_text !== b_text) {
dmp_diffs = jsondiff.dmp.diff_main(b_text, ab_text);
if (dmp_diffs.length > 2) {
Expand All @@ -489,8 +500,9 @@ const __bind = function(fn, me){ return function(){ return fn.apply(me, argument
}
}
}
return ad_new;
}

return ad_new;
};

jsondiff.prototype.patch_apply_with_offsets = function(patches, text, offsets) {};
Expand Down