debian-mirror-gitlab/app/assets/javascripts/lib/utils/ajax_cache.js

44 lines
1 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import axios from './axios_utils';
2017-09-10 17:25:29 +05:30
import Cache from './cache';
class AjaxCache extends Cache {
constructor() {
super();
2018-12-13 13:39:08 +05:30
this.pendingRequests = {};
2017-09-10 17:25:29 +05:30
}
override(endpoint, data) {
this.internalStorage[endpoint] = data;
}
retrieve(endpoint, forceRetrieve) {
if (this.hasData(endpoint) && !forceRetrieve) {
return Promise.resolve(this.get(endpoint));
}
let pendingRequest = this.pendingRequests[endpoint];
if (!pendingRequest) {
2018-12-13 13:39:08 +05:30
pendingRequest = axios
.get(endpoint)
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
this.internalStorage[endpoint] = data;
delete this.pendingRequests[endpoint];
})
2021-03-08 18:12:59 +05:30
.catch((e) => {
2018-03-17 18:26:18 +05:30
const error = new Error(`${endpoint}: ${e.message}`);
error.textStatus = e.message;
delete this.pendingRequests[endpoint];
throw error;
});
2017-09-10 17:25:29 +05:30
this.pendingRequests[endpoint] = pendingRequest;
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
return pendingRequest.then(() => this.get(endpoint));
}
}
export default new AjaxCache();