Skip to content

Commit d392cc8

Browse files
Replace splits::checkCache method with storage::validateCache method
1 parent 0cb3a1f commit d392cc8

File tree

10 files changed

+19
-49
lines changed

10 files changed

+19
-49
lines changed

src/storages/AbstractSplitsCacheAsync.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ export abstract class AbstractSplitsCacheAsync implements ISplitsCacheAsync {
2727
return Promise.resolve(true);
2828
}
2929

30-
/**
31-
* Check if the splits information is already stored in cache.
32-
* Noop, just keeping the interface. This is used by client-side implementations only.
33-
*/
34-
checkCache(): Promise<boolean> {
35-
return Promise.resolve(false);
36-
}
37-
3830
/**
3931
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
4032
* Used for SPLIT_KILL push notifications.

src/storages/AbstractSplitsCacheSync.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
4747

4848
abstract clear(): void
4949

50-
/**
51-
* Check if the splits information is already stored in cache. This data can be preloaded.
52-
* It is used as condition to emit SDK_SPLITS_CACHE_LOADED, and then SDK_READY_FROM_CACHE.
53-
*/
54-
checkCache(): boolean {
55-
return false;
56-
}
57-
5850
/**
5951
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
6052
* Used for SPLIT_KILL push notifications.

src/storages/inLocalStorage/SplitsCacheInLocal.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,6 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
212212
}
213213
}
214214

215-
/**
216-
* Check if the splits information is already stored in browser LocalStorage.
217-
* In this function we could add more code to check if the data is valid.
218-
* @override
219-
*/
220-
checkCache(): boolean {
221-
return this.getChangeNumber() > -1;
222-
}
223-
224215
/**
225216
* Clean Splits cache if its `lastUpdated` timestamp is older than the given `expirationTimestamp`,
226217
*
@@ -245,7 +236,7 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
245236
this.updateNewFilter = true;
246237

247238
// if there is cache, clear it
248-
if (this.checkCache()) this.clear();
239+
if (this.getChangeNumber() > -1) this.clear();
249240

250241
} catch (e) {
251242
this.log.error(LOG_PREFIX + e);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,10 @@ test('SPLIT CACHE / LocalStorage', () => {
3030
expect(cache.getSplit('lol1')).toEqual(null);
3131
expect(cache.getSplit('lol2')).toEqual(somethingElse);
3232

33-
expect(cache.checkCache()).toBe(false); // checkCache should return false until localstorage has data.
34-
3533
expect(cache.getChangeNumber() === -1).toBe(true);
3634

37-
expect(cache.checkCache()).toBe(false); // checkCache should return false until localstorage has data.
38-
3935
cache.setChangeNumber(123);
4036

41-
expect(cache.checkCache()).toBe(true); // checkCache should return true once localstorage has data.
42-
4337
expect(cache.getChangeNumber() === 123).toBe(true);
4438

4539
});

src/storages/inLocalStorage/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
5353
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
5454
uniqueKeys: impressionsMode === NONE ? new UniqueKeysCacheInMemoryCS() : undefined,
5555

56+
// @TODO implement
57+
validateCache() {
58+
return splits.getChangeNumber() > -1;
59+
},
60+
5661
destroy() { },
5762

5863
// When using shared instantiation with MEMORY we reuse everything but segments (they are customer per key).

src/storages/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ export interface ISplitsCacheBase {
191191
// only for Client-Side. Returns true if the storage is not synchronized yet (getChangeNumber() === -1) or contains a FF using segments or large segments
192192
usesSegments(): MaybeThenable<boolean>,
193193
clear(): MaybeThenable<boolean | void>,
194-
// should never reject or throw an exception. Instead return false by default, to avoid emitting SDK_READY_FROM_CACHE.
195-
checkCache(): MaybeThenable<boolean>,
196194
killLocally(name: string, defaultTreatment: string, changeNumber: number): MaybeThenable<boolean>,
197195
getNamesByFlagSets(flagSets: string[]): MaybeThenable<Set<string>[]>
198196
}
@@ -209,7 +207,6 @@ export interface ISplitsCacheSync extends ISplitsCacheBase {
209207
trafficTypeExists(trafficType: string): boolean,
210208
usesSegments(): boolean,
211209
clear(): void,
212-
checkCache(): boolean,
213210
killLocally(name: string, defaultTreatment: string, changeNumber: number): boolean,
214211
getNamesByFlagSets(flagSets: string[]): Set<string>[]
215212
}
@@ -226,7 +223,6 @@ export interface ISplitsCacheAsync extends ISplitsCacheBase {
226223
trafficTypeExists(trafficType: string): Promise<boolean>,
227224
usesSegments(): Promise<boolean>,
228225
clear(): Promise<boolean | void>,
229-
checkCache(): Promise<boolean>,
230226
killLocally(name: string, defaultTreatment: string, changeNumber: number): Promise<boolean>,
231227
getNamesByFlagSets(flagSets: string[]): Promise<Set<string>[]>
232228
}
@@ -457,6 +453,7 @@ export interface IStorageSync extends IStorageBase<
457453
IUniqueKeysCacheSync
458454
> {
459455
// Defined in client-side
456+
validateCache?: () => boolean, // @TODO support async
460457
largeSegments?: ISegmentsCacheSync,
461458
}
462459

src/sync/__tests__/syncManagerOnline.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test('syncManagerOnline should start or not the submitter depending on user cons
4848

4949
const syncManager = syncManagerOnlineFactory()({
5050
settings, // @ts-ignore
51-
storage: { splits: { checkCache: () => false } },
51+
storage: {},
5252
});
5353
const submitterManager = syncManager.submitterManager!;
5454

@@ -100,7 +100,7 @@ test('syncManagerOnline should syncAll a single time when sync is disabled', ()
100100
// Test pushManager for main client
101101
const syncManager = syncManagerOnlineFactory(() => pollingManagerMock, pushManagerFactoryMock)({
102102
settings, // @ts-ignore
103-
storage: { splits: { checkCache: () => false } },
103+
storage: { validateCache: () => false },
104104
});
105105

106106
expect(pushManagerFactoryMock).not.toBeCalled();
@@ -169,7 +169,7 @@ test('syncManagerOnline should syncAll a single time when sync is disabled', ()
169169
// pushManager instantiation control test
170170
const testSyncManager = syncManagerOnlineFactory(() => pollingManagerMock, pushManagerFactoryMock)({
171171
settings, // @ts-ignore
172-
storage: { splits: { checkCache: () => false } },
172+
storage: { validateCache: () => false },
173173
});
174174

175175
expect(pushManagerFactoryMock).toBeCalled();
@@ -186,7 +186,7 @@ test('syncManagerOnline should syncAll a single time when sync is disabled', ()
186186
test('syncManagerOnline should emit SDK_SPLITS_CACHE_LOADED if validateCache returns true', async () => {
187187
const params = {
188188
settings: fullSettings,
189-
storage: { splits: { checkCache: () => true } },
189+
storage: { validateCache: () => true },
190190
readiness: { splits: { emit: jest.fn() } }
191191
}; // @ts-ignore
192192
const syncManager = syncManagerOnlineFactory()(params);

src/sync/offline/syncTasks/fromObjectSyncTask.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { forOwn } from '../../../utils/lang';
22
import { IReadinessManager } from '../../../readiness/types';
3-
import { ISplitsCacheSync } from '../../../storages/types';
3+
import { ISplitsCacheSync, IStorageSync } from '../../../storages/types';
44
import { ISplitsParser } from '../splitsParser/types';
55
import { ISplit, ISplitPartial } from '../../../dtos/types';
66
import { syncTaskFactory } from '../../syncTask';
@@ -15,7 +15,7 @@ import { SYNC_OFFLINE_DATA, ERROR_SYNC_OFFLINE_LOADING } from '../../../logger/c
1515
*/
1616
export function fromObjectUpdaterFactory(
1717
splitsParser: ISplitsParser,
18-
storage: { splits: ISplitsCacheSync },
18+
storage: Pick<IStorageSync, 'splits' | 'validateCache'>,
1919
readiness: IReadinessManager,
2020
settings: ISettings,
2121
): () => Promise<boolean> {
@@ -60,9 +60,10 @@ export function fromObjectUpdaterFactory(
6060

6161
if (startingUp) {
6262
startingUp = false;
63-
Promise.resolve(splitsCache.checkCache()).then(cacheReady => {
63+
const isCacheLoaded = storage.validateCache ? storage.validateCache() : false;
64+
Promise.resolve().then(() => {
6465
// Emits SDK_READY_FROM_CACHE
65-
if (cacheReady) readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
66+
if (isCacheLoaded) readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
6667
// Emits SDK_READY
6768
readiness.segments.emit(SDK_SEGMENTS_ARRIVED);
6869
});

src/sync/syncManagerOnline.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ export function syncManagerOnlineFactory(
8989
running = true;
9090

9191
if (startFirstTime) {
92-
const isCacheLoaded = storage.splits.checkCache();
93-
Promise.resolve().then(() => {
94-
if (isCacheLoaded) readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
95-
});
92+
const isCacheLoaded = storage.validateCache ? storage.validateCache() : false;
93+
if (isCacheLoaded) Promise.resolve().then(() => { readiness.splits.emit(SDK_SPLITS_CACHE_LOADED); });
9694
}
9795

9896
// start syncing splits and segments

src/utils/settingsValidation/storage/storageCS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IStorageFactoryParams, IStorageSync } from '../../../storages/types';
88

99
export function __InLocalStorageMockFactory(params: IStorageFactoryParams): IStorageSync {
1010
const result = InMemoryStorageCSFactory(params);
11-
result.splits.checkCache = () => true; // to emit SDK_READY_FROM_CACHE
11+
result.validateCache = () => true; // to emit SDK_READY_FROM_CACHE
1212
return result;
1313
}
1414
__InLocalStorageMockFactory.type = STORAGE_MEMORY;

0 commit comments

Comments
 (0)