wilford's changes to serve cached data and then fetch from server if needed / specified

This commit is contained in:
Shyam Seshadri 2010-06-23 13:07:31 -07:00
parent 70c3dc8166
commit 42257f22af
3 changed files with 27 additions and 4 deletions

View file

@ -94,6 +94,8 @@ ResourceFactory.prototype = {
function(status, response) { function(status, response) {
if (status == 200) { if (status == 200) {
if (action.isArray) { if (action.isArray) {
if (action.cacheThenRetrieve)
value = [];
foreach(response, function(item){ foreach(response, function(item){
value.push(new Resource(item)); value.push(new Resource(item));
}); });
@ -104,7 +106,8 @@ ResourceFactory.prototype = {
} else { } else {
throw {status: status, response:response, message: status + ": " + response}; throw {status: status, response:response, message: status + ": " + response};
} }
} },
action.cacheThenRetrieve
); );
return value; return value;
}; };
@ -135,4 +138,3 @@ ResourceFactory.prototype = {
return Resource; return Resource;
} }
}; };

View file

@ -313,7 +313,7 @@ angularService('$xhr.bulk', function($xhr, $error, $log){
angularService('$xhr.cache', function($xhr){ angularService('$xhr.cache', function($xhr){
var inflight = {}, self = this;; var inflight = {}, self = this;;
function cache(method, url, post, callback){ function cache(method, url, post, callback, cacheThenRetrieve){
if (isFunction(post)) { if (isFunction(post)) {
callback = post; callback = post;
post = null; post = null;
@ -322,7 +322,11 @@ angularService('$xhr.cache', function($xhr){
var data; var data;
if (data = cache.data[url]) { if (data = cache.data[url]) {
callback(200, copy(data.value)); callback(200, copy(data.value));
} else if (data = inflight[url]) { if (!cacheThenRetrieve)
return;
}
if (data = inflight[url]) {
data.callbacks.push(callback); data.callbacks.push(callback);
} else { } else {
inflight[url] = {callbacks: [callback]}; inflight[url] = {callbacks: [callback]};
@ -340,6 +344,7 @@ angularService('$xhr.cache', function($xhr){
}); });
}); });
} }
} else { } else {
cache.data = {}; cache.data = {};
cache.delegate(method, url, post, callback); cache.delegate(method, url, post, callback);

View file

@ -306,12 +306,28 @@ describe("service", function(){
cache('GET', '/url', null, callback); cache('GET', '/url', null, callback);
xhr.flush(); xhr.flush();
expect(log).toEqual('"first";"first";'); expect(log).toEqual('"first";"first";');
cache('GET', '/url', null, callback, false);
xhr.flush();
expect(log).toEqual('"first";"first";"first";');
});
it('should first return cache request, then return server request', function(){
xhr.expectGET('/url').respond('first');
cache('GET', '/url', null, callback, true);
xhr.flush();
xhr.expectGET('/url').respond('ERROR');
cache('GET', '/url', null, callback, true);
expect(log).toEqual('"first";"first";');
xhr.flush();
expect(log).toEqual('"first";"first";"ERROR";');
}); });
it('should serve requests from cache', function(){ it('should serve requests from cache', function(){
cache.data.url = {value:'123'}; cache.data.url = {value:'123'};
cache('GET', 'url', null, callback); cache('GET', 'url', null, callback);
expect(log).toEqual('"123";'); expect(log).toEqual('"123";');
cache('GET', 'url', null, callback, false);
expect(log).toEqual('"123";"123";');
}); });
it('should keep track of in flight requests and request only once', function(){ it('should keep track of in flight requests and request only once', function(){