@@ -4,7 +4,9 @@ import BatchEventProcessor from './batch-event-processor';
4
4
import BatchRetryManager from './batch-retry-manager' ;
5
5
import EventDelivery from './event-delivery' ;
6
6
import EventDispatcher , { Event } from './event-dispatcher' ;
7
+ import NamedEventQueue from './named-event-queue' ;
7
8
import NetworkStatusListener from './network-status-listener' ;
9
+ import SdkKeyDecoder from './sdk-key-decoder' ;
8
10
9
11
export type EventDispatcherConfig = {
10
12
// target url to deliver events to
@@ -19,6 +21,16 @@ export type EventDispatcherConfig = {
19
21
maxRetries ?: number ;
20
22
} ;
21
23
24
+ // TODO: Have more realistic default batch size based on average event payload size once we have
25
+ // more concrete data.
26
+ export const DEFAULT_EVENT_DISPATCHER_BATCH_SIZE = 100 ;
27
+ export const DEFAULT_EVENT_DISPATCHER_CONFIG : Omit < EventDispatcherConfig , 'ingestionUrl' > = {
28
+ deliveryIntervalMs : 10_000 ,
29
+ retryIntervalMs : 5_000 ,
30
+ maxRetryDelayMs : 30_000 ,
31
+ maxRetries : 3 ,
32
+ } ;
33
+
22
34
/**
23
35
* @internal
24
36
* An {@link EventDispatcher} that, given the provided config settings, delivers events in batches
@@ -37,6 +49,7 @@ export default class DefaultEventDispatcher implements EventDispatcher {
37
49
private readonly networkStatusListener : NetworkStatusListener ,
38
50
config : EventDispatcherConfig ,
39
51
) {
52
+ this . ensureConfigFields ( config ) ;
40
53
this . eventDelivery = new EventDelivery ( config . ingestionUrl ) ;
41
54
this . retryManager = new BatchRetryManager ( this . eventDelivery , {
42
55
retryIntervalMs : config . retryIntervalMs ,
@@ -94,4 +107,39 @@ export default class DefaultEventDispatcher implements EventDispatcher {
94
107
this . dispatchTimer = setTimeout ( ( ) => this . deliverNextBatch ( ) , this . deliveryIntervalMs ) ;
95
108
}
96
109
}
110
+
111
+ private ensureConfigFields ( config : EventDispatcherConfig ) {
112
+ if ( ! config . ingestionUrl ) {
113
+ throw new Error ( 'Missing required ingestionUrl in EventDispatcherConfig' ) ;
114
+ }
115
+ if ( ! config . deliveryIntervalMs ) {
116
+ throw new Error ( 'Missing required deliveryIntervalMs in EventDispatcherConfig' ) ;
117
+ }
118
+ if ( ! config . retryIntervalMs ) {
119
+ throw new Error ( 'Missing required retryIntervalMs in EventDispatcherConfig' ) ;
120
+ }
121
+ if ( ! config . maxRetryDelayMs ) {
122
+ throw new Error ( 'Missing required maxRetryDelayMs in EventDispatcherConfig' ) ;
123
+ }
124
+ }
125
+ }
126
+
127
+ /** Creates a new {@link DefaultEventDispatcher} with the provided configuration. */
128
+ export function newDefaultEventDispatcher (
129
+ eventQueue : NamedEventQueue < unknown > ,
130
+ networkStatusListener : NetworkStatusListener ,
131
+ sdkKey : string ,
132
+ batchSize : number = DEFAULT_EVENT_DISPATCHER_BATCH_SIZE ,
133
+ config : Omit < EventDispatcherConfig , 'ingestionUrl' > = DEFAULT_EVENT_DISPATCHER_CONFIG ,
134
+ ) : EventDispatcher {
135
+ const sdkKeyDecoder = new SdkKeyDecoder ( ) ;
136
+ const ingestionUrl = sdkKeyDecoder . decodeEventIngestionHostName ( sdkKey ) ;
137
+ if ( ! ingestionUrl ) {
138
+ throw new Error ( 'Unable to parse Event ingestion URL from SDK key' ) ;
139
+ }
140
+ return new DefaultEventDispatcher (
141
+ new BatchEventProcessor ( eventQueue , batchSize ) ,
142
+ networkStatusListener ,
143
+ { ...config , ingestionUrl } ,
144
+ ) ;
97
145
}
0 commit comments