Skip to content

[FSSDK-9621] remove duplicate reactNativeAsyncStorageCache #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/modules/datafile-manager/datafileManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022-2023, Optimizely
* Copyright 2022-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import PersistentKeyValueCache from './persistentKeyValueCache';
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';

export interface DatafileUpdate {
datafile: string;
Expand Down
12 changes: 6 additions & 6 deletions lib/modules/datafile-manager/httpPollingDatafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import EventEmitter, { Disposer } from './eventEmitter';
import { AbortableRequest, Response, Headers } from './http';
import { DEFAULT_UPDATE_INTERVAL, MIN_UPDATE_INTERVAL, DEFAULT_URL_TEMPLATE, UPDATE_INTERVAL_BELOW_MINIMUM_MESSAGE } from './config';
import BackoffController from './backoffController';
import PersistentKeyValueCache from './persistentKeyValueCache';
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';

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

const noOpKeyValueCache: PersistentKeyValueCache = {
get(): Promise<string> {
return Promise.resolve('');
get(): Promise<string | undefined> {
return Promise.resolve(undefined);
},

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

remove(): Promise<void> {
return Promise.resolve();
remove(): Promise<boolean> {
return Promise.resolve(false);
},
};

Expand Down Expand Up @@ -339,7 +339,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana

setDatafileFromCacheIfAvailable(): void {
this.cache.get(this.cacheKey).then(datafile => {
if (this.isStarted && !this.isReadyPromiseSettled && datafile !== '') {
if (this.isStarted && !this.isReadyPromiseSettled && datafile) {
logger.debug('Using datafile from cache');
this.currentDatafile = datafile;
this.resolveReadyPromise();
Expand Down
59 changes: 0 additions & 59 deletions lib/modules/datafile-manager/persistentKeyValueCache.ts

This file was deleted.

40 changes: 0 additions & 40 deletions lib/modules/datafile-manager/reactNativeAsyncStorageCache.ts

This file was deleted.

4 changes: 2 additions & 2 deletions lib/modules/datafile-manager/reactNativeDatafileManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022, Optimizely
* Copyright 2022, 2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@ import { makeGetRequest } from './browserRequest';
import HttpPollingDatafileManager from './httpPollingDatafileManager';
import { Headers, AbortableRequest } from './http';
import { DatafileManagerConfig } from './datafileManager';
import ReactNativeAsyncStorageCache from './reactNativeAsyncStorageCache';
import ReactNativeAsyncStorageCache from '../../plugins/key_value_cache/reactNativeAsyncStorageCache';

export default class ReactNativeDatafileManager extends HttpPollingDatafileManager {
protected makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest {
Expand Down
60 changes: 0 additions & 60 deletions lib/modules/event_processor/persistentKeyValueCache.ts

This file was deleted.

47 changes: 0 additions & 47 deletions lib/modules/event_processor/reactNativeAsyncStorageCache.ts

This file was deleted.

19 changes: 10 additions & 9 deletions lib/modules/event_processor/reactNativeEventsStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/**
* Copyright 2022, Optimizely
* Copyright 2022, 2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@ import { getLogger } from '../logging'
import { objectValues } from "../../utils/fns"

import { Synchronizer } from './synchronizer'
import ReactNativeAsyncStorageCache from './reactNativeAsyncStorageCache'
import ReactNativeAsyncStorageCache from '../../plugins/key_value_cache/reactNativeAsyncStorageCache';

const logger = getLogger('ReactNativeEventsStore')

Expand All @@ -38,10 +38,10 @@ export class ReactNativeEventsStore<T> {

public async set(key: string, event: T): Promise<string> {
await this.synchronizer.getLock()
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
const eventsMap: {[key: string]: T} = await this.getEventsMap();
if (Object.keys(eventsMap).length < this.maxSize) {
eventsMap[key] = event
await this.cache.set(this.storeKey, eventsMap)
await this.cache.set(this.storeKey, JSON.stringify(eventsMap))
} else {
logger.warn('React native events store is full. Store key: %s', this.storeKey)
}
Expand All @@ -51,27 +51,28 @@ export class ReactNativeEventsStore<T> {

public async get(key: string): Promise<T> {
await this.synchronizer.getLock()
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
const eventsMap: {[key: string]: T} = await this.getEventsMap()
this.synchronizer.releaseLock()
return eventsMap[key]
}

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

public async getEventsList(): Promise<T[]> {
await this.synchronizer.getLock()
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
const eventsMap: {[key: string]: T} = await this.getEventsMap()
this.synchronizer.releaseLock()
return objectValues(eventsMap)
}

public async remove(key: string): Promise<void> {
await this.synchronizer.getLock()
const eventsMap: {[key: string]: T} = await this.cache.get(this.storeKey) || {}
const eventsMap: {[key: string]: T} = await this.getEventsMap()
eventsMap[key] && delete eventsMap[key]
await this.cache.set(this.storeKey, eventsMap)
await this.cache.set(this.storeKey, JSON.stringify(eventsMap))
this.synchronizer.releaseLock()
}

Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/key_value_cache/browserAsyncStorageCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022-2023, Optimizely
* Copyright 2022-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,14 +34,14 @@ export default class BrowserAsyncStorageCache implements PersistentKeyValueCache
});
}

async get(key: string): Promise<string | null | undefined> {
return tryWithLocalStorage<string | null | undefined>({
async get(key: string): Promise<string | undefined> {
return tryWithLocalStorage<string | undefined>({
browserCallback: (localStorage?: Storage) => {
return localStorage?.getItem(key);
return (localStorage?.getItem(key) || undefined);
},
nonBrowserCallback: () => {
this.logger.error(ERROR_MESSAGES.LOCAL_STORAGE_DOES_NOT_EXIST);
return null;
return undefined;
},
});
}
Expand Down
Loading