Skip to content

Commit a7d3246

Browse files
refactor: rename save method to whenSaved
1 parent 6bb2930 commit a7d3246

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,45 @@ test.each([
2121

2222
const storage = storageAdapter(loggerMock, 'prefix', wrapper);
2323

24-
await storage.load!();
24+
expect(storage.length).toBe(0);
2525

26-
expect(wrapper.getItem).toHaveBeenCalledWith('prefix');
26+
// Load cache from storage wrapper
27+
await storage.load();
2728

29+
expect(wrapper.getItem).toHaveBeenCalledWith('prefix');
2830
expect(storage.length).toBe(1);
2931
expect(storage.key(0)).toBe('key1');
3032
expect(storage.getItem('key1')).toBe('value1');
3133

34+
// Set item
3235
storage.setItem('key2', 'value2');
3336
expect(storage.getItem('key2')).toBe('value2');
3437
expect(storage.length).toBe(2);
3538

39+
// Remove item
3640
storage.removeItem('key1');
3741
expect(storage.getItem('key1')).toBe(null);
3842
expect(storage.length).toBe(1);
3943

40-
await storage.save!();
44+
// Until a till key is set/removed, changes should not be saved/persisted
45+
await storage.whenSaved();
4146
expect(wrapper.setItem).not.toHaveBeenCalled();
4247

48+
// When setting a till key, changes should be saved/persisted immediately
4349
storage.setItem('.till', '1');
4450
expect(storage.length).toBe(2);
4551
expect(storage.key(0)).toBe('key2');
4652
expect(storage.key(1)).toBe('.till');
4753

48-
await storage.save!();
54+
await storage.whenSaved();
4955
expect(wrapper.setItem).toHaveBeenCalledWith('prefix', JSON.stringify({ key2: 'value2', '.till': '1' }));
5056

57+
// When removing a till key, changes should be saved/persisted immediately
5158
storage.removeItem('.till');
5259

53-
await storage.save!();
60+
await storage.whenSaved();
5461
expect(wrapper.setItem).toHaveBeenCalledWith('prefix', JSON.stringify({ key2: 'value2' }));
5562

56-
await storage.save!();
63+
await storage.whenSaved();
5764
expect(wrapper.setItem).toHaveBeenCalledTimes(2);
5865
});

src/storages/inLocalStorage/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
6868
},
6969

7070
destroy() {
71-
return storage.save && storage.save();
71+
return storage.whenSaved && storage.whenSaved();
7272
},
7373

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

src/storages/inLocalStorage/storageAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function isTillKey(key: string) {
77
return key.endsWith('.till');
88
}
99

10-
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): StorageAdapter {
10+
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): Required<StorageAdapter> {
1111
let keys: string[] = [];
1212
let values: string[] = [];
1313

@@ -38,7 +38,7 @@ export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.St
3838
log.error(LOG_PREFIX + 'Rejected promise calling wrapper `getItem` method, with error: ' + e);
3939
}));
4040
},
41-
save() {
41+
whenSaved() {
4242
return savePromise;
4343
},
4444

src/storages/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { ISettings } from '../types';
1111
export interface StorageAdapter {
1212
// Methods to support async storages
1313
load?: () => Promise<void>;
14-
save?: () => Promise<void>;
14+
whenSaved?: () => Promise<void>;
1515
// Methods based on https://developer.mozilla.org/en-US/docs/Web/API/Storage
1616
readonly length: number;
17-
getItem(key: string): string | null;
1817
key(index: number): string | null;
18+
getItem(key: string): string | null;
1919
removeItem(key: string): void;
2020
setItem(key: string, value: string): void;
2121
}

0 commit comments

Comments
 (0)