Skip to content

Commit 7283019

Browse files
authored
[FSSDK-9621] remove duplicate reactNativeAsyncStorageCache (#913)
1 parent a1658d0 commit 7283019

15 files changed

+47
-254
lines changed

lib/modules/datafile-manager/datafileManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022-2023, Optimizely
2+
* Copyright 2022-2024, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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

+6-6
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

-59
This file was deleted.

lib/modules/datafile-manager/reactNativeAsyncStorageCache.ts

-40
This file was deleted.

lib/modules/datafile-manager/reactNativeDatafileManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Optimizely
2+
* Copyright 2022, 2024, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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

-60
This file was deleted.

lib/modules/event_processor/reactNativeAsyncStorageCache.ts

-47
This file was deleted.

lib/modules/event_processor/reactNativeEventsStore.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/**
3-
* Copyright 2022, Optimizely
3+
* Copyright 2022, 2024, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -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,10 @@ 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();
4242
if (Object.keys(eventsMap).length < this.maxSize) {
4343
eventsMap[key] = event
44-
await this.cache.set(this.storeKey, eventsMap)
44+
await this.cache.set(this.storeKey, JSON.stringify(eventsMap))
4545
} else {
4646
logger.warn('React native events store is full. Store key: %s', this.storeKey)
4747
}
@@ -51,27 +51,28 @@ export class ReactNativeEventsStore<T> {
5151

5252
public async get(key: string): Promise<T> {
5353
await this.synchronizer.getLock()
54-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
54+
const eventsMap: {[key: string]: T} = await this.getEventsMap()
5555
this.synchronizer.releaseLock()
5656
return eventsMap[key]
5757
}
5858

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

6364
public async getEventsList(): Promise<T[]> {
6465
await this.synchronizer.getLock()
65-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
66+
const eventsMap: {[key: string]: T} = await this.getEventsMap()
6667
this.synchronizer.releaseLock()
6768
return objectValues(eventsMap)
6869
}
6970

7071
public async remove(key: string): Promise<void> {
7172
await this.synchronizer.getLock()
72-
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
73+
const eventsMap: {[key: string]: T} = await this.getEventsMap()
7374
eventsMap[key] && delete eventsMap[key]
74-
await this.cache.set(this.storeKey, eventsMap)
75+
await this.cache.set(this.storeKey, JSON.stringify(eventsMap))
7576
this.synchronizer.releaseLock()
7677
}
7778

lib/plugins/key_value_cache/browserAsyncStorageCache.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022-2023, Optimizely
2+
* Copyright 2022-2024, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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
}

0 commit comments

Comments
 (0)