-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathInMemory.js
More file actions
146 lines (114 loc) · 3.52 KB
/
InMemory.js
File metadata and controls
146 lines (114 loc) · 3.52 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { numberIsFinite } from '../../utils/lang';
import usesSegments from '../../utils/splits/usesSegments';
import killLocally from './killLocally';
class SplitCacheInMemory {
constructor() {
this.flush();
}
addSplit(splitName, split) {
const splitFromMemory = this.getSplit(splitName);
if (splitFromMemory) { // We had this Split already
const previousSplit = JSON.parse(splitFromMemory);
if (previousSplit.trafficTypeName) {
const previousTtName = previousSplit.trafficTypeName;
this.ttCache[previousTtName]--;
if (!this.ttCache[previousTtName]) delete this.ttCache[previousTtName];
}
if (usesSegments(previousSplit.conditions)) { // Substract from segments count for the previous version of this Split.
this.splitsWithSegmentsCount--;
}
}
const parsedSplit = JSON.parse(split);
if (parsedSplit) {
// Store the Split.
this.splitCache[splitName] = split;
// Update TT cache
const ttName = parsedSplit.trafficTypeName;
if (ttName) { // safeguard
if (!this.ttCache[ttName]) this.ttCache[ttName] = 0;
this.ttCache[ttName]++;
}
// Add to segments count for the new version of the Split
if (usesSegments(parsedSplit.conditions)) this.splitsWithSegmentsCount++;
return true;
} else {
return false;
}
}
addSplits(entries) {
let results = [];
entries.forEach(keyValuePair => {
results.push(this.addSplit(keyValuePair[0], keyValuePair[1]));
});
return results;
}
removeSplit(splitName) {
const split = this.getSplit(splitName);
if (split) {
// Delete the Split
delete this.splitCache[splitName];
const parsedSplit = JSON.parse(split);
const ttName = parsedSplit.trafficTypeName;
if (ttName) { // safeguard
this.ttCache[ttName]--; // Update tt cache
if (!this.ttCache[ttName]) delete this.ttCache[ttName];
}
// Update the segments count.
if (usesSegments(parsedSplit.conditions)) this.splitsWithSegmentsCount--;
return 1;
} else {
return 0;
}
}
removeSplits(splitNames) {
splitNames.forEach(n => this.removeSplit(n));
return splitNames.length;
}
getSplit(splitName) {
return this.splitCache[splitName];
}
setChangeNumber(changeNumber) {
this.changeNumber = changeNumber;
return true;
}
getChangeNumber() {
return this.changeNumber;
}
getAll() {
return this.getKeys().map(key => this.splitCache[key]);
}
getKeys() {
return Object.keys(this.splitCache);
}
trafficTypeExists(trafficType) {
return numberIsFinite(this.ttCache[trafficType]) && this.ttCache[trafficType] > 0;
}
usesSegments() {
return this.getChangeNumber() === -1 || this.splitsWithSegmentsCount > 0;
}
flush() {
this.splitCache = {};
this.ttCache = {};
this.changeNumber = -1;
this.splitsWithSegmentsCount = 0;
}
/**
* Fetches multiple splits definitions.
*/
fetchMany(splitNames) {
const splits = {};
splitNames.forEach(splitName => {
splits[splitName] = this.splitCache[splitName] || null;
});
return splits;
}
/**
* Check if the splits information is already stored in cache. The data can be preloaded and passed via the config.
*/
checkCache() {
// @TODO rollback if we decide not to emit SDK_READY_FROM_CACHE using InMemory storage
return this.getChangeNumber() > -1;
}
}
SplitCacheInMemory.prototype.killLocally = killLocally;
export default SplitCacheInMemory;