Skip to content

[FSSDK-9621] add persistentCacheProvider option for instantiation #914

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 7 commits into from
Mar 18, 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
9 changes: 8 additions & 1 deletion lib/index.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const createInstance = function(config: Config): Client | null {
batchSize: eventBatchSize,
maxQueueSize: config.eventMaxQueueSize || DEFAULT_EVENT_MAX_QUEUE_SIZE,
notificationCenter,
peristentCacheProvider: config.persistentCacheProvider,
};

const eventProcessor = createEventProcessor(eventProcessorConfig);
Expand All @@ -109,7 +110,13 @@ const createInstance = function(config: Config): Client | null {
logger,
errorHandler,
datafileManager: config.sdkKey
? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions)
? createHttpPollingDatafileManager(
config.sdkKey,
logger,
config.datafile,
config.datafileOptions,
config.persistentCacheProvider,
)
: undefined,
notificationCenter,
isValidInstance: isValidInstance,
Expand Down
6 changes: 4 additions & 2 deletions lib/modules/event_processor/reactNativeEventsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { objectValues } from "../../utils/fns"

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

const logger = getLogger('ReactNativeEventsStore')

Expand All @@ -29,11 +30,12 @@ export class ReactNativeEventsStore<T> {
private maxSize: number
private storeKey: string
private synchronizer: Synchronizer = new Synchronizer()
private cache: ReactNativeAsyncStorageCache = new ReactNativeAsyncStorageCache()
private cache: PersistentKeyValueCache;

constructor(maxSize: number, storeKey: string) {
constructor(maxSize: number, storeKey: string, cache?: PersistentKeyValueCache) {
this.maxSize = maxSize
this.storeKey = storeKey
this.cache = cache || new ReactNativeAsyncStorageCache()
}

public async set(key: string, event: T): Promise<string> {
Expand Down
17 changes: 14 additions & 3 deletions lib/modules/event_processor/v1/v1EventProcessor.react_native.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 @@ -45,6 +45,7 @@ import {
EventDispatcher,
EventDispatcherResponse,
} from '../eventDispatcher'
import { PersistentCacheProvider } from '../../../shared_types'

const logger = getLogger('ReactNativeEventProcessor')

Expand Down Expand Up @@ -91,12 +92,14 @@ export class LogTierV1EventProcessor implements EventProcessor {
batchSize = DEFAULT_BATCH_SIZE,
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE,
notificationCenter,
persistentCacheProvider,
}: {
dispatcher: EventDispatcher
flushInterval?: number
batchSize?: number
maxQueueSize?: number
notificationCenter?: NotificationSender
persistentCacheProvider?: PersistentCacheProvider
}) {
this.dispatcher = dispatcher
this.notificationSender = notificationCenter
Expand All @@ -105,8 +108,16 @@ export class LogTierV1EventProcessor implements EventProcessor {
flushInterval = validateAndGetFlushInterval(flushInterval)
batchSize = validateAndGetBatchSize(batchSize)
this.queue = getQueue(batchSize, flushInterval, areEventContextsEqual, this.drainQueue.bind(this))
this.pendingEventsStore = new ReactNativeEventsStore(maxQueueSize, PENDING_EVENTS_STORE_KEY)
this.eventBufferStore = new ReactNativeEventsStore(maxQueueSize, EVENT_BUFFER_STORE_KEY)
this.pendingEventsStore = new ReactNativeEventsStore(
maxQueueSize,
PENDING_EVENTS_STORE_KEY,
persistentCacheProvider && persistentCacheProvider(),
);
this.eventBufferStore = new ReactNativeEventsStore(
maxQueueSize,
EVENT_BUFFER_STORE_KEY,
persistentCacheProvider && persistentCacheProvider(),
)
}

private async connectionListener(state: NetInfoState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2021-2022, Optimizely
* Copyright 2021-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,37 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LoggerFacade } from '../../modules/logging';
import { HttpPollingDatafileManager } from '../../modules/datafile-manager/index.react_native';
import { DatafileOptions, DatafileManagerConfig, DatafileManager } from '../../shared_types';
import { toDatafile, tryCreatingProjectConfig } from '../../core/project_config';
import fns from '../../utils/fns';
import { LoggerFacade } from '../../modules/logging';
import { HttpPollingDatafileManager } from '../../modules/datafile-manager/index.react_native';
import { DatafileOptions, DatafileManager, PersistentCacheProvider } from '../../shared_types';
import { DatafileManagerConfig } from '../../modules/datafile-manager/index.react_native';
import { toDatafile, tryCreatingProjectConfig } from '../../core/project_config';
import fns from '../../utils/fns';

export function createHttpPollingDatafileManager(
sdkKey: string,
logger: LoggerFacade,
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
datafile?: string | object,
datafileOptions?: DatafileOptions,
export function createHttpPollingDatafileManager(
sdkKey: string,
logger: LoggerFacade,
// TODO[OASIS-6649]: Don't use object type
// eslint-disable-next-line @typescript-eslint/ban-types
datafile?: string | object,
datafileOptions?: DatafileOptions,
persistentCacheProvider?: PersistentCacheProvider,
): DatafileManager {
const datafileManagerConfig: DatafileManagerConfig = { sdkKey };
if (datafileOptions === undefined || (typeof datafileOptions === 'object' && datafileOptions !== null)) {
fns.assign(datafileManagerConfig, datafileOptions);
}
if (datafile) {
const { configObj, error } = tryCreatingProjectConfig({
datafile: datafile,
jsonSchemaValidator: undefined,
logger: logger,
});

if (error) {
logger.error(error);
}
if (configObj) {
datafileManagerConfig.datafile = toDatafile(configObj);
}
}
return new HttpPollingDatafileManager(datafileManagerConfig);
}
const datafileManagerConfig: DatafileManagerConfig = { sdkKey };
if (datafileOptions === undefined || (typeof datafileOptions === 'object' && datafileOptions !== null)) {
fns.assign(datafileManagerConfig, datafileOptions);
}
if (datafile) {
const { configObj, error } = tryCreatingProjectConfig({
datafile: datafile,
jsonSchemaValidator: undefined,
logger: logger,
});

if (error) {
logger.error(error);
}
if (configObj) {
datafileManagerConfig.datafile = toDatafile(configObj);
}
}
if (persistentCacheProvider) {
datafileManagerConfig.cache = persistentCacheProvider();
}
return new HttpPollingDatafileManager(datafileManagerConfig);
}
6 changes: 5 additions & 1 deletion lib/shared_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2020-2023, Optimizely
* Copyright 2020-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 @@ -36,6 +36,7 @@ import { IOdpEventApiManager } from './core/odp/odp_event_api_manager';
import { IOdpEventManager } from './core/odp/odp_event_manager';
import { IOdpManager } from './core/odp/odp_manager';
import { IUserAgentParser } from './core/odp/user_agent_parser';
import PersistentCache from './plugins/key_value_cache/persistentKeyValueCache';

export interface BucketerParams {
experimentId: string;
Expand Down Expand Up @@ -382,6 +383,8 @@ export interface TrackListenerPayload extends ListenerPayload {
logEvent: Event;
}

export type PersistentCacheProvider = () => PersistentCache;

/**
* Entry level Config Entities
* For compatibility with the previous declaration file
Expand All @@ -393,6 +396,7 @@ export interface Config extends ConfigLite {
eventMaxQueueSize?: number; // Maximum size for the event queue
sdkKey?: string;
odpOptions?: OdpOptions;
persistentCacheProvider?: PersistentCacheProvider;
}

/**
Expand Down
26 changes: 17 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/chai": "^4.2.11",
"@types/jest": "^23.3.14",
"@types/jest": "^29.5.12",
"@types/mocha": "^5.2.7",
"@types/nise": "^1.4.0",
"@types/node": "^18.7.18",
Expand Down
Loading