From 13f6418cce037ee7a636ee47402eaeac63715750 Mon Sep 17 00:00:00 2001 From: Benji Smith Date: Sat, 20 Jul 2024 15:32:57 -0700 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20inflight/pending=20cal?= =?UTF-8?q?lbacks=20execute=20in=20wrong=20order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/client/doc.js | 2 +- test/client/doc.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/client/doc.js b/lib/client/doc.js index b8c700de2..dd702eace 100644 --- a/lib/client/doc.js +++ b/lib/client/doc.js @@ -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); diff --git a/test/client/doc.js b/test/client/doc.js index 523561ec4..2258eaf6f 100644 --- a/test/client/doc.js +++ b/test/client/doc.js @@ -101,6 +101,21 @@ 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() { From 7aa4f32423c294fe03323107855374d94ce46d63 Mon Sep 17 00:00:00 2001 From: Benji Smith Date: Sun, 21 Jul 2024 01:52:30 -0700 Subject: [PATCH 2/2] fix lint errors --- test/client/doc.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/client/doc.js b/test/client/doc.js index 2258eaf6f..4156ab4a1 100644 --- a/test/client/doc.js +++ b/test/client/doc.js @@ -105,14 +105,20 @@ describe('Doc', function() { var connection = this.connection; var doc = connection.get('dogs', 'fido'); doc.create({name: 'fido'}); - var order = "" - doc.fetch(function() { order += "A" }); + var order = ''; + doc.fetch(function() { + order += 'A'; + }); doc.submitOp([{p: ['snacks'], oi: true}]); - doc.fetch(function() { order += "B" }); + doc.fetch(function() { + order += 'B'; + }); doc.submitOp([{p: ['color'], oi: 'gray'}]); - doc.fetch(function() { order += "C" }); + doc.fetch(function() { + order += 'C'; + }); doc.whenNothingPending(function() { - expect(order).to.eql("ABC"); + expect(order).to.eql('ABC'); done(); }); });