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

🐛 Fix inflight/pending callbacks execute in wrong order #671

Merged
merged 2 commits into from
Jul 30, 2024
Merged
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
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
Loading