-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathRetention.js
104 lines (102 loc) · 2.74 KB
/
Retention.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import BIN from "BinaryMessage";
import model from "model";
import Preference from "preference";
const preferenceDomain = "selection";
class Retention {
static defaultParams(modelParams) {
let selectionParams = { VERSION:model.VERSION };
for (let name in modelParams)
selectionParams[name] = modelParams[name].value;
return selectionParams;
}
static deletePreference(key) {
return Preference.delete(preferenceDomain, key);
}
static filterParams(retention, modelParams, selectionParams) {
let changed = false;
for (let name in modelParams) {
let param = modelParams[name];
if (param.retention <= retention) {
let value = param.value;
if (selectionParams[name] != value) {
selectionParams[name] = value;
changed = true;
}
}
}
return changed;
}
static readParams(modelParams, key, format) {
let selectionParams = this.defaultParams(modelParams);
try {
let buffer = Preference.get(preferenceDomain, key);
if (buffer) {
let parsedParams = BIN.parse(format, buffer, false);
if (parsedParams) {
if (parsedParams.VERSION == selectionParams.VERSION) {
for (let name in modelParams) {
let param = modelParams[name];
if (name in parsedParams) {
let value = parsedParams[name];
if (param.range) {
if (param.range.indexOf(value) >= 0)
selectionParams[name] = value;
}
else
selectionParams[name] = value;
}
}
}
}
}
}
catch {
this.writeParams(format, key, selectionParams);
}
return selectionParams;
}
static readPreference(key) {
return Preference.get(preferenceDomain, key);
}
static testParams(modelParams, key, format) {
let selectionParams = null;
try {
let buffer = Preference.get(preferenceDomain, key);
if (buffer) {
selectionParams = this.defaultParams(modelParams);
let parsedParams = BIN.parse(format, buffer, false);
if (parsedParams) {
if (parsedParams.VERSION == selectionParams.VERSION) {
for (let name in modelParams) {
let param = modelParams[name];
if (name in parsedParams) {
let value = parsedParams[name];
if (param.range) {
if (param.range.indexOf(value) >= 0)
selectionParams[name] = value;
}
else
selectionParams[name] = value;
}
}
}
}
}
}
catch {
}
return selectionParams;
}
static writeParams(format, key, params) {
let buffer = BIN.serialize(format, params, false);
Preference.set(preferenceDomain, key, buffer);
}
static writePreference(key, value) {
return Preference.set(preferenceDomain, key, value);
}
}
Retention.NEVER = 0;
Retention.UNTIL_RESET = 1;
Retention.UNTIL_FACTORY_RESET = 2;
Retention.FOREVER = 3;
export default Retention;