Skip to content

Commit 5be8618

Browse files
committed
simplify session storage
1 parent 6caf55f commit 5be8618

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function init(config: IClientConfig): Promise<IEppoClient> {
7171
configurationRequestor,
7272
config.subjectAttributes,
7373
);
74-
if (!configurationStore.isSessionStorageInitialized()) {
74+
if (!configurationStore.isInitialized()) {
7575
await configurationRequestor.fetchAndStoreConfigurations();
7676
}
7777
return clientInstance;

src/storage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ describe('EppoSessionStorage', () => {
2727
});
2828

2929
it('returns stored entries', () => {
30-
expect(storage.isSessionStorageInitialized()).toEqual(false);
30+
expect(storage.isInitialized()).toEqual(false);
3131
storage.setEntries({ key1: config1, key2: config2 });
32-
expect(storage.isSessionStorageInitialized()).toEqual(true);
32+
expect(storage.isInitialized()).toEqual(true);
3333
expect(storage.get<ITestEntry>('key1')).toEqual(config1);
3434
expect(storage.get<ITestEntry>('key2')).toEqual(config2);
3535
});

src/storage.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,36 @@
11
const SESSION_STORAGE_INITIALIZED = 'eppo-session-storage-initialized';
22

33
export class EppoSessionStorage {
4-
// Fallback storage in case the user has disabled session storage in the browser.
5-
private fallbackStorage: Record<string, string> = {};
6-
74
public get<T>(key: string): T {
8-
let serializedEntry;
95
if (this.hasWindowSessionStorage()) {
10-
serializedEntry = window.sessionStorage.getItem(key);
11-
} else {
12-
serializedEntry = this.fallbackStorage[key];
13-
}
14-
if (serializedEntry) {
15-
return JSON.parse(serializedEntry);
6+
const serializedEntry = window.sessionStorage.getItem(key);
7+
if (serializedEntry) {
8+
return JSON.parse(serializedEntry);
9+
}
1610
}
1711
return null;
1812
}
1913

14+
// Checks whether session storage is enabled in the browser (the user might have disabled it).
2015
private hasWindowSessionStorage(): boolean {
2116
try {
2217
return typeof window !== 'undefined' && !!window.sessionStorage;
2318
} catch {
24-
// Some browsers throw an error if session storage is disabled and you try to access it
19+
// Chrome throws an error if session storage is disabled and you try to access it
2520
return false;
2621
}
2722
}
2823

29-
public isSessionStorageInitialized(): boolean {
24+
public isInitialized(): boolean {
3025
return !!this.get(SESSION_STORAGE_INITIALIZED);
3126
}
3227

3328
public setEntries<T>(entries: Record<string, T>) {
3429
if (this.hasWindowSessionStorage()) {
35-
this.setEntriesInSessionStorage(entries);
36-
} else {
37-
this.setEntriesInFallbackStorage(entries);
30+
Object.entries(entries).forEach(([key, val]) => {
31+
window.sessionStorage.setItem(key, JSON.stringify(val));
32+
});
33+
window.sessionStorage.setItem(SESSION_STORAGE_INITIALIZED, 'true');
3834
}
3935
}
40-
41-
private setEntriesInSessionStorage<T>(entries: Record<string, T>) {
42-
Object.entries(entries).forEach(([key, val]) => {
43-
window.sessionStorage.setItem(key, JSON.stringify(val));
44-
});
45-
window.sessionStorage.setItem(SESSION_STORAGE_INITIALIZED, 'true');
46-
}
47-
48-
private setEntriesInFallbackStorage<T>(entries: Record<string, T>) {
49-
Object.entries(entries).forEach(([key, val]) => {
50-
this.fallbackStorage[key] = JSON.stringify(val);
51-
});
52-
}
5336
}

0 commit comments

Comments
 (0)