Skip to content

Commit

Permalink
Merge pull request #671 from benjismith/master
Browse files Browse the repository at this point in the history
🐛 Fix inflight/pending callbacks execute in wrong order
  • Loading branch information
alecgibson authored Jul 30, 2024
2 parents 161d8b5 + 7aa4f32 commit 296199b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Doc.prototype._handleFetch = function(error, snapshot) {
var callbacks = this.pendingFetch;
this.pendingFetch = [];
var callback = this.inflightFetch.shift();
if (callback) callbacks.push(callback);
if (callback) callbacks.unshift(callback);
if (callbacks.length) {
callback = function(error) {
util.callEach(callbacks, error);
Expand Down
21 changes: 21 additions & 0 deletions test/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ describe('Doc', function() {
doc.fetch(finish);
doc.fetch(finish);
});
it('callbacks called in correct order when fetching and applying ops in quick succession', function(done) {
var connection = this.connection;
var doc = connection.get('dogs', 'fido');
doc.create({name: 'fido'});
var order = '';
doc.fetch(function() {
order += 'A';
});
doc.submitOp([{p: ['snacks'], oi: true}]);
doc.fetch(function() {
order += 'B';
});
doc.submitOp([{p: ['color'], oi: 'gray'}]);
doc.fetch(function() {
order += 'C';
});
doc.whenNothingPending(function() {
expect(order).to.eql('ABC');
done();
});
});
});

describe('when connection closed', function() {
Expand Down

0 comments on commit 296199b

Please sign in to comment.