-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Rewrite transport #25
Changes from 2 commits
f125f57
ceff358
a6b249a
fd397ab
68266d6
bdb56e3
7632109
6674bd1
77656a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,46 +5,41 @@ | |
*/ | ||
|
||
var Transport = (function() { | ||
var concurrentConnections = 0, | ||
maxConcurrentConnections, | ||
requestCache; | ||
|
||
function Transport(o) { | ||
var rateLimitFn; | ||
|
||
utils.bindAll(this); | ||
|
||
o = o || {}; | ||
|
||
rateLimitFn = (/^throttle$/i).test(o.rateLimitFn) ? | ||
utils.throttle : utils.debounce; | ||
maxConcurrentConnections = utils.isNumber(o.maxConcurrentConnections) ? | ||
o.maxConcurrentConnections : maxConcurrentConnections || 6; | ||
|
||
this.wait = o.wait || 300; | ||
this.wildcard = o.wildcard || '%QUERY'; | ||
this.maxConcurrentRequests = o.maxConcurrentRequests || 6; | ||
|
||
this.concurrentRequests = 0; | ||
this.onDeckRequestArgs = null; | ||
this.ajaxSettings = utils.mixin({}, o.ajax, { | ||
// needs to be true to jqXHR methods (done, always) | ||
// also you're out of your mind if you want to make a sync request | ||
async: true, | ||
beforeSend: function() { | ||
incrementConcurrentConnections(); | ||
|
||
if (o.ajax.beforeSend) { | ||
return o.ajax.beforeSend.apply(this, arguments); | ||
} | ||
} | ||
}); | ||
|
||
this.cache = new RequestCache(); | ||
requestCache = requestCache || new RequestCache(); | ||
|
||
this.get = rateLimitFn(this.get, this.wait); | ||
this.get = (/^throttle$/i.test(o.rateLimitFn) ? | ||
utils.throttle : utils.debounce)(this.get, o.wait || 300); | ||
} | ||
|
||
utils.mixin(Transport.prototype, { | ||
|
||
// private methods | ||
// --------------- | ||
|
||
_incrementConcurrentRequests: function() { | ||
this.concurrentRequests++; | ||
}, | ||
|
||
_decrementConcurrentRequests: function() { | ||
this.concurrentRequests--; | ||
}, | ||
|
||
_belowConcurrentRequestsThreshold: function() { | ||
return this.concurrentRequests < this.maxConcurrentRequests; | ||
}, | ||
|
||
// public methods | ||
// -------------- | ||
|
||
|
@@ -53,29 +48,23 @@ var Transport = (function() { | |
|
||
url = url.replace(this.wildcard, encodeURIComponent(query || '')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is not being used in the ajax call anymore, which i'm guessing is wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I'll make sure to address this. |
||
|
||
if (resp = this.cache.get(url)) { | ||
if (resp = requestCache.get(url)) { | ||
cb && cb(resp); | ||
} | ||
|
||
else if (this._belowConcurrentRequestsThreshold()) { | ||
$.ajax({ | ||
url: url, | ||
type: 'GET', | ||
dataType: 'json', | ||
beforeSend: function() { | ||
that._incrementConcurrentRequests(); | ||
}, | ||
success: function(resp) { | ||
cb && cb(resp); | ||
that.cache.set(url, resp); | ||
}, | ||
complete: function() { | ||
that._decrementConcurrentRequests(); | ||
|
||
if (that.onDeckRequestArgs) { | ||
that.get.apply(that, that.onDeckRequestArgs); | ||
that.onDeckRequestArgs = null; | ||
} | ||
else if (belowConcurrentConnectionsThreshold()) { | ||
$.ajax(this.ajaxSettings) | ||
.done(function(resp) { | ||
cb && cb(resp); | ||
requestCache.set(url, resp); | ||
}) | ||
.always(function() { | ||
decrementConcurrentConnections(); | ||
|
||
// ensures request is always made for the latest query | ||
if (that.onDeckRequestArgs) { | ||
that.get.apply(that, that.onDeckRequestArgs); | ||
that.onDeckRequestArgs = null; | ||
} | ||
}); | ||
} | ||
|
@@ -87,4 +76,19 @@ var Transport = (function() { | |
}); | ||
|
||
return Transport; | ||
|
||
// static methods | ||
// -------------- | ||
|
||
function incrementConcurrentConnections() { | ||
concurrentConnections++; | ||
} | ||
|
||
function decrementConcurrentConnections() { | ||
concurrentConnections--; | ||
} | ||
|
||
function belowConcurrentConnectionsThreshold() { | ||
return concurrentConnections < maxConcurrentConnections; | ||
} | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just have '6' instead of 'maxConcurrentConnections || 6', I don't see how maxConcurrentConnections could be instantiated at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxConcurrentConnections
is a static variable shared between eachTransport
instance. So if you have the following code:The final value for
maxConcurrentConnections
should be4
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha, makes sense. I was considering the single transport case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing we should discuss is if it makes sense to have the transports share a cache. Might make more sense to give each transport its own cache instance, reducing the coupling between the activity on the inputs.