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 all commits
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
84 changes: 60 additions & 24 deletions src/js/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ var Dataset = (function() {
function Dataset(o) {
utils.bindAll(this);

this.storage = new PersistentStorage(o.name);
this.adjacencyList = {};
this.itemHash = {};
if (o.template && !o.engine) {
throw new Error('no template engine specified');
}

this.name = o.name;
this.resetDataOnProtocolSwitch = o.resetDataOnProtocolSwitch || false;
this.queryUrl = o.remote;
this.transport = o.transport;
this.limit = o.limit || 10;
this._customMatcher = o.matcher || null;
this._customRanker = o.ranker || null;
this._ttl_ms = o.ttl_ms || 3 * 24 * 60 * 60 * 1000; // 3 days;
this.limit = o.limit || 5;
this.template = compileTemplate(o.template, o.engine);

this.keys = {
version: 'version',
Expand All @@ -29,8 +24,9 @@ var Dataset = (function() {
adjacencyList: 'adjacencyList'
};

o.local && this._processLocalData(o.local);
o.prefetch && this._loadPrefetchData(o.prefetch);
this.itemHash = {};
this.adjacencyList = {};
this.storage = new PersistentStorage(o.name);
}

utils.mixin(Dataset.prototype, {
Expand All @@ -39,17 +35,20 @@ var Dataset = (function() {
// ---------------

_processLocalData: function(data) {
data && this._mergeProcessedData(this._processData(data));
this._mergeProcessedData(this._processData(data));
},

_loadPrefetchData: function(url) {
_loadPrefetchData: function(o) {
var that = this,
version = this.storage.get(this.keys.version),
protocol = this.storage.get(this.keys.protocol),
itemHash = this.storage.get(this.keys.itemHash),
adjacencyList = this.storage.get(this.keys.adjacencyList),
protocol = this.storage.get(this.keys.protocol),
version = this.storage.get(this.keys.version),
isExpired = version !== VERSION || protocol !== utils.getProtocol();

o = utils.isString(o) ? { url: o } : o;
o.ttl = o.ttl || 3 * 24 * 60 * 60 * 1000; // 3 days

// data was available in local storage, use it
if (itemHash && adjacencyList && !isExpired) {
this._mergeProcessedData({
Expand All @@ -59,20 +58,21 @@ var Dataset = (function() {
}

else {
$.getJSON(url).done(processPrefetchData);
$.getJSON(o.url).done(processPrefetchData);
}

function processPrefetchData(data) {
var processedData = that._processData(data),
var filteredData = o.filter ? o.filter(data) : data,
processedData = that._processData(filteredData),
itemHash = processedData.itemHash,
adjacencyList = processedData.adjacencyList;

// store process data in local storage
// this saves us from processing the data on every page load
that.storage.set(that.keys.itemHash, itemHash, that._ttl_ms);
that.storage.set(that.keys.adjacencyList, adjacencyList, that._ttl_ms);
that.storage.set(that.keys.version, VERSION, that._ttl_ms);
that.storage.set(that.keys.protocol, utils.getProtocol(), that._ttl_ms);
that.storage.set(that.keys.itemHash, itemHash, o.ttl);
that.storage.set(that.keys.adjacencyList, adjacencyList, o.ttl);
that.storage.set(that.keys.version, VERSION, o.ttl);
that.storage.set(that.keys.protocol, utils.getProtocol(), o.ttl);

that._mergeProcessedData(processedData);
}
Expand Down Expand Up @@ -248,18 +248,54 @@ var Dataset = (function() {
// public methods
// ---------------

// the contents of this function are broken out of the constructor
// to help improve the testability of datasets
initialize: function(o) {
if (!o.local && !o.prefetch && !o.remote) {
throw new Error('one of local, prefetch, or remote is requried');
}

this.transport = o.remote ? new Transport(o.remote) : null;

o.local && this._processLocalData(o.local);
o.prefetch && this._loadPrefetchData(o.prefetch);

return this;
},

getSuggestions: function(query, callback) {
var terms = utils.tokenizeQuery(query);
var potentiallyMatchingIds = this._getPotentiallyMatchingIds(terms);
var potentiallyMatchingItems = this._getItemsFromIds(potentiallyMatchingIds);
var matchedItems = utils.filter(potentiallyMatchingItems, this._matcher(terms));
matchedItems.sort(this._ranker);
callback && callback(matchedItems);
if (matchedItems.length < this.limit && this.queryUrl) {
this.transport.get(this.queryUrl, query, this._processRemoteSuggestions(callback, matchedItems));
if (matchedItems.length < this.limit && this.transport) {
this.transport.get(query, this._processRemoteSuggestions(callback, matchedItems));
}
}
});

return Dataset;

function compileTemplate(template, engine) {
var wrapper = '<li class="tt-suggestion">%body</li>',
compiledTemplate;

if (template) {
compiledTemplate = engine.compile(wrapper.replace('%body', template));
}

// if no template is provided, render suggestion
// as its value wrapped in a p tag
else {
compiledTemplate = {
render: function(context) {
return wrapper.replace('%body', '<p>' + context.value + '</p>');
}
};
}

return compiledTemplate;
}
})();
121 changes: 67 additions & 54 deletions src/js/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,99 @@
*/

var Transport = (function() {
var pendingRequests = 0, maxParallelRequests, requestCache;

function Transport(o) {
var rateLimitFn;

utils.bindAll(this);

o = o || {};

rateLimitFn = (/^throttle$/i).test(o.rateLimitFn) ?
utils.throttle : utils.debounce;

this.wait = o.wait || 300;
this.wildcard = o.wildcard || '%QUERY';
this.maxConcurrentRequests = o.maxConcurrentRequests || 6;
o = utils.isString(o) ? { url: o } : o;

this.concurrentRequests = 0;
this.onDeckRequestArgs = null;
requestCache = requestCache || new RequestCache();

this.cache = new RequestCache();
// shared between all instances, last instance to set it wins
maxParallelRequests = utils.isNumber(o.maxParallelRequests) ?
o.maxParallelRequests : maxParallelRequests || 6;

this.get = rateLimitFn(this.get, this.wait);
this.url = o.url;
this.wildcard = o.wildcard || '%QUERY';
this.filter = o.filter;
this.replace = o.replace;

this.ajaxSettings = {
type: 'get',
cache: o.cache,
timeout: o.timeout,
dataType: o.dataType || 'json',
beforeSend: o.beforeSend
};

this.get = (/^throttle$/i.test(o.rateLimitFn) ?
utils.throttle : utils.debounce)(this.get, o.rateLimitWait || 300);
}

utils.mixin(Transport.prototype, {

// private methods
// ---------------

_incrementConcurrentRequests: function() {
this.concurrentRequests++;
},

_decrementConcurrentRequests: function() {
this.concurrentRequests--;
},

_belowConcurrentRequestsThreshold: function() {
return this.concurrentRequests < this.maxConcurrentRequests;
},

// public methods
// --------------

get: function(url, query, cb) {
var that = this, resp;
get: function(query, cb) {
var that = this,
encodedQuery = encodeURIComponent(query || ''),
url,
resp;

url = url.replace(this.wildcard, encodeURIComponent(query || ''));
url = this.replace ?
this.replace(this.url, encodedQuery) :
this.url.replace(this.wildcard, encodedQuery);

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 (belowPendingRequestsThreshold()) {
incrementPendingRequests();
$.ajax(url, this.ajaxSettings).done(done).always(always);
}

else {
this.onDeckRequestArgs = [].slice.call(arguments, 0);
}

// success callback
function done(resp) {
resp = that.filter ? that.filter(resp) : resp;

cb && cb(resp);
requestCache.set(url, resp);
}

// comlete callback
function always() {
decrementPendingRequests();

// ensures request is always made for the latest query
if (that.onDeckRequestArgs) {
that.get.apply(that, that.onDeckRequestArgs);
that.onDeckRequestArgs = null;
}
}
}
});

return Transport;

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

function incrementPendingRequests() {
pendingRequests++;
}

function decrementPendingRequests() {
pendingRequests--;
}

function belowPendingRequestsThreshold() {
return pendingRequests < maxParallelRequests;
}
})();
Loading