Skip to content

Commit 0c22f4d

Browse files
Merge branch 'inlocalstorage_storageAdapter' into inlocalstorage_sessionStorage
2 parents 09f15de + d04eb63 commit 0c22f4d

File tree

5 files changed

+22
-35
lines changed

5 files changed

+22
-35
lines changed

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.AsyncStorageWrapper {
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ILogger } from '../../logger/types';
1919
import SplitIO from '../../../types/splitio';
2020
import { storageAdapter } from './storageAdapter';
2121

22-
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.SyncStorageWrapper | SplitIO.AsyncStorageWrapper): StorageAdapter | undefined {
22+
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.StorageWrapper): StorageAdapter | undefined {
2323
if (wrapper) {
2424
if (isValidStorageWrapper(wrapper)) {
2525
return isWebStorage(wrapper) ?

src/storages/inLocalStorage/storageAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LOG_PREFIX } from './constants';
44
import { StorageAdapter } from '../types';
55

66

7-
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.SyncStorageWrapper | SplitIO.AsyncStorageWrapper): Required<StorageAdapter> {
7+
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): Required<StorageAdapter> {
88
let keys: string[] = [];
99
let cache: Record<string, string> = {};
1010

src/utils/env/isLocalStorageAvailable.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1+
/* eslint-disable no-undef */
12
export function isLocalStorageAvailable(): boolean {
3+
var mod = '__SPLITSOFTWARE__';
24
try {
3-
// eslint-disable-next-line no-undef
4-
return isValidStorageWrapper(localStorage);
5+
localStorage.setItem(mod, mod);
6+
localStorage.removeItem(mod);
7+
return true;
58
} catch (e) {
69
return false;
710
}
811
}
912

1013
export function isValidStorageWrapper(wrapper: any): boolean {
11-
var mod = '__SPLITSOFTWARE__';
12-
try {
13-
wrapper.setItem(mod, mod);
14-
wrapper.getItem(mod);
15-
wrapper.removeItem(mod);
16-
return true;
17-
} catch (e) {
18-
return false;
19-
}
14+
return wrapper !== null &&
15+
typeof wrapper === 'object' &&
16+
typeof wrapper.setItem === 'function' &&
17+
typeof wrapper.getItem === 'function' &&
18+
typeof wrapper.removeItem === 'function';
2019
}
2120

2221
export function isWebStorage(wrapper: any): boolean {

types/splitio.d.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -463,34 +463,22 @@ interface IClientSideSyncSharedSettings extends IClientSideSharedSettings, ISync
463463
*/
464464
declare namespace SplitIO {
465465

466-
interface SyncStorageWrapper {
466+
interface StorageWrapper {
467467
/**
468468
* Returns the value associated with the given key, or null if the key does not exist.
469+
* If the operation is asynchronous, returns a Promise.
469470
*/
470-
getItem(key: string): string | null;
471+
getItem(key: string): string | null | Promise<string | null>;
471472
/**
472473
* Sets the value for the given key, creating a new key/value pair if key does not exist.
474+
* If the operation is asynchronous, returns a Promise.
473475
*/
474-
setItem(key: string, value: string): void;
476+
setItem(key: string, value: string): void | Promise<void>;
475477
/**
476478
* Removes the key/value pair for the given key, if the key exists.
479+
* If the operation is asynchronous, returns a Promise.
477480
*/
478-
removeItem(key: string): void;
479-
}
480-
481-
interface AsyncStorageWrapper {
482-
/**
483-
* Returns a promise that resolves to the value associated with the given key, or null if the key does not exist.
484-
*/
485-
getItem(key: string): Promise<string | null>;
486-
/**
487-
* Returns a promise that resolves when the value of the pair identified by key is set to value, creating a new key/value pair if key does not exist.
488-
*/
489-
setItem(key: string, value: string): Promise<void>;
490-
/**
491-
* Returns a promise that resolves when the key/value pair for the given key is removed, if the key exists.
492-
*/
493-
removeItem(key: string): Promise<void>;
481+
removeItem(key: string): void | Promise<void>;
494482
}
495483

496484
/**
@@ -1013,7 +1001,7 @@ declare namespace SplitIO {
10131001
*
10141002
* @defaultValue `window.localStorage`
10151003
*/
1016-
wrapper?: SyncStorageWrapper | AsyncStorageWrapper;
1004+
wrapper?: StorageWrapper;
10171005
}
10181006
/**
10191007
* Storage for asynchronous (consumer) SDK.
@@ -1380,7 +1368,7 @@ declare namespace SplitIO {
13801368
*
13811369
* @defaultValue `window.localStorage`
13821370
*/
1383-
wrapper?: SyncStorageWrapper | AsyncStorageWrapper;
1371+
wrapper?: StorageWrapper;
13841372
};
13851373
}
13861374
/**

0 commit comments

Comments
 (0)