Skip to content

Commit

Permalink
Don't cache unnamed datasets. Fixes #111.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Mar 14, 2013
1 parent 2da2670 commit f808b04
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
39 changes: 26 additions & 13 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ var Dataset = (function() {
$.error('one of local, prefetch, or remote is requried');
}

this.name = o.name;
this.name = o.name || utils.getUniqueId();
this.limit = o.limit || 5;
this.header = o.header;
this.footer = o.footer;
this.template = compileTemplate(o.template, o.engine);

// used in #initialize
// used then deleted in #initialize
this.local = o.local;
this.prefetch = o.prefetch;
this.remote = o.remote;
Expand All @@ -37,7 +37,10 @@ var Dataset = (function() {

this.itemHash = {};
this.adjacencyList = {};
this.storage = new PersistentStorage(o.name);

// only initialize storage if there's a name otherwise
// loading from storage on subsequent page loads is impossible
this.storage = o.name ? new PersistentStorage(o.name) : null;
}

utils.mixin(Dataset.prototype, {
Expand All @@ -52,11 +55,19 @@ var Dataset = (function() {
_loadPrefetchData: function(o) {
var that = this,
deferred,
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();
version,
protocol,
itemHash,
adjacencyList,
isExpired;

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

o = utils.isString(o) ? { url: o } : o;
o.ttl = utils.isNumber(o.ttl) ? o.ttl : 24 * 60 * 60 * 1000;
Expand All @@ -83,12 +94,14 @@ var Dataset = (function() {
itemHash = processedData.itemHash,
adjacencyList = processedData.adjacencyList;

// store process data in local storage
// store process data in local storage, if storage is available
// this saves us from processing the data on every page load
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);
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._mergeProcessedData(processedData);
}
Expand Down
12 changes: 7 additions & 5 deletions src/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

(function() {
var datasetCache = {}, viewKey = 'ttView', methods;
var cache = {}, viewKey = 'ttView', methods;

methods = {
initialize: function(datasetDefs) {
Expand All @@ -22,11 +22,13 @@
}

datasets = utils.map(datasetDefs, function(o) {
o.name = o.name || utils.getUniqueId();
var dataset = cache[o.name] ? cache[o.name] : new Dataset(o);

return datasetCache[o.name] ?
datasetCache[o.name] :
datasetCache[o.name] = new Dataset(o);
if (o.name) {
cache[o.name] = dataset;
}

return dataset;
});

return this.each(initialize);
Expand Down
36 changes: 30 additions & 6 deletions test/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,26 @@ describe('Dataset', function() {
// --------------

describe('#constructor', function() {
it('should initialize persistent storage', function() {
expect(new Dataset({ local: fixtureData }).storage).toBeDefined();
expect(PersistentStorage).toHaveBeenCalled();
describe('when called with a name', function() {
beforeEach(function() {
this.dataset = new Dataset({ name: '#constructor', local: fixtureData });
});

it('should initialize persistent storage', function() {
expect(this.dataset.storage).toBeDefined();
expect(PersistentStorage).toHaveBeenCalled();
});
});

describe('when called with no name', function() {
beforeEach(function() {
this.dataset = new Dataset({ local: fixtureData });
});

it('should not use persistent storage', function() {
expect(this.dataset.storage).toBeNull();
expect(PersistentStorage).not.toHaveBeenCalled();
});
});

describe('when called with a template but no engine', function() {
Expand Down Expand Up @@ -137,7 +154,11 @@ describe('Dataset', function() {
describe('when called with prefetch', function() {
describe('if data is available in storage', function() {
beforeEach(function() {
this.dataset = new Dataset({ prefetch: '/prefetch.json' });
this.dataset = new Dataset({
name: 'prefetch',
prefetch: '/prefetch.json'
});

this.dataset.storage.get.andCallFake(mockStorageFns.getHit);
this.dataset.initialize();
});
Expand All @@ -164,6 +185,7 @@ describe('Dataset', function() {

beforeEach(function() {
this.dataset = new Dataset({
name: 'prefetch',
prefetch: {
url: '/prefetch.json',
filter: function(data) { return ['filter']; }
Expand Down Expand Up @@ -203,10 +225,12 @@ describe('Dataset', function() {

describe('if filter was not passed in', function() {
beforeEach(function() {
this.dataset = new Dataset({ prefetch: '/prefetch.json' });
this.dataset = new Dataset({
name: 'prefetch',
prefetch: '/prefetch.json'
});

this.dataset.storage.get.andCallFake(mockStorageFns.getMiss);

this.dataset.initialize();

this.request = mostRecentAjaxRequest();
Expand Down

0 comments on commit f808b04

Please sign in to comment.