Skip to content

Commit 7b43dc5

Browse files
refactor: disable flag sets caching in local storage
1 parent 7d0475d commit 7b43dc5

File tree

4 files changed

+68
-68
lines changed

4 files changed

+68
-68
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splitsoftware/splitio-commons",
3-
"version": "1.17.1",
3+
"version": "1.17.2-rc.0",
44
"description": "Split JavaScript SDK common components",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",

src/storages/inLocalStorage/SplitsCacheInLocal.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isFiniteNumber, toNumber, isNaNNumber } from '../../utils/lang';
44
import { KeyBuilderCS } from '../KeyBuilderCS';
55
import { ILogger } from '../../logger/types';
66
import { LOG_PREFIX } from './constants';
7-
import { ISet, _Set, setToArray } from '../../utils/lang/sets';
7+
import { ISet, _Set } from '../../utils/lang/sets';
88
import { ISettings } from '../../types';
99
import { getStorageHash } from '../KeyBuilder';
1010

@@ -112,8 +112,8 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
112112
this._incrementCounts(split);
113113
this._decrementCounts(previousSplit);
114114

115-
if (previousSplit) this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
116-
this.addToFlagSets(split);
115+
// if (previousSplit) this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
116+
// this.addToFlagSets(split);
117117

118118
return true;
119119
} catch (e) {
@@ -128,7 +128,7 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
128128
localStorage.removeItem(this.keys.buildSplitKey(name));
129129

130130
this._decrementCounts(split);
131-
if (split) this.removeFromFlagSets(split.name, split.sets);
131+
// if (split) this.removeFromFlagSets(split.name, split.sets);
132132

133133
return true;
134134
} catch (e) {
@@ -268,48 +268,48 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
268268
});
269269
}
270270

271-
private addToFlagSets(featureFlag: ISplit) {
272-
if (!featureFlag.sets) return;
271+
// private addToFlagSets(featureFlag: ISplit) {
272+
// if (!featureFlag.sets) return;
273273

274-
featureFlag.sets.forEach(featureFlagSet => {
274+
// featureFlag.sets.forEach(featureFlagSet => {
275275

276-
if (this.flagSetsFilter.length > 0 && !this.flagSetsFilter.some(filterFlagSet => filterFlagSet === featureFlagSet)) return;
276+
// if (this.flagSetsFilter.length > 0 && !this.flagSetsFilter.some(filterFlagSet => filterFlagSet === featureFlagSet)) return;
277277

278-
const flagSetKey = this.keys.buildFlagSetKey(featureFlagSet);
278+
// const flagSetKey = this.keys.buildFlagSetKey(featureFlagSet);
279279

280-
const flagSetFromLocalStorage = localStorage.getItem(flagSetKey);
280+
// const flagSetFromLocalStorage = localStorage.getItem(flagSetKey);
281281

282-
const flagSetCache = new _Set(flagSetFromLocalStorage ? JSON.parse(flagSetFromLocalStorage) : []);
283-
flagSetCache.add(featureFlag.name);
282+
// const flagSetCache = new _Set(flagSetFromLocalStorage ? JSON.parse(flagSetFromLocalStorage) : []);
283+
// flagSetCache.add(featureFlag.name);
284284

285-
localStorage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
286-
});
287-
}
285+
// localStorage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
286+
// });
287+
// }
288288

289-
private removeFromFlagSets(featureFlagName: string, flagSets?: string[]) {
290-
if (!flagSets) return;
289+
// private removeFromFlagSets(featureFlagName: string, flagSets?: string[]) {
290+
// if (!flagSets) return;
291291

292-
flagSets.forEach(flagSet => {
293-
this.removeNames(flagSet, featureFlagName);
294-
});
295-
}
292+
// flagSets.forEach(flagSet => {
293+
// this.removeNames(flagSet, featureFlagName);
294+
// });
295+
// }
296296

297-
private removeNames(flagSetName: string, featureFlagName: string) {
298-
const flagSetKey = this.keys.buildFlagSetKey(flagSetName);
297+
// private removeNames(flagSetName: string, featureFlagName: string) {
298+
// const flagSetKey = this.keys.buildFlagSetKey(flagSetName);
299299

300-
const flagSetFromLocalStorage = localStorage.getItem(flagSetKey);
300+
// const flagSetFromLocalStorage = localStorage.getItem(flagSetKey);
301301

302-
if (!flagSetFromLocalStorage) return;
302+
// if (!flagSetFromLocalStorage) return;
303303

304-
const flagSetCache = new _Set(JSON.parse(flagSetFromLocalStorage));
305-
flagSetCache.delete(featureFlagName);
304+
// const flagSetCache = new _Set(JSON.parse(flagSetFromLocalStorage));
305+
// flagSetCache.delete(featureFlagName);
306306

307-
if (flagSetCache.size === 0) {
308-
localStorage.removeItem(flagSetKey);
309-
return;
310-
}
307+
// if (flagSetCache.size === 0) {
308+
// localStorage.removeItem(flagSetKey);
309+
// return;
310+
// }
311311

312-
localStorage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
313-
}
312+
// localStorage.setItem(flagSetKey, JSON.stringify(setToArray(flagSetCache)));
313+
// }
314314

315315
}

src/storages/inLocalStorage/__tests__/SplitsCacheInLocal.spec.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ test('SPLIT CACHE / LocalStorage / usesSegments', () => {
163163
expect(cache.usesSegments()).toBe(false); // 0 splits using segments
164164
});
165165

166-
test('SPLIT CACHE / LocalStorage / flag set cache tests', () => {
166+
test('SPLIT CACHE / LocalStorage / flag set cache disabled', () => {
167167
// @ts-ignore
168168
const cache = new SplitsCacheInLocal({
169169
...fullSettings,
@@ -183,21 +183,21 @@ test('SPLIT CACHE / LocalStorage / flag set cache tests', () => {
183183
]);
184184
cache.addSplit(featureFlagWithEmptyFS.name, featureFlagWithEmptyFS);
185185

186-
expect(cache.getNamesByFlagSets(['o'])).toEqual([new _Set(['ff_one', 'ff_two'])]);
187-
expect(cache.getNamesByFlagSets(['n'])).toEqual([new _Set(['ff_one'])]);
188-
expect(cache.getNamesByFlagSets(['e'])).toEqual([new _Set(['ff_one', 'ff_three'])]);
186+
expect(cache.getNamesByFlagSets(['o'])).toEqual([emptySet]);
187+
expect(cache.getNamesByFlagSets(['n'])).toEqual([emptySet]);
188+
expect(cache.getNamesByFlagSets(['e'])).toEqual([emptySet]);
189189
expect(cache.getNamesByFlagSets(['t'])).toEqual([emptySet]); // 't' not in filter
190-
expect(cache.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new _Set(['ff_one', 'ff_two']), new _Set(['ff_one']), new _Set(['ff_one', 'ff_three'])]);
190+
expect(cache.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([emptySet, emptySet, emptySet]);
191191

192192
cache.addSplit(featureFlagOne.name, { ...featureFlagOne, sets: ['1'] });
193193

194194
expect(cache.getNamesByFlagSets(['1'])).toEqual([emptySet]); // '1' not in filter
195-
expect(cache.getNamesByFlagSets(['o'])).toEqual([new _Set(['ff_two'])]);
195+
expect(cache.getNamesByFlagSets(['o'])).toEqual([emptySet]);
196196
expect(cache.getNamesByFlagSets(['n'])).toEqual([emptySet]);
197197

198198
cache.addSplit(featureFlagOne.name, { ...featureFlagOne, sets: ['x'] });
199-
expect(cache.getNamesByFlagSets(['x'])).toEqual([new _Set(['ff_one'])]);
200-
expect(cache.getNamesByFlagSets(['o', 'e', 'x'])).toEqual([new _Set(['ff_two']), new _Set(['ff_three']), new _Set(['ff_one'])]);
199+
expect(cache.getNamesByFlagSets(['x'])).toEqual([emptySet]);
200+
expect(cache.getNamesByFlagSets(['o', 'e', 'x'])).toEqual([emptySet, emptySet, emptySet]);
201201

202202

203203
cache.removeSplit(featureFlagOne.name);
@@ -211,27 +211,27 @@ test('SPLIT CACHE / LocalStorage / flag set cache tests', () => {
211211
expect(cache.getNamesByFlagSets([])).toEqual([]);
212212
});
213213

214-
// if FlagSets are not defined, it should store all FlagSets in memory.
215-
test('SPLIT CACHE / LocalStorage / flag set cache tests without filters', () => {
216-
const cacheWithoutFilters = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS('SPLITIO', 'user'));
217-
const emptySet = new _Set([]);
218-
219-
cacheWithoutFilters.addSplits([
220-
[featureFlagOne.name, featureFlagOne],
221-
[featureFlagTwo.name, featureFlagTwo],
222-
[featureFlagThree.name, featureFlagThree],
223-
]);
224-
cacheWithoutFilters.addSplit(featureFlagWithEmptyFS.name, featureFlagWithEmptyFS);
225-
226-
expect(cacheWithoutFilters.getNamesByFlagSets(['o'])).toEqual([new _Set(['ff_one', 'ff_two'])]);
227-
expect(cacheWithoutFilters.getNamesByFlagSets(['n'])).toEqual([new _Set(['ff_one'])]);
228-
expect(cacheWithoutFilters.getNamesByFlagSets(['e'])).toEqual([new _Set(['ff_one', 'ff_three'])]);
229-
expect(cacheWithoutFilters.getNamesByFlagSets(['t'])).toEqual([new _Set(['ff_two', 'ff_three'])]);
230-
expect(cacheWithoutFilters.getNamesByFlagSets(['y'])).toEqual([emptySet]);
231-
expect(cacheWithoutFilters.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new _Set(['ff_one', 'ff_two']), new _Set(['ff_one']), new _Set(['ff_one', 'ff_three'])]);
232-
233-
// Validate that the feature flag cache is cleared when calling `clear` method
234-
cacheWithoutFilters.clear();
235-
expect(localStorage.length).toBe(1); // only 'SPLITIO.hash' should remain in localStorage
236-
expect(localStorage.key(0)).toBe('SPLITIO.hash');
237-
});
214+
// // if FlagSets are not defined, it should store all FlagSets in memory.
215+
// test('SPLIT CACHE / LocalStorage / flag set cache tests without filters', () => {
216+
// const cacheWithoutFilters = new SplitsCacheInLocal(fullSettings, new KeyBuilderCS('SPLITIO', 'user'));
217+
// const emptySet = new _Set([]);
218+
219+
// cacheWithoutFilters.addSplits([
220+
// [featureFlagOne.name, featureFlagOne],
221+
// [featureFlagTwo.name, featureFlagTwo],
222+
// [featureFlagThree.name, featureFlagThree],
223+
// ]);
224+
// cacheWithoutFilters.addSplit(featureFlagWithEmptyFS.name, featureFlagWithEmptyFS);
225+
226+
// expect(cacheWithoutFilters.getNamesByFlagSets(['o'])).toEqual([new _Set(['ff_one', 'ff_two'])]);
227+
// expect(cacheWithoutFilters.getNamesByFlagSets(['n'])).toEqual([new _Set(['ff_one'])]);
228+
// expect(cacheWithoutFilters.getNamesByFlagSets(['e'])).toEqual([new _Set(['ff_one', 'ff_three'])]);
229+
// expect(cacheWithoutFilters.getNamesByFlagSets(['t'])).toEqual([new _Set(['ff_two', 'ff_three'])]);
230+
// expect(cacheWithoutFilters.getNamesByFlagSets(['y'])).toEqual([emptySet]);
231+
// expect(cacheWithoutFilters.getNamesByFlagSets(['o', 'n', 'e'])).toEqual([new _Set(['ff_one', 'ff_two']), new _Set(['ff_one']), new _Set(['ff_one', 'ff_three'])]);
232+
233+
// // Validate that the feature flag cache is cleared when calling `clear` method
234+
// cacheWithoutFilters.clear();
235+
// expect(localStorage.length).toBe(1); // only 'SPLITIO.hash' should remain in localStorage
236+
// expect(localStorage.key(0)).toBe('SPLITIO.hash');
237+
// });

0 commit comments

Comments
 (0)