Skip to content

Commit

Permalink
In Doc._clearInflightOp, clear inflightOp before calling callbacks
Browse files Browse the repository at this point in the history
Fixes double-callback issue for chained op submissions where the second op is invalid:
#272
  • Loading branch information
ericyhwang committed Feb 28, 2019
1 parent 9c33e41 commit cb79636
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,12 @@ Doc.prototype._hardRollback = function(err) {
};

Doc.prototype._clearInflightOp = function(err) {
var called = callEach(this.inflightOp.callbacks, err);
var inflightOp = this.inflightOp;

this.inflightOp = null;

var called = callEach(inflightOp.callbacks, err);

this.flush();
this._emitNothingPending();

Expand Down
37 changes: 36 additions & 1 deletion test/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Backend = require('../../lib/backend');
var expect = require('expect.js');
var util = require('../util')

describe('client query subscribe', function() {
describe('Doc', function() {

beforeEach(function() {
this.backend = new Backend();
Expand Down Expand Up @@ -213,6 +213,41 @@ describe('client query subscribe', function() {

});

describe('submitting ops in callbacks', function() {
beforeEach(function () {
this.doc = this.connection.get('dogs', 'scooby');
});

it('succeeds with valid op', function(done) {
var doc = this.doc;
doc.create({ name: 'Scooby Doo' }, function(error) {
expect(error).to.not.be.ok();
// Build valid op that deletes a substring at index 0 of name.
var textOpComponents = [{ p: 0, d: 'Scooby '}];
var op = [{ p: ['name'], t: 'text0', o: textOpComponents }];
doc.submitOp(op, function(error) {
if (error) return done(error);
expect(doc.data).eql({ name: 'Doo' });
done();
});
});
});

it('fails with invalid op', function(done) {
var doc = this.doc;
doc.create({ name: 'Scooby Doo' }, function(error) {
expect(error).to.not.be.ok();
// Build op that tries to delete an invalid substring at index 0 of name.
var textOpComponents = [{ p: 0, d: 'invalid'}];
var op = [{ p: ['name'], t: 'text0', o: textOpComponents }];
doc.submitOp(op, function(error) {
expect(error).to.be.ok();
done();
});
});
});
});

describe('submitting an invalid op', function () {
var doc;
var invalidOp;
Expand Down

0 comments on commit cb79636

Please sign in to comment.