-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSplitsCacheInLocal.ts
More file actions
234 lines (178 loc) · 6.89 KB
/
SplitsCacheInLocal.ts
File metadata and controls
234 lines (178 loc) · 6.89 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { ISplit } from '../../dtos/types';
import { AbstractSplitsCacheSync, usesSegments } from '../AbstractSplitsCacheSync';
import { isFiniteNumber, toNumber, isNaNNumber } from '../../utils/lang';
import { KeyBuilderCS } from '../KeyBuilderCS';
import { ILogger } from '../../logger/types';
import { LOG_PREFIX } from './constants';
import { ISettings } from '../../types';
import { setToArray } from '../../utils/lang/sets';
import { StorageAdapter } from '../types';
export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
private readonly keys: KeyBuilderCS;
private readonly log: ILogger;
private readonly flagSetsFilter: string[];
private hasSync?: boolean;
private readonly storage: StorageAdapter;
constructor(settings: ISettings, keys: KeyBuilderCS, storage: StorageAdapter) {
super();
this.keys = keys;
this.log = settings.log;
this.flagSetsFilter = settings.sync.__splitFiltersValidation.groupedFilters.bySet;
this.storage = storage;
}
private _decrementCount(key: string) {
const count = toNumber(this.storage.getItem(key)) - 1;
if (count > 0) this.storage.setItem(key, count + '');
else this.storage.removeItem(key);
}
private _decrementCounts(split: ISplit) {
try {
const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
this._decrementCount(ttKey);
if (usesSegments(split)) {
const segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
this._decrementCount(segmentsCountKey);
}
} catch (e) {
this.log.error(LOG_PREFIX + e);
}
}
private _incrementCounts(split: ISplit) {
try {
const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
this.storage.setItem(ttKey, (toNumber(this.storage.getItem(ttKey)) + 1) + '');
if (usesSegments(split)) {
const segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
this.storage.setItem(segmentsCountKey, (toNumber(this.storage.getItem(segmentsCountKey)) + 1) + '');
}
} catch (e) {
this.log.error(LOG_PREFIX + e);
}
}
/**
* Removes all splits cache related data from localStorage (splits, counters, changeNumber and lastUpdated).
* We cannot simply call `localStorage.clear()` since that implies removing user items from the storage.
*/
clear() {
// collect item keys
const len = this.storage.length;
const accum = [];
for (let cur = 0; cur < len; cur++) {
const key = this.storage.key(cur);
if (key != null && this.keys.isSplitsCacheKey(key)) accum.push(key);
}
// remove items
accum.forEach(key => {
this.storage.removeItem(key);
});
this.hasSync = false;
}
addSplit(split: ISplit) {
const name = split.name;
const splitKey = this.keys.buildSplitKey(name);
const splitFromStorage = this.storage.getItem(splitKey);
const previousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : null;
if (previousSplit) {
this._decrementCounts(previousSplit);
this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
}
this.storage.setItem(splitKey, JSON.stringify(split));
this._incrementCounts(split);
this.addToFlagSets(split);
return true;
}
removeSplit(name: string): boolean {
const split = this.getSplit(name);
if (!split) return false;
this.storage.removeItem(this.keys.buildSplitKey(name));
this._decrementCounts(split);
this.removeFromFlagSets(split.name, split.sets);
return true;
}
getSplit(name: string): ISplit | null {
const item = this.storage.getItem(this.keys.buildSplitKey(name));
return item && JSON.parse(item);
}
setChangeNumber(changeNumber: number): boolean {
try {
this.storage.setItem(this.keys.buildSplitsTillKey(), changeNumber + '');
// update "last updated" timestamp with current time
this.storage.setItem(this.keys.buildLastUpdatedKey(), Date.now() + '');
this.hasSync = true;
return true;
} catch (e) {
this.log.error(LOG_PREFIX + e);
return false;
}
}
getChangeNumber(): number {
const n = -1;
let value: string | number | null = this.storage.getItem(this.keys.buildSplitsTillKey());
if (value !== null) {
value = parseInt(value, 10);
return isNaNNumber(value) ? n : value;
}
return n;
}
getSplitNames(): string[] {
const len = this.storage.length;
const accum = [];
let cur = 0;
while (cur < len) {
const key = this.storage.key(cur);
if (key != null && this.keys.isSplitKey(key)) accum.push(this.keys.extractKey(key));
cur++;
}
return accum;
}
trafficTypeExists(trafficType: string): boolean {
const ttCount = toNumber(this.storage.getItem(this.keys.buildTrafficTypeKey(trafficType)));
return isFiniteNumber(ttCount) && ttCount > 0;
}
usesSegments() {
// If cache hasn't been synchronized with the cloud, assume we need them.
if (!this.hasSync) return true;
const storedCount = this.storage.getItem(this.keys.buildSplitsWithSegmentCountKey());
const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber(storedCount);
return isFiniteNumber(splitsWithSegmentsCount) ?
splitsWithSegmentsCount > 0 :
true;
}
getNamesByFlagSets(flagSets: string[]): Set<string>[] {
return flagSets.map(flagSet => {
const flagSetKey = this.keys.buildFlagSetKey(flagSet);
const flagSetFromStorage = this.storage.getItem(flagSetKey);
return new Set(flagSetFromStorage ? JSON.parse(flagSetFromStorage) : []);
});
}
private addToFlagSets(featureFlag: ISplit) {
if (!featureFlag.sets) return;
featureFlag.sets.forEach(featureFlagSet => {
if (this.flagSetsFilter.length > 0 && !this.flagSetsFilter.some(filterFlagSet => filterFlagSet === featureFlagSet)) return;
const flagSetKey = this.keys.buildFlagSetKey(featureFlagSet);
const flagSetFromStorage = this.storage.getItem(flagSetKey);
const flagSetCache = new Set(flagSetFromStorage ? JSON.parse(flagSetFromStorage) : []);
if (flagSetCache.has(featureFlag.name)) return;
flagSetCache.add(featureFlag.name);
this.storage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
});
}
private removeFromFlagSets(featureFlagName: string, flagSets?: string[]) {
if (!flagSets) return;
flagSets.forEach(flagSet => {
this.removeNames(flagSet, featureFlagName);
});
}
private removeNames(flagSetName: string, featureFlagName: string) {
const flagSetKey = this.keys.buildFlagSetKey(flagSetName);
const flagSetFromStorage = this.storage.getItem(flagSetKey);
if (!flagSetFromStorage) return;
const flagSetCache = new Set(JSON.parse(flagSetFromStorage));
flagSetCache.delete(featureFlagName);
if (flagSetCache.size === 0) {
this.storage.removeItem(flagSetKey);
return;
}
this.storage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
}
}