Skip to content

Commit 296199b

Browse files
authored
Merge pull request #671 from benjismith/master
🐛 Fix inflight/pending callbacks execute in wrong order
2 parents 161d8b5 + 7aa4f32 commit 296199b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/client/doc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Doc.prototype._handleFetch = function(error, snapshot) {
295295
var callbacks = this.pendingFetch;
296296
this.pendingFetch = [];
297297
var callback = this.inflightFetch.shift();
298-
if (callback) callbacks.push(callback);
298+
if (callback) callbacks.unshift(callback);
299299
if (callbacks.length) {
300300
callback = function(error) {
301301
util.callEach(callbacks, error);

test/client/doc.js

+21
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ describe('Doc', function() {
101101
doc.fetch(finish);
102102
doc.fetch(finish);
103103
});
104+
it('callbacks called in correct order when fetching and applying ops in quick succession', function(done) {
105+
var connection = this.connection;
106+
var doc = connection.get('dogs', 'fido');
107+
doc.create({name: 'fido'});
108+
var order = '';
109+
doc.fetch(function() {
110+
order += 'A';
111+
});
112+
doc.submitOp([{p: ['snacks'], oi: true}]);
113+
doc.fetch(function() {
114+
order += 'B';
115+
});
116+
doc.submitOp([{p: ['color'], oi: 'gray'}]);
117+
doc.fetch(function() {
118+
order += 'C';
119+
});
120+
doc.whenNothingPending(function() {
121+
expect(order).to.eql('ABC');
122+
done();
123+
});
124+
});
104125
});
105126

106127
describe('when connection closed', function() {

0 commit comments

Comments
 (0)