Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

An attempt to clean up ajax code related to cache #47

Merged
merged 3 commits into from
Sep 25, 2014
Merged
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
33 changes: 20 additions & 13 deletions lib/jquery/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module.exports = exports = function(window, exec, options) {
if (xhr.readyState === 2) {
req && req.abort();
if (!hard) {
callback('abort', undefined, '');
callback('abort');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that this was to prevent a NPE. This all still behaves properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no difference between the third parameter being '' or undefined here https://github.com/walmartlabs/fruit-loops/blob/ajax-cleanup/lib/jquery/ajax.js#L124 as only its truthyness or falsyness matters.

} else {
xhr.readyState = 4;
}
Expand Down Expand Up @@ -203,16 +203,28 @@ module.exports = exports = function(window, exec, options) {
return xhr;
}

function generateFunc(next) {
execRequest(options, jsonp, undefined, function(err, response) {
if (err) {
return next(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have test coverage already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did not. Added a test.

}

var ttl = 0;
if (!response.cachingInfo['no-cache'] && !response.cachingInfo.private) {
ttl = response.cachingInfo.expires - Date.now();
}

next(undefined, response, ttl);
});
}

if (ajaxCache && (!options.type || options.type === 'GET')) {
ajaxCache.getOrGenerate(options.url, function(callback) {
execRequest(options, true, jsonp, undefined, callback);
},
callback);
ajaxCache.getOrGenerate(options.url, generateFunc, callback);

// Do a custom timeout here so we can prime the cache for future requests
startTimeout();
} else {
req = execRequest(options, false, jsonp, timeout, callback);
req = execRequest(options, jsonp, timeout, callback);
requests.push(xhr);
}

Expand All @@ -227,7 +239,7 @@ module.exports = exports = function(window, exec, options) {



function execRequest(options, caching, jsonp, timeout, callback) {
function execRequest(options, jsonp, timeout, callback) {
return request({
method: options.type || 'GET',
url: options.url,
Expand Down Expand Up @@ -269,12 +281,7 @@ function execRequest(options, caching, jsonp, timeout, callback) {
responseText: body
};

// If this is a cachable response then return it as such
if (caching && !ret.cachingInfo['no-cache'] && !ret.cachingInfo.private) {
callback(undefined, ret, ret.cachingInfo.expires - Date.now());
} else {
callback(undefined, ret, 0);
}
callback(undefined, ret);
});
}

Expand Down
29 changes: 29 additions & 0 deletions test/jquery/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,35 @@ describe('ajax', function() {
});
xhrReturn.readyState.should.equal(2);
});
it('should handle errored connections (with cache enabled)', function(done) {
inst = ajax(window, exec, {cache: policy});
var errorCalled;
var xhrReturn = $.ajax({
url: 'http://localhost:' + (server.info.port+1) + '/',
error: function(xhr, status, error) {
error.should.be.instanceOf(Error);

status.should.equal('error');

xhr.should.equal(xhrReturn);
xhr.readyState.should.equal(4);
errorCalled = true;
},
complete: function(xhr, status) {
should.exist(errorCalled);

status.should.equal('error');

xhr.should.equal(xhrReturn);
xhr.readyState.should.equal(4);

inst.on('complete', function() {
done();
});
}
});
xhrReturn.readyState.should.equal(2);
});
it('should short circuit cached requests', function(done) {
inst = ajax(window, exec, {cache: policy});

Expand Down