Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ajax_cache.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 935bd0f16e957a609dd7c16a507bc7d4cfa8c44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import axios from './axios_utils';
import Cache from './cache';

class AjaxCache extends Cache {
  constructor() {
    super();
    this.pendingRequests = {};
  }

  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) {
      pendingRequest = axios
        .get(endpoint)
        .then(({ data }) => {
          this.internalStorage[endpoint] = data;
          delete this.pendingRequests[endpoint];
        })
        .catch((e) => {
          const error = new Error(`${endpoint}: ${e.message}`);
          error.textStatus = e.message;

          delete this.pendingRequests[endpoint];
          throw error;
        });

      this.pendingRequests[endpoint] = pendingRequest;
    }

    return pendingRequest.then(() => this.get(endpoint));
  }
}

export default new AjaxCache();