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

Add preventInvertInRollback option #443

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
5 changes: 4 additions & 1 deletion lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ function Doc(connection, collection, id) {
// specifc op and toggled off afterward
this.preventCompose = false;

// Disable the default behavior of inverting ops in rollback
this.preventInvertInRollback = false;

// If set to true, the source will be submitted over the connection. This
// will also have the side-effect of only composing ops whose sources are
// equal
Expand Down Expand Up @@ -947,7 +950,7 @@ Doc.prototype._rollback = function(err) {
// working state, then call back
var op = this.inflightOp;

if ('op' in op && op.type.invert) {
if ('op' in op && !this.preventInvertInRollback && op.type.invert) {
op.op = op.type.invert(op.op);

// Transform the undo operation by any pending ops.
Expand Down
69 changes: 69 additions & 0 deletions test/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,5 +474,74 @@ describe('Doc', function() {
}
], done);
});

describe('with an op collision', function() {
function testScenario(preventInvertInRollback, done) {
var doc1 = this.backend.connect().get('dogs', 'snoopy');
var doc2 = this.backend.connect().get('dogs', 'snoopy');
doc2.preventInvertInRollback = preventInvertInRollback;

var pauseSubmit = false;
var fireSubmit;
var invertHaveBeenCalled = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't help but notice a grammar issue. Suggest to simplify this name.

Suggested change
var invertHaveBeenCalled = false;
var invertCalled = false;


this.backend.use('submit', function(request, callback) {
if (pauseSubmit) {
fireSubmit = function() {
pauseSubmit = false;
callback();
};
} else {
fireSubmit = null;
callback();
}
});
async.series([
doc1.create.bind(doc1, {colours: ['white']}),
doc1.whenNothingPending.bind(doc1),
doc2.fetch.bind(doc2),
doc2.whenNothingPending.bind(doc2),
function(next) {
// Wrap invert to test that it has been called
var originalInvert = doc2.type.invert;
doc2.type.invert = function(op) {
invertHaveBeenCalled = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
invertHaveBeenCalled = true;
invertCalled = true;

return originalInvert(op);
};
next();
},
doc1.submitOp.bind(doc1, {p: ['colours'], oi: 'white,black'}),
function(next) {
pauseSubmit = true;
doc2.submitOp({p: ['colours', '0'], li: 'black'}, function(error) {
expect(error.message).to.equal('Referenced element not a list');
next();
});

doc2.fetch(function(error) {
if (error) return next(error);
fireSubmit();
});
},
function(next) {
expect(invertHaveBeenCalled).to.eql(!preventInvertInRollback);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(invertHaveBeenCalled).to.eql(!preventInvertInRollback);
expect(invertCalled).to.eql(!preventInvertInRollback);

expect(doc2.data).to.eql(doc1.data);
next();
}
], done);
}

it('calls doc type invert by default', function(done) {
var preventInvertInRollback = false;
var testScenarioBound = testScenario.bind(this);
testScenarioBound(preventInvertInRollback, done);
});

it('does not call doc type invert if preventInvertInRollback is `true`', function(done) {
var preventInvertInRollback = true;
var testScenarioBound = testScenario.bind(this);
testScenarioBound(preventInvertInRollback, done);
});
});
});
});