Skip to content

Commit

Permalink
Merge pull request #157 from jharding/140-thumbprint
Browse files Browse the repository at this point in the history
Add thumbprint prefetch option
  • Loading branch information
jharding committed Mar 31, 2013
2 parents 05d130f + 19eb54c commit 21ec913
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
52 changes: 27 additions & 25 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/

var Dataset = (function() {
var keys = {
thumbprint: 'thumbprint',
protocol: 'protocol',
itemHash: 'itemHash',
adjacencyList: 'adjacencyList'
};

function Dataset(o) {
utils.bindAll(this);
Expand All @@ -30,13 +36,6 @@ var Dataset = (function() {
this.prefetch = o.prefetch;
this.remote = o.remote;

this.keys = {
version: 'version',
protocol: 'protocol',
itemHash: 'itemHash',
adjacencyList: 'adjacencyList'
};

this.itemHash = {};
this.adjacencyList = {};

Expand All @@ -56,29 +55,32 @@ var Dataset = (function() {

_loadPrefetchData: function(o) {
var that = this,
deferred,
version,
protocol,
itemHash,
adjacencyList,
isExpired;
thumbprint = VERSION + (o.thumbprint || ''),
storedThumbprint,
storedProtocol,
storedItemHash,
storedAdjacencyList,
isExpired,
deferred;

if (this.storage) {
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);
isExpired = version !== VERSION || protocol !== utils.getProtocol();
storedThumbprint = this.storage.get(keys.thumbprint);
storedProtocol = this.storage.get(keys.protocol);
storedItemHash = this.storage.get(keys.itemHash);
storedAdjacencyList = this.storage.get(keys.adjacencyList);
}

isExpired = storedThumbprint !== thumbprint ||
storedProtocol !== utils.getProtocol();

o = utils.isString(o) ? { url: o } : o;
o.ttl = utils.isNumber(o.ttl) ? o.ttl : 24 * 60 * 60 * 1000;

// data was available in local storage, use it
if (itemHash && adjacencyList && !isExpired) {
if (storedItemHash && storedAdjacencyList && !isExpired) {
this._mergeProcessedData({
itemHash: itemHash,
adjacencyList: adjacencyList
itemHash: storedItemHash,
adjacencyList: storedAdjacencyList
});

deferred = $.Deferred().resolve();
Expand All @@ -99,10 +101,10 @@ var Dataset = (function() {
// store process data in local storage, if storage is available
// this saves us from processing the data on every page load
if (that.storage) {
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.storage.set(keys.itemHash, itemHash, o.ttl);
that.storage.set(keys.adjacencyList, adjacencyList, o.ttl);
that.storage.set(keys.thumbprint, thumbprint, o.ttl);
that.storage.set(keys.protocol, utils.getProtocol(), o.ttl);
}

that._mergeProcessedData(processedData);
Expand Down
8 changes: 4 additions & 4 deletions src/persistent_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var PersistentStorage = (function() {
var ls = window.localStorage, methods;

function PersistentStorage(namespace) {
this.prefix = ["__", namespace, "__"].join("");
this.ttlKey = "__ttl__";
this.keyMatcher = new RegExp("^" + this.prefix);
this.prefix = ['__', namespace, '__'].join('');
this.ttlKey = '__ttl__';
this.keyMatcher = new RegExp('^' + this.prefix);
}

if (window.localStorage && window.JSON) {
Expand Down Expand Up @@ -63,7 +63,7 @@ var PersistentStorage = (function() {
for (i = 0; i < len; i++) {
if ((key = ls.key(i)).match(this.keyMatcher)) {
// gather keys to remove after loop exits
keys.push(key.replace(this.keyMatcher, ""));
keys.push(key.replace(this.keyMatcher, ''));
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Dataset', function() {
return expectedAdjacencyList;
}

else if (/version/.test(key)) {
else if (/thumbprint/.test(key)) {
return VERSION;
}

Expand Down Expand Up @@ -246,7 +246,7 @@ describe('Dataset', function() {
expect(this.dataset.storage.set)
.toHaveBeenCalledWith('protocol', utils.getProtocol(), ttl);
expect(this.dataset.storage.set)
.toHaveBeenCalledWith('version', VERSION, ttl);
.toHaveBeenCalledWith('thumbprint', VERSION, ttl);
});
});

Expand Down Expand Up @@ -284,7 +284,7 @@ describe('Dataset', function() {
expect(this.dataset.storage.set)
.toHaveBeenCalledWith('protocol', utils.getProtocol(), ttl);
expect(this.dataset.storage.set)
.toHaveBeenCalledWith('version', VERSION, ttl);
.toHaveBeenCalledWith('thumbprint', VERSION, ttl);
});
});
});
Expand Down

0 comments on commit 21ec913

Please sign in to comment.