Skip to content

Commit 42257f2

Browse files
author
Shyam Seshadri
committed
wilford's changes to serve cached data and then fetch from server if needed / specified
1 parent 70c3dc8 commit 42257f2

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/Resource.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ ResourceFactory.prototype = {
9494
function(status, response) {
9595
if (status == 200) {
9696
if (action.isArray) {
97+
if (action.cacheThenRetrieve)
98+
value = [];
9799
foreach(response, function(item){
98100
value.push(new Resource(item));
99101
});
@@ -104,7 +106,8 @@ ResourceFactory.prototype = {
104106
} else {
105107
throw {status: status, response:response, message: status + ": " + response};
106108
}
107-
}
109+
},
110+
action.cacheThenRetrieve
108111
);
109112
return value;
110113
};
@@ -135,4 +138,3 @@ ResourceFactory.prototype = {
135138
return Resource;
136139
}
137140
};
138-

src/services.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ angularService('$xhr.bulk', function($xhr, $error, $log){
313313

314314
angularService('$xhr.cache', function($xhr){
315315
var inflight = {}, self = this;;
316-
function cache(method, url, post, callback){
316+
function cache(method, url, post, callback, cacheThenRetrieve){
317317
if (isFunction(post)) {
318318
callback = post;
319319
post = null;
@@ -322,7 +322,11 @@ angularService('$xhr.cache', function($xhr){
322322
var data;
323323
if (data = cache.data[url]) {
324324
callback(200, copy(data.value));
325-
} else if (data = inflight[url]) {
325+
if (!cacheThenRetrieve)
326+
return;
327+
}
328+
329+
if (data = inflight[url]) {
326330
data.callbacks.push(callback);
327331
} else {
328332
inflight[url] = {callbacks: [callback]};
@@ -340,6 +344,7 @@ angularService('$xhr.cache', function($xhr){
340344
});
341345
});
342346
}
347+
343348
} else {
344349
cache.data = {};
345350
cache.delegate(method, url, post, callback);

test/servicesSpec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,28 @@ describe("service", function(){
306306
cache('GET', '/url', null, callback);
307307
xhr.flush();
308308
expect(log).toEqual('"first";"first";');
309+
cache('GET', '/url', null, callback, false);
310+
xhr.flush();
311+
expect(log).toEqual('"first";"first";"first";');
312+
});
313+
314+
it('should first return cache request, then return server request', function(){
315+
xhr.expectGET('/url').respond('first');
316+
cache('GET', '/url', null, callback, true);
317+
xhr.flush();
318+
xhr.expectGET('/url').respond('ERROR');
319+
cache('GET', '/url', null, callback, true);
320+
expect(log).toEqual('"first";"first";');
321+
xhr.flush();
322+
expect(log).toEqual('"first";"first";"ERROR";');
309323
});
310324

311325
it('should serve requests from cache', function(){
312326
cache.data.url = {value:'123'};
313327
cache('GET', 'url', null, callback);
314328
expect(log).toEqual('"123";');
329+
cache('GET', 'url', null, callback, false);
330+
expect(log).toEqual('"123";"123";');
315331
});
316332

317333
it('should keep track of in flight requests and request only once', function(){

0 commit comments

Comments
 (0)