Skip to content

Instantly share code, notes, and snippets.

@renie
Last active August 29, 2015 14:20
Show Gist options
  • Save renie/287e4d00fed74ff6ceda to your computer and use it in GitHub Desktop.
Save renie/287e4d00fed74ff6ceda to your computer and use it in GitHub Desktop.
cache ajax
/*
*
*
*
* WILL BE MOVED SOON
* USE THIS => https://github.com/renie/CacheRequest
*
*
*
*
*
*/
var CachedRequestHelper = {
storageKeys : {
timeout : 'TIMEOUT',
lastcall : 'LASTCALL',
response : 'RESPONSE'
},
defaultTimeout : 300000, // 5 minutos
send : function(options) {
this.__prepareVariables(options);
let valid = this.__isValid();
if (valid)
this.__callCachedCallback();
else
this.__doRequest();
this.d.promise();
},
__prepareVariables : function(options) {
this.opt = options;
this.d = new $.Deferred();
this.__createHash();
this.__getStored();
},
__createHash : function() {
this.hash = this.opt.url + '' + (this.opt.data || '') + (this.opt.method || '-ANY');
},
__getStored : function() {
this.stored = localStorage.getItem(this.hash);
this.stored = JSON.parse(this.stored) || null;
},
__isValid : function() {
if (!this.stored)
return false;
let to = this.stored[this.storageKeys.timeout],
lc = this.stored[this.storageKeys.lastcall];
return (Date.now() - lc) < to;
},
__callCachedCallback : function() {
if (this.opt.hasOwnProperty('success'))
this.opt.success.call(this.opt.context, null, 'CACHED', this.stored[this.storageKeys.response]);
else if (this.opt.hasOwnProperty('complete'))
this.opt.complete(this.opt.context, 'CACHED', this.stored[this.storageKeys.response]);
this.d.resolve();
},
__doRequest: function() {
this.originalSettings = {
's' : this.opt.success,
'f' : this.opt.error,
'c' : this.opt.complete,
'ctx' : this.opt.context
};
this.opt.success = this.__successCallback;
this.opt.error = this.__errorCallback;
this.opt.complete = this.__completeCallback;
this.opt.context = this;
$.ajax(this.opt);
},
__successCallback : function(data, status, res) {
this.__cacheRequest(res);
if (this.originalSettings.s)
this.originalSettings.s.call(this.originalSettings.ctx, data, status, res);
else if (this.originalSettings.c)
this.originalSettings.c.call(this.originalSettings.ctx, res, status);
this.d.resolve();
},
__errorCallback : function(res, status, err) {
if (this.opt.useOldIfError && this.stored)
res = this.stored[this.storageKeys.response];
if (this.originalSettings.e)
this.originalSettings.call(this.originalSettings.ctx, res, status, err);
this.d.resolve();
},
__completeCallback : function(res, status) {
if(res.status === 0 && status === 'error') {
if (this.opt.useOldIfError && this.stored)
res = this.stored[this.storageKeys.response];
else
res = null;
}
if (this.originalSettings.c)
this.originalSettings.c.call(this.originalSettings.ctx, res, status);
else
console.error('No "complete" callback, no connection and no cache available (or you don\'t want it).');
this.d.resolve();
},
__cacheRequest : function(res) {
let data = {};
data[this.storageKeys.timeout] = this.opt.cacheTimeout || this.defaultTimeout;
data[this.storageKeys.lastcall] = Date.now();
data[this.storageKeys.response] = res;
localStorage.setItem(this.hash, JSON.stringify(data));
}
};
export default CachedRequestHelper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment