-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistry.js
58 lines (49 loc) · 1.07 KB
/
Registry.js
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
56
57
58
class Registry {
/**
* @param {string} hash
* @param {{}} data
* @throws {Error}
*/
static updateUser(hash, data) {
const user = this.getUser(hash);
this.data.users[hash] = Object.assign(user, data);
}
/**
* @param {string} hash
* @param {{}} [data]
*/
static createUser(hash, data = {}) {
data = Object.assign({ hash }, data);
this.data.users[hash] = data;
}
/**
* @param {string} hash
* @throws {Error}
*/
static checkUserExists(hash) {
try {
this.getUser(hash);
} catch (e) {
throw new Error(`User does not exist. Hash: ${hash}.`);
}
}
/**
* @param {string} hash
* @return {{}}
* @throws {Error}
*/
static getUser(hash) {
const user = this.data.users[hash];
if (!user)
throw new Error(`Failed to find user. Hash: ${hash}.`);
return Object.freeze(user);
}
}
/**
* @type {{}}
* @private
*/
Registry.data = {
users: {}
};
module.exports = Registry;