From c4cfc1f4aef08dbffba4f86c4d922f3995cc78f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=9D=83=E5=A8=81?= Date: Tue, 9 May 2023 15:17:24 +0800 Subject: [PATCH] fix: #604 --- lib/client/doc.js | 4 +++- test/client/doc.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/client/doc.js b/lib/client/doc.js index d5a3d05b7..a1ed5ce0b 100644 --- a/lib/client/doc.js +++ b/lib/client/doc.js @@ -358,7 +358,9 @@ Doc.prototype._handleOp = function(err, message) { // Fetch only sends a new fetch command if no fetches are inflight, which // will act as a natural debouncing so we don't send multiple fetch // requests for many ops received at once. - this.fetch(); + if (!this.inflightFetch.length) { + this.fetch(); + } return; } diff --git a/test/client/doc.js b/test/client/doc.js index fc9e771f8..2f8454e48 100644 --- a/test/client/doc.js +++ b/test/client/doc.js @@ -591,4 +591,47 @@ describe('Doc', function() { }); }); }); + + describe('doc fetch', function () { + var doc, oriHandleMessage, lastMessage; + + beforeEach(function(done) { + doc = this.connection.get('dogs', 'sams'); + oriHandleMessage = this.connection.handleMessage; + + this.connection.handleMessage = function() { + lastMessage = arguments[0]; + oriHandleMessage.apply(this, arguments); + }; + + doc.create({name: 'Sams'}, done); + }); + + afterEach(function() { + this.connection.handleMessage = oriHandleMessage; + }); + + it ('doc fetch should be enqueue', function() { + // create op + expect(doc.pendingOps.length).to.eql(0); + expect(doc.inflightOp).to.eql(null); + + // construct remote message + const resMsg1 = {...lastMessage}; + resMsg1.v += 10; + resMsg1.seq += 10; + + const resMsg2 = {...lastMessage}; + resMsg2.v += 11; + resMsg2.seq += 11; + + expect(doc.inflightFetch.length).to.eql(0); + this.connection.handleMessage(resMsg1); + expect(doc.inflightFetch.length).to.eql(1); + + this.connection.handleMessage(resMsg2); + // we expect do not send multiple fetch, when first fetch uncompleted. + expect(doc.inflightFetch.length).to.eql(1); + }); + }); });