Skip to content

Commit 299585b

Browse files
refactor: rename Storage interface to StorageWrapper for clarity and consistency
1 parent 4b0988d commit 299585b

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ describe('IN LOCAL STORAGE', () => {
3535
expect(storage).toBe(fakeInMemoryStorage);
3636

3737
// @ts-expect-error Provided storage is invalid
38-
storageFactory = InLocalStorage({ prefix: 'prefix', storage: {} });
38+
storageFactory = InLocalStorage({ prefix: 'prefix', wrapper: {} });
3939
storage = storageFactory(internalSdkParams);
4040
expect(storage).toBe(fakeInMemoryStorage);
4141

4242
// Provided storage is valid
43-
storageFactory = InLocalStorage({ prefix: 'prefix', storage: { getItem: () => Promise.resolve(null), setItem: () => Promise.resolve(), removeItem: () => Promise.resolve() } });
43+
storageFactory = InLocalStorage({ prefix: 'prefix', wrapper: { getItem: () => Promise.resolve(null), setItem: () => Promise.resolve(), removeItem: () => Promise.resolve() } });
4444
storage = storageFactory(internalSdkParams);
4545
expect(storage).not.toBe(fakeInMemoryStorage);
4646

src/storages/inLocalStorage/__tests__/wrapper.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
44

55
export const PREFIX = 'SPLITIO';
66

7-
export function createMemoryStorage(): SplitIO.Storage {
7+
export function createMemoryStorage(): SplitIO.StorageWrapper {
88
let cache: Record<string, string> = {};
99
return {
1010
getItem(key: string) {

src/storages/inLocalStorage/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { EventsCacheInMemory } from '../inMemory/EventsCacheInMemory';
44
import { IStorageFactoryParams, IStorageSync, IStorageSyncFactory } from '../types';
55
import { validatePrefix } from '../KeyBuilder';
66
import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
7-
import { isLocalStorageAvailable, isStorageValid } from '../../utils/env/isLocalStorageAvailable';
7+
import { isLocalStorageAvailable, isStorageWrapperValid } from '../../utils/env/isLocalStorageAvailable';
88
import { SplitsCacheInLocal } from './SplitsCacheInLocal';
99
import { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
1010
import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
@@ -34,23 +34,23 @@ function isTillKey(key: string) {
3434
return key.endsWith('.till');
3535
}
3636

37-
export function storageAdapter(log: ILogger, prefix: string, storage: SplitIO.Storage): StorageAdapter {
37+
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): StorageAdapter {
3838
let cache: Record<string, string> = {};
3939

4040
let connectPromise: Promise<void> | undefined;
4141
let disconnectPromise = Promise.resolve();
4242

4343
return {
4444
load() {
45-
return connectPromise || (connectPromise = storage.getItem(prefix).then((storedCache) => {
45+
return connectPromise || (connectPromise = wrapper.getItem(prefix).then((storedCache) => {
4646
cache = JSON.parse(storedCache || '{}');
4747
}).catch((e) => {
4848
log.error(LOG_PREFIX + 'Rejected promise calling storage getItem, with error: ' + e);
4949
}));
5050
},
5151
save() {
5252
return disconnectPromise = disconnectPromise.then(() => {
53-
return storage.setItem(prefix, JSON.stringify(cache)).catch((e) => {
53+
return wrapper.setItem(prefix, JSON.stringify(cache)).catch((e) => {
5454
log.error(LOG_PREFIX + 'Rejected promise calling storage setItem, with error: ' + e);
5555
});
5656
});
@@ -76,9 +76,9 @@ export function storageAdapter(log: ILogger, prefix: string, storage: SplitIO.St
7676
};
7777
}
7878

79-
function validateStorage(log: ILogger, prefix: string, storage?: SplitIO.Storage): StorageAdapter | undefined {
80-
if (storage) {
81-
if (isStorageValid(storage)) return storageAdapter(log, prefix, storage);
79+
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.StorageWrapper): StorageAdapter | undefined {
80+
if (wrapper) {
81+
if (isStorageWrapperValid(wrapper)) return storageAdapter(log, prefix, wrapper);
8282
log.warn(LOG_PREFIX + 'Invalid storage provided. Falling back to LocalStorage API');
8383
}
8484

@@ -97,7 +97,7 @@ export function InLocalStorage(options: SplitIO.InLocalStorageOptions = {}): ISt
9797
function InLocalStorageCSFactory(params: IStorageFactoryParams): IStorageSync {
9898
const { settings, settings: { log, scheduler: { impressionsQueueSize, eventsQueueSize } } } = params;
9999

100-
const storage = validateStorage(log, prefix, options.storage);
100+
const storage = validateStorage(log, prefix, options.wrapper);
101101
if (!storage) return InMemoryStorageCSFactory(params);
102102

103103
const matchingKey = getMatching(settings.core.key);

src/utils/env/isLocalStorageAvailable.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
export function isLocalStorageAvailable(): boolean {
22
try {
33
// eslint-disable-next-line no-undef
4-
return isStorageValid(localStorage);
4+
return isStorageWrapperValid(localStorage);
55
} catch (e) {
66
return false;
77
}
88
}
99

10-
export function isStorageValid(storage: any): boolean {
10+
export function isStorageWrapperValid(wrapper: any): boolean {
1111
var mod = '__SPLITSOFTWARE__';
1212
try {
13-
storage.setItem(mod, mod);
14-
storage.getItem(mod);
15-
storage.removeItem(mod);
13+
wrapper.setItem(mod, mod);
14+
wrapper.getItem(mod);
15+
wrapper.removeItem(mod);
1616
return true;
1717
} catch (e) {
1818
return false;

types/splitio.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ interface IClientSideSyncSharedSettings extends IClientSideSharedSettings, ISync
449449
*/
450450
declare namespace SplitIO {
451451

452-
interface Storage {
452+
interface StorageWrapper {
453453
/**
454454
* Returns a promise that resolves to the current value associated with the given key, or null if the given key does not exist.
455455
*/
@@ -979,11 +979,11 @@ declare namespace SplitIO {
979979
*/
980980
clearOnInit?: boolean;
981981
/**
982-
* Optional storage API to persist rollout plan related data. If not provided, the SDK will use the default localStorage Web API.
982+
* Optional storage wrapper to persist rollout plan related data. If not provided, the SDK will use the default localStorage Web API.
983983
*
984984
* @defaultValue `window.localStorage`
985985
*/
986-
storage?: Storage;
986+
wrapper?: StorageWrapper;
987987
}
988988
/**
989989
* Storage for asynchronous (consumer) SDK.
@@ -1325,11 +1325,11 @@ declare namespace SplitIO {
13251325
*/
13261326
clearOnInit?: boolean;
13271327
/**
1328-
* Optional storage API to persist rollout plan related data. If not provided, the SDK will use the default localStorage Web API.
1328+
* Optional storage wrapper to persist rollout plan related data. If not provided, the SDK will use the default localStorage Web API.
13291329
*
13301330
* @defaultValue `window.localStorage`
13311331
*/
1332-
storage?: Storage;
1332+
wrapper?: StorageWrapper;
13331333
};
13341334
}
13351335
/**

0 commit comments

Comments
 (0)