Skip to content

Commit edd8285

Browse files
committed
[FSSDK-9621] remove duplicate reactNativeAsyncStorageCache imlementations and refactor
1 parent a1658d0 commit edd8285

15 files changed

+39
-245
lines changed

lib/modules/datafile-manager/datafileManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import PersistentKeyValueCache from './persistentKeyValueCache';
16+
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';
1717

1818
export interface DatafileUpdate {
1919
datafile: string;

lib/modules/datafile-manager/httpPollingDatafileManager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import EventEmitter, { Disposer } from './eventEmitter';
2121
import { AbortableRequest, Response, Headers } from './http';
2222
import { DEFAULT_UPDATE_INTERVAL, MIN_UPDATE_INTERVAL, DEFAULT_URL_TEMPLATE, UPDATE_INTERVAL_BELOW_MINIMUM_MESSAGE } from './config';
2323
import BackoffController from './backoffController';
24-
import PersistentKeyValueCache from './persistentKeyValueCache';
24+
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';
2525

2626
import { NotificationRegistry } from './../../core/notification_center/notification_registry';
2727
import { NOTIFICATION_TYPES } from '../../utils/enums';
@@ -35,8 +35,8 @@ function isSuccessStatusCode(statusCode: number): boolean {
3535
}
3636

3737
const noOpKeyValueCache: PersistentKeyValueCache = {
38-
get(): Promise<string> {
39-
return Promise.resolve('');
38+
get(): Promise<string | undefined> {
39+
return Promise.resolve(undefined);
4040
},
4141

4242
set(): Promise<void> {
@@ -47,8 +47,8 @@ const noOpKeyValueCache: PersistentKeyValueCache = {
4747
return Promise.resolve(false);
4848
},
4949

50-
remove(): Promise<void> {
51-
return Promise.resolve();
50+
remove(): Promise<boolean> {
51+
return Promise.resolve(false);
5252
},
5353
};
5454

@@ -339,7 +339,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
339339

340340
setDatafileFromCacheIfAvailable(): void {
341341
this.cache.get(this.cacheKey).then(datafile => {
342-
if (this.isStarted && !this.isReadyPromiseSettled && datafile !== '') {
342+
if (this.isStarted && !this.isReadyPromiseSettled && datafile) {
343343
logger.debug('Using datafile from cache');
344344
this.currentDatafile = datafile;
345345
this.resolveReadyPromise();

lib/modules/datafile-manager/persistentKeyValueCache.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/modules/datafile-manager/reactNativeAsyncStorageCache.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

lib/modules/datafile-manager/reactNativeDatafileManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { makeGetRequest } from './browserRequest';
1818
import HttpPollingDatafileManager from './httpPollingDatafileManager';
1919
import { Headers, AbortableRequest } from './http';
2020
import { DatafileManagerConfig } from './datafileManager';
21-
import ReactNativeAsyncStorageCache from './reactNativeAsyncStorageCache';
21+
import ReactNativeAsyncStorageCache from '../../plugins/key_value_cache/reactNativeAsyncStorageCache';
2222

2323
export default class ReactNativeDatafileManager extends HttpPollingDatafileManager {
2424
protected makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest {

lib/modules/event_processor/persistentKeyValueCache.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/modules/event_processor/reactNativeAsyncStorageCache.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

lib/modules/event_processor/reactNativeEventsStore.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { getLogger } from '../logging'
1818
import { objectValues } from "../../utils/fns"
1919

2020
import { Synchronizer } from './synchronizer'
21-
import ReactNativeAsyncStorageCache from './reactNativeAsyncStorageCache'
21+
import ReactNativeAsyncStorageCache from '../../plugins/key_value_cache/reactNativeAsyncStorageCache';
2222

2323
const logger = getLogger('ReactNativeEventsStore')
2424

@@ -38,10 +38,11 @@ export class ReactNativeEventsStore<T> {
3838

3939
public async set(key: string, event: T): Promise<string> {
4040
await this.synchronizer.getLock()
41-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
41+
const eventsMap: {[key: string]: T} = await this.getEventsMap();
42+
// const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
4243
if (Object.keys(eventsMap).length < this.maxSize) {
4344
eventsMap[key] = event
44-
await this.cache.set(this.storeKey, eventsMap)
45+
await this.cache.set(this.storeKey, JSON.stringify(eventsMap))
4546
} else {
4647
logger.warn('React native events store is full. Store key: %s', this.storeKey)
4748
}
@@ -51,27 +52,28 @@ export class ReactNativeEventsStore<T> {
5152

5253
public async get(key: string): Promise<T> {
5354
await this.synchronizer.getLock()
54-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
55+
const eventsMap: {[key: string]: T} = await this.getEventsMap()
5556
this.synchronizer.releaseLock()
5657
return eventsMap[key]
5758
}
5859

5960
public async getEventsMap(): Promise<{[key: string]: T}> {
60-
return await this.cache.get(this.storeKey) || {}
61+
const cachedValue = await this.cache.get(this.storeKey) || '{}';
62+
return JSON.parse(cachedValue)
6163
}
6264

6365
public async getEventsList(): Promise<T[]> {
6466
await this.synchronizer.getLock()
65-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
67+
const eventsMap: {[key: string]: T} = await this.getEventsMap()
6668
this.synchronizer.releaseLock()
6769
return objectValues(eventsMap)
6870
}
6971

7072
public async remove(key: string): Promise<void> {
7173
await this.synchronizer.getLock()
72-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
74+
const eventsMap: {[key: string]: T} = await this.getEventsMap()
7375
eventsMap[key] && delete eventsMap[key]
74-
await this.cache.set(this.storeKey, eventsMap)
76+
await this.cache.set(this.storeKey, JSON.stringify(eventsMap))
7577
this.synchronizer.releaseLock()
7678
}
7779

lib/plugins/key_value_cache/browserAsyncStorageCache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ export default class BrowserAsyncStorageCache implements PersistentKeyValueCache
3434
});
3535
}
3636

37-
async get(key: string): Promise<string | null | undefined> {
38-
return tryWithLocalStorage<string | null | undefined>({
37+
async get(key: string): Promise<string | undefined> {
38+
return tryWithLocalStorage<string | undefined>({
3939
browserCallback: (localStorage?: Storage) => {
40-
return localStorage?.getItem(key);
40+
return (localStorage?.getItem(key) || undefined);
4141
},
4242
nonBrowserCallback: () => {
4343
this.logger.error(ERROR_MESSAGES.LOCAL_STORAGE_DOES_NOT_EXIST);
44-
return null;
44+
return undefined;
4545
},
4646
});
4747
}

lib/plugins/key_value_cache/persistentKeyValueCache.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
/**
18-
* An Interface to implement a persistent key value cache which supports strings as keys and value objects.
18+
* An Interface to implement a persistent key value cache which supports strings as keys.
1919
*/
20-
export default interface PersistentKeyValueCache {
20+
export default interface PersistentKeyValueCache<V = string> {
2121
/**
2222
* Checks if a key exists in the cache
2323
* @param key
@@ -37,8 +37,7 @@ export default interface PersistentKeyValueCache {
3737
* 2. undefined if the key does not exist in the cache.
3838
* Rejects the promise in case of an error
3939
*/
40-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41-
get(key: string): Promise<any>;
40+
get(key: string): Promise<V | undefined>;
4241

4342
/**
4443
* Removes the key value pair from cache.
@@ -59,6 +58,5 @@ export default interface PersistentKeyValueCache {
5958
* Resolves promise without a value if successful
6059
* Rejects the promise in case of an error
6160
*/
62-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
63-
set(key: string, val: any): Promise<void>;
61+
set(key: string, val: V): Promise<void>;
6462
}

lib/plugins/key_value_cache/reactNativeAsyncStorageCache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export default class ReactNativeAsyncStorageCache implements PersistentKeyValueC
2222
return await AsyncStorage.getItem(key) !== null;
2323
}
2424

25-
async get(key: string): Promise<string | null> {
26-
return await AsyncStorage.getItem(key);
25+
async get(key: string): Promise<string | undefined> {
26+
return (await AsyncStorage.getItem(key) || undefined);
2727
}
2828

2929
async remove(key: string): Promise<boolean> {

tests/browserAsyncStorageCache.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ describe('BrowserAsyncStorageCache', () => {
6363
expect(foundValue).toEqual(VALUE_FOR_KEY_THAT_EXISTS);
6464
});
6565

66-
it('should return empty string if item is not found in cache', async () => {
66+
it('should return undefined if item is not found in cache', async () => {
6767
const json = await cacheInstance.get(NONEXISTENT_KEY);
6868

69-
expect(json).toBeNull();
69+
expect(json).toBeUndefined();
7070
});
7171
});
7272

0 commit comments

Comments
 (0)