Skip to content
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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 46 additions & 43 deletions src/js/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,40 @@
*/

var Transport = (function() {
var concurrentConnections = 0,
maxConcurrentConnections;

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;
Copy link
Collaborator

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?

Copy link
Contributor Author

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 each Transport instance. So if you have the following code:

$('.typeahead-one').typeahead({
  maxConcurrentConnections: 10
});

$('.typeahead-one').typeahead({
  maxConcurrentConnections: 4
});

The final value for maxConcurrentConnections should be 4.

Copy link
Collaborator

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.

Copy link
Collaborator

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.


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();

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
// --------------

Expand All @@ -57,25 +51,19 @@ var Transport = (function() {
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);
that.cache.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;
}
});
}
Expand All @@ -87,4 +75,19 @@ var Transport = (function() {
});

return Transport;

// static methods
// --------------

function incrementConcurrentConnections() {
concurrentConnections++;
}

function decrementConcurrentConnections() {
concurrentConnections--;
}

function belowConcurrentConnectionsThreshold() {
return concurrentConnections < maxConcurrentConnections;
}
})();
66 changes: 29 additions & 37 deletions src/js/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
*/

(function() {
var initializedDatasets = {},
transportOptions = {},
transport,
methods;

jQuery.fn.typeahead = typeahead;
typeahead.configureTransport = configureTransport;
var initializedDatasets = {}, methods;

methods = {
initialize: function(datasetDefs) {
Expand All @@ -23,44 +17,46 @@
throw new Error('no datasets provided');
}

delete typeahead.configureTransport;
transport = transport || new Transport(transportOptions);
utils.each(datasetDefs, function(i, o) {
var transport, dataset;

utils.each(datasetDefs, function(i, datasetDef) {
var dataset,
name = datasetDef.name = datasetDef.name || utils.getUniqueId();
o.name = o.name || utils.getUniqueId();

// dataset by this name has already been intialized, used it
if (initializedDatasets[name]) {
dataset = initializedDatasets[name];
if (initializedDatasets[o.name]) {
dataset = initializedDatasets[o.name];
}

else {
datasetDef.limit = datasetDef.limit || 5;
datasetDef.template = datasetDef.template;
datasetDef.engine = datasetDef.engine;

if (datasetDef.template && !datasetDef.engine) {
throw new Error('no template engine specified for ' + name);
if (o.template && !o.engine) {
throw new Error('no template engine specified for ' + o.name);
}

transport = new Transport({
ajax: o.ajax,
wildcard: o.wildcard,
rateLimitFn: o.rateLimitFn,
maxConcurrentConnections: o.maxConcurrentConnections
});

dataset = initializedDatasets[name] = new Dataset({
name: datasetDef.name,
limit: datasetDef.limit,
local: datasetDef.local,
prefetch: datasetDef.prefetch,
remote: datasetDef.remote,
matcher: datasetDef.matcher,
ranker: datasetDef.ranker,
name: o.name,
limit: o.limit,
local: o.local,
prefetch: o.prefetch,
remote: o.remote,
matcher: o.matcher,
ranker: o.ranker,
transport: transport
});
}

// faux dataset used by TypeaheadView instances
datasets[name] = {
name: datasetDef.name,
limit: datasetDef.limit,
template: datasetDef.template,
engine: datasetDef.engine,
name: o.name,
limit: o.limit,
template: o.template,
engine: o.engine,
getSuggestions: dataset.getSuggestions
};
});
Expand All @@ -73,17 +69,13 @@
}
};

function typeahead(method) {
jQuery.fn.typeahead = function(method) {
if (methods[method]) {
methods[method].apply(this, [].slice.call(arguments, 1));
}

else {
methods.initialize.apply(this, arguments);
}
}

function configureTransport(o) {
transportOptions = o;
}
};
})();