Skip to content

Commit 05547df

Browse files
authored
[FSSDK-9621] add persistentCacheProvider option for instantiation (#914)
1 parent 7283019 commit 05547df

12 files changed

+600
-63
lines changed

lib/index.react_native.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const createInstance = function(config: Config): Client | null {
9393
batchSize: eventBatchSize,
9494
maxQueueSize: config.eventMaxQueueSize || DEFAULT_EVENT_MAX_QUEUE_SIZE,
9595
notificationCenter,
96+
peristentCacheProvider: config.persistentCacheProvider,
9697
};
9798

9899
const eventProcessor = createEventProcessor(eventProcessorConfig);
@@ -109,7 +110,13 @@ const createInstance = function(config: Config): Client | null {
109110
logger,
110111
errorHandler,
111112
datafileManager: config.sdkKey
112-
? createHttpPollingDatafileManager(config.sdkKey, logger, config.datafile, config.datafileOptions)
113+
? createHttpPollingDatafileManager(
114+
config.sdkKey,
115+
logger,
116+
config.datafile,
117+
config.datafileOptions,
118+
config.persistentCacheProvider,
119+
)
113120
: undefined,
114121
notificationCenter,
115122
isValidInstance: isValidInstance,

lib/modules/event_processor/reactNativeEventsStore.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { objectValues } from "../../utils/fns"
1919

2020
import { Synchronizer } from './synchronizer'
2121
import ReactNativeAsyncStorageCache from '../../plugins/key_value_cache/reactNativeAsyncStorageCache';
22+
import PersistentKeyValueCache from '../../plugins/key_value_cache/persistentKeyValueCache';
2223

2324
const logger = getLogger('ReactNativeEventsStore')
2425

@@ -29,11 +30,12 @@ export class ReactNativeEventsStore<T> {
2930
private maxSize: number
3031
private storeKey: string
3132
private synchronizer: Synchronizer = new Synchronizer()
32-
private cache: ReactNativeAsyncStorageCache = new ReactNativeAsyncStorageCache()
33+
private cache: PersistentKeyValueCache;
3334

34-
constructor(maxSize: number, storeKey: string) {
35+
constructor(maxSize: number, storeKey: string, cache?: PersistentKeyValueCache) {
3536
this.maxSize = maxSize
3637
this.storeKey = storeKey
38+
this.cache = cache || new ReactNativeAsyncStorageCache()
3739
}
3840

3941
public async set(key: string, event: T): Promise<string> {

lib/modules/event_processor/v1/v1EventProcessor.react_native.ts

+14-3
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.
@@ -45,6 +45,7 @@ import {
4545
EventDispatcher,
4646
EventDispatcherResponse,
4747
} from '../eventDispatcher'
48+
import { PersistentCacheProvider } from '../../../shared_types'
4849

4950
const logger = getLogger('ReactNativeEventProcessor')
5051

@@ -91,12 +92,14 @@ export class LogTierV1EventProcessor implements EventProcessor {
9192
batchSize = DEFAULT_BATCH_SIZE,
9293
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE,
9394
notificationCenter,
95+
persistentCacheProvider,
9496
}: {
9597
dispatcher: EventDispatcher
9698
flushInterval?: number
9799
batchSize?: number
98100
maxQueueSize?: number
99101
notificationCenter?: NotificationSender
102+
persistentCacheProvider?: PersistentCacheProvider
100103
}) {
101104
this.dispatcher = dispatcher
102105
this.notificationSender = notificationCenter
@@ -105,8 +108,16 @@ export class LogTierV1EventProcessor implements EventProcessor {
105108
flushInterval = validateAndGetFlushInterval(flushInterval)
106109
batchSize = validateAndGetBatchSize(batchSize)
107110
this.queue = getQueue(batchSize, flushInterval, areEventContextsEqual, this.drainQueue.bind(this))
108-
this.pendingEventsStore = new ReactNativeEventsStore(maxQueueSize, PENDING_EVENTS_STORE_KEY)
109-
this.eventBufferStore = new ReactNativeEventsStore(maxQueueSize, EVENT_BUFFER_STORE_KEY)
111+
this.pendingEventsStore = new ReactNativeEventsStore(
112+
maxQueueSize,
113+
PENDING_EVENTS_STORE_KEY,
114+
persistentCacheProvider && persistentCacheProvider(),
115+
);
116+
this.eventBufferStore = new ReactNativeEventsStore(
117+
maxQueueSize,
118+
EVENT_BUFFER_STORE_KEY,
119+
persistentCacheProvider && persistentCacheProvider(),
120+
)
110121
}
111122

112123
private async connectionListener(state: NetInfoState) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2021-2022, Optimizely
2+
* Copyright 2021-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,37 +13,42 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { LoggerFacade } from '../../modules/logging';
17-
import { HttpPollingDatafileManager } from '../../modules/datafile-manager/index.react_native';
18-
import { DatafileOptions, DatafileManagerConfig, DatafileManager } from '../../shared_types';
19-
import { toDatafile, tryCreatingProjectConfig } from '../../core/project_config';
20-
import fns from '../../utils/fns';
16+
import { LoggerFacade } from '../../modules/logging';
17+
import { HttpPollingDatafileManager } from '../../modules/datafile-manager/index.react_native';
18+
import { DatafileOptions, DatafileManager, PersistentCacheProvider } from '../../shared_types';
19+
import { DatafileManagerConfig } from '../../modules/datafile-manager/index.react_native';
20+
import { toDatafile, tryCreatingProjectConfig } from '../../core/project_config';
21+
import fns from '../../utils/fns';
2122

22-
export function createHttpPollingDatafileManager(
23-
sdkKey: string,
24-
logger: LoggerFacade,
25-
// TODO[OASIS-6649]: Don't use object type
26-
// eslint-disable-next-line @typescript-eslint/ban-types
27-
datafile?: string | object,
28-
datafileOptions?: DatafileOptions,
23+
export function createHttpPollingDatafileManager(
24+
sdkKey: string,
25+
logger: LoggerFacade,
26+
// TODO[OASIS-6649]: Don't use object type
27+
// eslint-disable-next-line @typescript-eslint/ban-types
28+
datafile?: string | object,
29+
datafileOptions?: DatafileOptions,
30+
persistentCacheProvider?: PersistentCacheProvider,
2931
): DatafileManager {
30-
const datafileManagerConfig: DatafileManagerConfig = { sdkKey };
31-
if (datafileOptions === undefined || (typeof datafileOptions === 'object' && datafileOptions !== null)) {
32-
fns.assign(datafileManagerConfig, datafileOptions);
33-
}
34-
if (datafile) {
35-
const { configObj, error } = tryCreatingProjectConfig({
36-
datafile: datafile,
37-
jsonSchemaValidator: undefined,
38-
logger: logger,
39-
});
40-
41-
if (error) {
42-
logger.error(error);
43-
}
44-
if (configObj) {
45-
datafileManagerConfig.datafile = toDatafile(configObj);
46-
}
47-
}
48-
return new HttpPollingDatafileManager(datafileManagerConfig);
49-
}
32+
const datafileManagerConfig: DatafileManagerConfig = { sdkKey };
33+
if (datafileOptions === undefined || (typeof datafileOptions === 'object' && datafileOptions !== null)) {
34+
fns.assign(datafileManagerConfig, datafileOptions);
35+
}
36+
if (datafile) {
37+
const { configObj, error } = tryCreatingProjectConfig({
38+
datafile: datafile,
39+
jsonSchemaValidator: undefined,
40+
logger: logger,
41+
});
42+
43+
if (error) {
44+
logger.error(error);
45+
}
46+
if (configObj) {
47+
datafileManagerConfig.datafile = toDatafile(configObj);
48+
}
49+
}
50+
if (persistentCacheProvider) {
51+
datafileManagerConfig.cache = persistentCacheProvider();
52+
}
53+
return new HttpPollingDatafileManager(datafileManagerConfig);
54+
}

lib/shared_types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2020-2023, Optimizely
2+
* Copyright 2020-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.
@@ -36,6 +36,7 @@ import { IOdpEventApiManager } from './core/odp/odp_event_api_manager';
3636
import { IOdpEventManager } from './core/odp/odp_event_manager';
3737
import { IOdpManager } from './core/odp/odp_manager';
3838
import { IUserAgentParser } from './core/odp/user_agent_parser';
39+
import PersistentCache from './plugins/key_value_cache/persistentKeyValueCache';
3940

4041
export interface BucketerParams {
4142
experimentId: string;
@@ -382,6 +383,8 @@ export interface TrackListenerPayload extends ListenerPayload {
382383
logEvent: Event;
383384
}
384385

386+
export type PersistentCacheProvider = () => PersistentCache;
387+
385388
/**
386389
* Entry level Config Entities
387390
* For compatibility with the previous declaration file
@@ -393,6 +396,7 @@ export interface Config extends ConfigLite {
393396
eventMaxQueueSize?: number; // Maximum size for the event queue
394397
sdkKey?: string;
395398
odpOptions?: OdpOptions;
399+
persistentCacheProvider?: PersistentCacheProvider;
396400
}
397401

398402
/**

package-lock.json

+17-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"@rollup/plugin-commonjs": "^11.0.2",
117117
"@rollup/plugin-node-resolve": "^7.1.1",
118118
"@types/chai": "^4.2.11",
119-
"@types/jest": "^23.3.14",
119+
"@types/jest": "^29.5.12",
120120
"@types/mocha": "^5.2.7",
121121
"@types/nise": "^1.4.0",
122122
"@types/node": "^18.7.18",

0 commit comments

Comments
 (0)