Skip to content

Commit e38d200

Browse files
authored
Merge pull request #62 from sbsurf/master
Allowing for custom setting of maxPendingRequests.
2 parents 35e2096 + 51d9da0 commit e38d200

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/bloodhound/remote.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var Remote = (function() {
1919
this.transport = new Transport({
2020
cache: o.cache,
2121
limiter: o.limiter,
22-
transport: o.transport
22+
transport: o.transport,
23+
maxPendingRequests: o.maxPendingRequests
2324
});
2425
}
2526

src/bloodhound/transport.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var Transport = (function() {
99

1010
var pendingRequestsCount = 0,
1111
pendingRequests = {},
12-
maxPendingRequests = 6,
1312
sharedCache = new LruCache(10);
1413

1514
// constructor
@@ -18,6 +17,7 @@ var Transport = (function() {
1817
function Transport(o) {
1918
o = o || {};
2019

20+
this.maxPendingRequests = o.maxPendingRequests || 6;
2121
this.cancelled = false;
2222
this.lastReq = null;
2323

@@ -31,7 +31,7 @@ var Transport = (function() {
3131
// --------------
3232

3333
Transport.setMaxPendingRequests = function setMaxPendingRequests(num) {
34-
maxPendingRequests = num;
34+
this.maxPendingRequests = num;
3535
};
3636

3737
Transport.resetCache = function resetCache() {
@@ -65,7 +65,7 @@ var Transport = (function() {
6565
}
6666

6767
// under the pending request threshold, so fire off a request
68-
else if (pendingRequestsCount < maxPendingRequests) {
68+
else if (pendingRequestsCount < this.maxPendingRequests) {
6969
pendingRequestsCount++;
7070
pendingRequests[fingerprint] =
7171
this._send(o).done(done).fail(fail).always(always);

test/bloodhound/transport_spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@ describe('Transport', function() {
3232
expect(spy).toHaveBeenCalledWith(null, resp.parsed);
3333
});
3434

35-
it('should respect maxPendingRequests configuration', function() {
35+
it('should use maxPendingRequests configuration option', function() {
36+
this.transport = new Transport({ transport: $.ajax, maxPendingRequests: 2 });
37+
38+
for (var i = 0; i < 5; i++) {
39+
this.transport.get('/test' + i, $.noop);
40+
}
41+
42+
expect(ajaxRequests.length).toBe(2);
43+
});
44+
45+
it('should open up to 6 maxPendingRequests by default', function() {
3646
for (var i = 0; i < 10; i++) {
3747
this.transport.get('/test' + i, $.noop);
3848
}

0 commit comments

Comments
 (0)