-
Notifications
You must be signed in to change notification settings - Fork 4
/
PermanentCacheManager.js
81 lines (60 loc) · 2.23 KB
/
PermanentCacheManager.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
//uses property service
class PermanentCacheManager {
constructor(instance) {
this.instance = instance.getScriptProperties();
}
getOperations() {
const self = this; // Capture the current instance context
return {
getPCacheValue: (key) => self.getPCacheValue(key),
getPCache: () => self.getPCache(),
setPCacheValue: (key, value) => self.setPCacheValue(key, value),
setPCacheValues: (objectOfKeyValuePairs) => self.setPCacheValues(objectOfKeyValuePairs),
removePCacheValue: (key) => self.removePCacheValue(key),
deletePCache: () => self.deletePCache()
};
}
getPCacheValue(key) {
if (typeof key === 'number' || typeof key === 'string') {
return this.instance.getProperty(key)
}
throw new TelesunError("KEY_TYPE_ERROR", ERRORS.KEY_TYPE_ERROR);
}
getPCache() {
if (!this.instance) {
throw new TelesunError("TEMPORARY_MEMORY_INSTANCE_NULL", ERRORS.TEMPORARY_MEMORY_INSTANCE_NULL);
}
return this.instance.getProperties()
}
setPCacheValue(key, value) {
if (typeof key !== 'number' && typeof key !== 'string') {
throw new TelesunError("KEY_TYPE_ERROR", ERRORS.KEY_TYPE_ERROR);
}
if (typeof value !== 'number' && typeof value !== 'string') {
throw new TelesunError("VALUE_TYPE_ERROR", ERRORS.VALUE_TYPE_ERROR);
}
this.instance.setProperty(key, value);
return { ok: true, [key]: value }
}
setPCacheValues(objectOfKeyValuePairs) {
if (typeof objectOfKeyValuePairs !== 'object' || Array.isArray(objectOfKeyValuePairs)) {
throw new TelesunError("OBJECT_OF_KEYVALUEPAIRS_TYPE_ERROR", ERRORS.OBJECT_OF_KEYVALUEPAIRS_TYPE_ERROR);
}
this.instance.setProperties(objectOfKeyValuePairs)
return { ok: true, ...objectOfKeyValuePairs }
}
removePCacheValue(key) {
if (typeof key !== 'number' && typeof key !== 'string') {
throw new TelesunError("KEY_TYPE_ERROR", ERRORS.KEY_TYPE_ERROR);
}
this.instance.deleteProperty(key)
return { ok: true, [key]: null }
}
deletePCache() {
if (!this.instance) {
throw new TelesunError("TEMPORARY_MEMORY_INSTANCE_NULL", ERRORS.TEMPORARY_MEMORY_INSTANCE_NULL);
}
this.instance.deleteAllProperties()
return { ok: true, data: null }
}
}