-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathpermissionfile.js
More file actions
77 lines (65 loc) · 1.93 KB
/
permissionfile.js
File metadata and controls
77 lines (65 loc) · 1.93 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { getDataDbKey, saveToDbKey, setKey } = require('./configutil');
const keyValue = 'permissions';
class PermissionData {
permissionData = {
nativeLocation: '',
wifiLocation: '',
};
constructor() {
// eslint-disable-next-line no-constructor-return
if (PermissionData.instance instanceof PermissionData) return PermissionData.instance;
this.load(() => {
PermissionData.instance = this;
});
}
// eslint-disable-next-line class-methods-use-this
load = (cb) => {
// eslint-disable-next-line consistent-return
getDataDbKey(keyValue, (error, stored) => {
if (error) return;
if (!stored) {
setKey(keyValue, JSON.stringify(this.permissionData), () => {
cb();
});
} else {
const data = JSON.parse(stored[0].value);
Object.keys(this.permissionData).forEach((key) => {
if (data[key]) this.permissionData[key] = data[key];
});
cb();
}
});
};
all = () => this.permissionData;
getData = (key) => this.permissionData[key];
setData = (key, value, cb) => {
this.permissionData[key] = value;
saveToDbKey(keyValue, this.permissionData, () => {
if (cb && typeof cb === 'function') cb();
});
};
update = (key, value, cb) => {
this.permissionData[key] = value;
saveToDbKey(keyValue, this.permissionData, () => {
if (cb && typeof cb === 'function') cb();
});
};
setFullFromData = (data, cb) => {
Object.keys(this.permissionData).forEach((key) => {
if (data[key]) {
this.permissionData[key] = data[key];
}
});
saveToDbKey(keyValue, this.permissionData, () => {
if (cb && typeof cb === 'function') cb();
});
};
setFull = (cb) => {
saveToDbKey(keyValue, this.permissionData, () => {
if (cb && typeof cb === 'function') cb();
});
};
}
const instance = new PermissionData();
Object.freeze(instance);
module.exports = instance;