Skip to content

Commit 60aa25d

Browse files
refactor: move storageAdapter to dedicated file and update imports
1 parent 47a8cc2 commit 60aa25d

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

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

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

src/storages/inLocalStorage/index.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,7 @@ import { getMatching } from '../../utils/key';
1717
import { validateCache } from './validateCache';
1818
import { ILogger } from '../../logger/types';
1919
import SplitIO from '../../../types/splitio';
20-
21-
function isTillKey(key: string) {
22-
return key.endsWith('.till');
23-
}
24-
25-
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): StorageAdapter {
26-
let cache: Record<string, string> = {};
27-
28-
let connectPromise: Promise<void> | undefined;
29-
let disconnectPromise = Promise.resolve();
30-
31-
return {
32-
load() {
33-
return connectPromise || (connectPromise = Promise.resolve(wrapper.getItem(prefix)).then((storedCache) => {
34-
cache = JSON.parse(storedCache || '{}');
35-
}).catch((e) => {
36-
log.error(LOG_PREFIX + 'Rejected promise calling storage getItem, with error: ' + e);
37-
}));
38-
},
39-
save() {
40-
return disconnectPromise = disconnectPromise.then(() => {
41-
return Promise.resolve(wrapper.setItem(prefix, JSON.stringify(cache))).catch((e) => {
42-
log.error(LOG_PREFIX + 'Rejected promise calling storage setItem, with error: ' + e);
43-
});
44-
});
45-
},
46-
47-
get length() {
48-
return Object.keys(cache).length;
49-
},
50-
getItem(key: string) {
51-
return cache[key] || null;
52-
},
53-
key(index: number) {
54-
return Object.keys(cache)[index] || null;
55-
},
56-
removeItem(key: string) {
57-
delete cache[key];
58-
if (isTillKey(key)) this.save!();
59-
},
60-
setItem(key: string, value: string) {
61-
cache[key] = value;
62-
if (isTillKey(key)) this.save!();
63-
}
64-
};
65-
}
20+
import { storageAdapter } from './storageAdapter';
6621

6722
function validateStorage(log: ILogger, prefix: string, wrapper?: SplitIO.StorageWrapper): StorageAdapter | undefined {
6823
if (wrapper) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { ILogger } from '../../logger/types';
2+
import SplitIO from '../../../types/splitio';
3+
import { LOG_PREFIX } from './constants';
4+
import { StorageAdapter } from '../types';
5+
6+
function isTillKey(key: string) {
7+
return key.endsWith('.till');
8+
}
9+
10+
export function storageAdapter(log: ILogger, prefix: string, wrapper: SplitIO.StorageWrapper): StorageAdapter {
11+
let cache: Record<string, string> = {};
12+
13+
let connectPromise: Promise<void> | undefined;
14+
let disconnectPromise = Promise.resolve();
15+
16+
return {
17+
load() {
18+
return connectPromise || (connectPromise = Promise.resolve(wrapper.getItem(prefix)).then((storedCache) => {
19+
cache = JSON.parse(storedCache || '{}');
20+
}).catch((e) => {
21+
log.error(LOG_PREFIX + 'Rejected promise calling storage getItem, with error: ' + e);
22+
}));
23+
},
24+
save() {
25+
return disconnectPromise = disconnectPromise.then(() => {
26+
return Promise.resolve(wrapper.setItem(prefix, JSON.stringify(cache))).catch((e) => {
27+
log.error(LOG_PREFIX + 'Rejected promise calling storage setItem, with error: ' + e);
28+
});
29+
});
30+
},
31+
32+
get length() {
33+
return Object.keys(cache).length;
34+
},
35+
getItem(key: string) {
36+
return cache[key] || null;
37+
},
38+
key(index: number) {
39+
return Object.keys(cache)[index] || null;
40+
},
41+
removeItem(key: string) {
42+
delete cache[key];
43+
if (isTillKey(key)) this.save!();
44+
},
45+
setItem(key: string, value: string) {
46+
cache[key] = value;
47+
if (isTillKey(key)) this.save!();
48+
}
49+
};
50+
}

0 commit comments

Comments
 (0)