Skip to content

Commit 1cc876b

Browse files
refactor: call storage::save in updaters and remove from cache classes
1 parent afdf656 commit 1cc876b

File tree

7 files changed

+12
-19
lines changed

7 files changed

+12
-19
lines changed

src/storages/inLocalStorage/MySegmentsCacheInLocal.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { AbstractMySegmentsCacheSync } from '../AbstractMySegmentsCacheSync';
44
import type { MySegmentsKeyBuilder } from '../KeyBuilderCS';
55
import { LOG_PREFIX, DEFINED } from './constants';
66
import { StorageAdapter } from '../types';
7-
import { IMySegmentsResponse } from '../../dtos/types';
8-
import { MySegmentsData } from '../../sync/polling/types';
97

108
export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
119

@@ -21,12 +19,6 @@ export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
2119
// There is not need to flush segments cache like splits cache, since resetSegments receives the up-to-date list of active segments
2220
}
2321

24-
resetSegments(segmentsData: MySegmentsData | IMySegmentsResponse): boolean {
25-
const result = super.resetSegments(segmentsData);
26-
if (this.storage.save) this.storage.save();
27-
return result;
28-
}
29-
3022
protected addSegment(name: string): boolean {
3123
const segmentKey = this.keys.buildSegmentNameKey(name);
3224

src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync {
2727

2828
update(toAdd: IRBSegment[], toRemove: IRBSegment[], changeNumber: number): boolean {
2929
this.setChangeNumber(changeNumber);
30-
let updated = toAdd.map(toAdd => this.add(toAdd)).some(result => result);
31-
updated = toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
32-
if (this.storage.save) this.storage.save();
33-
return updated;
30+
const updated = toAdd.map(toAdd => this.add(toAdd)).some(result => result);
31+
return toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated;
3432
}
3533

3634
private setChangeNumber(changeNumber: number) {

src/storages/inLocalStorage/SplitsCacheInLocal.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ export class SplitsCacheInLocal extends AbstractSplitsCacheSync {
7979
this.hasSync = false;
8080
}
8181

82-
update(toAdd: ISplit[], toRemove: ISplit[], changeNumber: number): boolean {
83-
const result = super.update(toAdd, toRemove, changeNumber);
84-
if (this.storage.save) this.storage.save();
85-
return result;
86-
}
87-
8882
addSplit(split: ISplit) {
8983
try {
9084
const name = split.name;

src/storages/inLocalStorage/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
6767
return validateCachePromise || (validateCachePromise = validateCache(options, storage, settings, keys, splits, rbSegments, segments, largeSegments));
6868
},
6969

70+
save() {
71+
return storage.save && storage.save();
72+
},
73+
7074
destroy() {
7175
return storage.whenSaved && storage.whenSaved();
7276
},

src/storages/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ export interface IStorageBase<
483483
uniqueKeys: TUniqueKeysCache,
484484
destroy(): void | Promise<void>,
485485
shared?: (matchingKey: string, onReadyCb: (error?: any) => void) => this
486+
save?: () => void | Promise<void>,
486487
}
487488

488489
export interface IStorageSync extends IStorageBase<

src/sync/polling/updaters/mySegmentsUpdater.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export function mySegmentsUpdaterFactory(
5151
shouldNotifyUpdate = largeSegments!.resetSegments((segmentsData as IMembershipsResponse).ls || {}) || shouldNotifyUpdate;
5252
}
5353

54+
if (storage.save) storage.save();
55+
5456
// Notify update if required
5557
if (usesSegmentsSync(storage) && (shouldNotifyUpdate || readyOnAlreadyExistentState)) {
5658
readyOnAlreadyExistentState = false;

src/sync/polling/updaters/splitChangesUpdater.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function computeMutation<T extends ISplit | IRBSegment>(rules: Array<T>,
117117
export function splitChangesUpdaterFactory(
118118
log: ILogger,
119119
splitChangesFetcher: ISplitChangesFetcher,
120-
storage: Pick<IStorageBase, 'splits' | 'rbSegments' | 'segments'>,
120+
storage: Pick<IStorageBase, 'splits' | 'rbSegments' | 'segments' | 'save'>,
121121
splitFiltersValidation: ISplitFiltersValidation,
122122
splitsEventEmitter?: ISplitsEventEmitter,
123123
requestTimeoutBeforeReady: number = 0,
@@ -185,6 +185,8 @@ export function splitChangesUpdaterFactory(
185185
// @TODO if at least 1 segment fetch fails due to 404 and other segments are updated in the storage, SDK_UPDATE is not emitted
186186
segments.registerSegments(setToArray(usedSegments))
187187
]).then(([ffChanged, rbsChanged]) => {
188+
if (storage.save) storage.save();
189+
188190
if (splitsEventEmitter) {
189191
// To emit SDK_SPLITS_ARRIVED for server-side SDK, we must check that all registered segments have been fetched
190192
return Promise.resolve(!splitsEventEmitter.splitsArrived || ((ffChanged || rbsChanged) && (isClientSide || checkAllSegmentsExist(segments))))

0 commit comments

Comments
 (0)