Skip to content

Commit 7bb5c64

Browse files
committed
up
1 parent 851da33 commit 7bb5c64

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

lib/event_processor/event_processor_factory.browser.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ vi.mock('../utils/cache/cache', () => {
4545
import defaultEventDispatcher from './default_dispatcher.browser';
4646
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
4747
import { SyncPrefixCache } from '../utils/cache/cache';
48-
import { createForwardingEventProcessor, createBatchEventProcessor, EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory.browser';
48+
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.browser';
49+
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
4950
import sendBeaconEventDispatcher from '../plugins/event_dispatcher/send_beacon_dispatcher';
5051
import { getForwardingEventProcessor } from './forwarding_event_processor';
5152
import browserDefaultEventDispatcher from './default_dispatcher.browser';

lib/event_processor/event_processor_factory.browser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import defaultEventDispatcher from './default_dispatcher.browser';
2323
import sendBeaconEventDispatcher from '../plugins/event_dispatcher/send_beacon_dispatcher';
2424
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
2525
import { SyncPrefixCache } from '../utils/cache/cache';
26-
27-
export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
28-
export const EVENT_STORE_PREFIX = 'optly_event:';
26+
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
2927

3028
export const createForwardingEventProcessor = (
3129
eventDispatcher: EventDispatcher = defaultEventDispatcher,

lib/event_processor/event_processor_factory.react_native.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,65 @@ import { getForwardingEventProcessor } from './forwarding_event_processor';
1717
import { EventDispatcher } from './eventDispatcher';
1818
import { EventProcessor } from './eventProcessor';
1919
import defaultEventDispatcher from './default_dispatcher.browser';
20+
import { BatchEventProcessorOptions, getBatchEventProcessor } from './event_processor_factory';
21+
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
22+
import { AsyncPrefixCache, Cache, SyncPrefixCache } from '../utils/cache/cache';
23+
import { EventWithId } from './batch_event_processor';
2024

2125
export const createForwardingEventProcessor = (
2226
eventDispatcher: EventDispatcher = defaultEventDispatcher,
2327
): EventProcessor => {
2428
return getForwardingEventProcessor(eventDispatcher);
2529
};
30+
31+
const identity = <T>(v: T): T => v;
32+
33+
const getDefaultEventStore = () => {
34+
const AsyncStorageCache = require('../utils/cache/async_storage_cache.react_native').AsyncStorageCache;
35+
36+
const asyncStorageCache = new AsyncStorageCache();
37+
38+
const eventStore = new AsyncPrefixCache<EventWithId, EventWithId>(
39+
asyncStorageCache,
40+
EVENT_STORE_PREFIX,
41+
identity,
42+
identity,
43+
);
44+
45+
return eventStore;
46+
}
47+
48+
const getPrefixEventStore = (cache: Cache<string>): Cache<EventWithId> => {
49+
if (cache.operation === 'async') {
50+
return new AsyncPrefixCache<string, EventWithId>(
51+
cache,
52+
EVENT_STORE_PREFIX,
53+
JSON.parse,
54+
JSON.stringify,
55+
);
56+
} else {
57+
return new SyncPrefixCache<string, EventWithId>(
58+
cache,
59+
EVENT_STORE_PREFIX,
60+
JSON.parse,
61+
JSON.stringify,
62+
);
63+
}
64+
};
65+
66+
export const createBatchEventProcessor = (
67+
options: BatchEventProcessorOptions
68+
): EventProcessor => {
69+
const eventStore = options.eventStore ? getPrefixEventStore(options.eventStore) : getDefaultEventStore();
70+
71+
return getBatchEventProcessor({
72+
eventDispatcher: options.eventDispatcher || defaultEventDispatcher,
73+
flushInterval: options.flushInterval,
74+
batchSize: options.batchSize,
75+
retryOptions: {
76+
maxRetries: 5,
77+
},
78+
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
79+
eventStore,
80+
});
81+
};

lib/event_processor/event_processor_factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const DEFAULT_EVENT_FLUSH_INTERVAL = 1000;
1111
export const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000;
1212
export const DEFAULT_MIN_BACKOFF = 1000;
1313
export const DEFAULT_MAX_BACKOFF = 32000;
14+
export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
15+
export const EVENT_STORE_PREFIX = 'optly_event:';
1416

1517
export type BatchEventProcessorOptions = {
1618
eventDispatcher?: EventDispatcher;

0 commit comments

Comments
 (0)