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

users_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: 9f980fd48998cd54610132562c1d8353d5941462 (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
44
45
46
47
48
49
50
51
52
53
54
55
import Api from '../../api';
import Cache from './cache';

class UsersCache extends Cache {
  retrieve(username) {
    if (this.hasData(username)) {
      return Promise.resolve(this.get(username));
    }

    return Api.users('', { username }).then(({ data }) => {
      if (!data.length) {
        throw new Error(`User "${username}" could not be found!`);
      }

      if (data.length > 1) {
        throw new Error(`Expected username "${username}" to be unique!`);
      }

      const user = data[0];
      this.internalStorage[username] = user;
      return user;
    });
    // missing catch is intentional, error handling depends on use case
  }

  retrieveById(userId) {
    if (this.hasData(userId) && this.get(userId).username) {
      return Promise.resolve(this.get(userId));
    }

    return Api.user(userId).then(({ data }) => {
      this.internalStorage[userId] = data;
      return data;
    });
    // missing catch is intentional, error handling depends on use case
  }

  retrieveStatusById(userId) {
    if (this.hasData(userId) && this.get(userId).status) {
      return Promise.resolve(this.get(userId).status);
    }

    return Api.userStatus(userId).then(({ data }) => {
      if (!this.hasData(userId)) {
        this.internalStorage[userId] = {};
      }
      this.internalStorage[userId].status = data;

      return data;
    });
    // missing catch is intentional, error handling depends on use case
  }
}

export default new UsersCache();