Skip to content

Commit

Permalink
fix: share#604
Browse files Browse the repository at this point in the history
  • Loading branch information
李权威 committed May 9, 2023
1 parent 21750e3 commit c4cfc1f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
43 changes: 43 additions & 0 deletions test/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit c4cfc1f

Please sign in to comment.