@@ -17,9 +17,65 @@ import { getForwardingEventProcessor } from './forwarding_event_processor';
17
17
import { EventDispatcher } from './eventDispatcher' ;
18
18
import { EventProcessor } from './eventProcessor' ;
19
19
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' ;
20
24
21
25
export const createForwardingEventProcessor = (
22
26
eventDispatcher : EventDispatcher = defaultEventDispatcher ,
23
27
) : EventProcessor => {
24
28
return getForwardingEventProcessor ( eventDispatcher ) ;
25
29
} ;
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
+ } ;
0 commit comments