gitlab-org--gitlab-foss/app/assets/javascripts/lib/utils/ajax_cache.js
2017-05-05 22:47:32 +00:00

32 lines
907 B
JavaScript

const AjaxCache = {
internalStorage: { },
get(endpoint) {
return this.internalStorage[endpoint];
},
hasData(endpoint) {
return Object.prototype.hasOwnProperty.call(this.internalStorage, endpoint);
},
purge(endpoint) {
delete this.internalStorage[endpoint];
},
retrieve(endpoint) {
if (AjaxCache.hasData(endpoint)) {
return Promise.resolve(AjaxCache.get(endpoint));
}
return new Promise((resolve, reject) => {
$.ajax(endpoint) // eslint-disable-line promise/catch-or-return
.then(data => resolve(data),
(jqXHR, textStatus, errorThrown) => {
const error = new Error(`${endpoint}: ${errorThrown}`);
error.textStatus = textStatus;
reject(error);
},
);
})
.then((data) => { this.internalStorage[endpoint] = data; })
.then(() => AjaxCache.get(endpoint));
},
};
export default AjaxCache;