@@ -22,7 +22,7 @@ import { ERROR_MESSAGES, ODP_USER_KEY, ODP_DEFAULT_EVENT_TYPE, ODP_EVENT_ACTION
22
22
import { OdpEvent } from './odp_event' ;
23
23
import { OdpConfig } from './odp_config' ;
24
24
import { OdpEventApiManager } from './odp_event_api_manager' ;
25
- import { invalidOdpDataFound } from './odp_utils' ;
25
+ import { invalidOdpDataFound , isBrowserContext } from './odp_utils' ;
26
26
27
27
const MAX_RETRIES = 3 ;
28
28
const DEFAULT_BATCH_SIZE = 10 ;
@@ -54,6 +54,8 @@ export interface IOdpEventManager {
54
54
identifyUser ( userId : string , vuid ?: string ) : void ;
55
55
56
56
sendEvent ( event : OdpEvent ) : void ;
57
+
58
+ flush ( ) : void ;
57
59
}
58
60
59
61
/**
@@ -95,12 +97,12 @@ export class OdpEventManager implements IOdpEventManager {
95
97
*/
96
98
private readonly queueSize : number ;
97
99
/**
98
- * Maximum number of events to process at once
100
+ * Maximum number of events to process at once. Ignored in browser context
99
101
* @private
100
102
*/
101
103
private readonly batchSize : number ;
102
104
/**
103
- * Milliseconds between setTimeout() to process new batches
105
+ * Milliseconds between setTimeout() to process new batches. Ignored in browser context
104
106
* @private
105
107
*/
106
108
private readonly flushInterval : number ;
@@ -140,27 +142,29 @@ export class OdpEventManager implements IOdpEventManager {
140
142
this . clientEngine = clientEngine ;
141
143
this . clientVersion = clientVersion ;
142
144
143
- let defaultQueueSize = DEFAULT_BROWSER_QUEUE_SIZE ;
144
-
145
- try {
146
- // TODO: Consider refactoring to use typeof process and combine w/above line
147
- if ( process ) {
148
- defaultQueueSize = DEFAULT_SERVER_QUEUE_SIZE ;
149
- }
150
- } catch ( e ) {
151
- // TODO: Create Browser and Non-Browser specific variants of ODP Event Manager to avoid this try/catch
152
- }
145
+ const isBrowser = isBrowserContext ( ) ;
153
146
147
+ const defaultQueueSize = isBrowser ? DEFAULT_BROWSER_QUEUE_SIZE : DEFAULT_SERVER_QUEUE_SIZE ;
154
148
this . queueSize = queueSize || defaultQueueSize ;
155
149
this . batchSize = batchSize || DEFAULT_BATCH_SIZE ;
156
- if ( flushInterval === 0 ) {
150
+
151
+ if ( flushInterval === 0 || isBrowser ) {
157
152
// disable event batching
158
153
this . batchSize = 1 ;
159
154
this . flushInterval = 0 ;
160
155
} else {
161
156
this . flushInterval = flushInterval || DEFAULT_FLUSH_INTERVAL_MSECS ;
162
157
}
163
158
159
+ if ( isBrowser ) {
160
+ if ( typeof batchSize !== 'undefined' && batchSize !== 1 ) {
161
+ this . logger . log ( LogLevel . WARNING , 'ODP event batch size must be 1 in the browser.' ) ;
162
+ }
163
+ if ( typeof flushInterval !== 'undefined' && flushInterval !== 0 ) {
164
+ this . logger . log ( LogLevel . WARNING , 'ODP event flush interval must be 0 in the browser.' ) ;
165
+ }
166
+ }
167
+
164
168
this . state = STATE . STOPPED ;
165
169
}
166
170
@@ -398,17 +402,12 @@ export class OdpEventManager implements IOdpEventManager {
398
402
return true ;
399
403
}
400
404
401
- try {
402
- if ( process ) {
403
- // if Node/server-side context, empty queue items before ready state
404
- this . logger . log ( LogLevel . WARNING , 'ODPConfig not ready. Discarding events in queue.' ) ;
405
- this . queue = new Array < OdpEvent > ( ) ;
406
- } else {
407
- // in Browser/client-side context, give debug message but leave events in queue
408
- this . logger . log ( LogLevel . DEBUG , 'ODPConfig not ready. Leaving events in queue.' ) ;
409
- }
410
- } catch ( e ) {
411
- // TODO: Create Browser and Non-Browser specific variants of ODP Event Manager to avoid this try/catch
405
+ if ( ! isBrowserContext ( ) ) {
406
+ // if Node/server-side context, empty queue items before ready state
407
+ this . logger . log ( LogLevel . WARNING , 'ODPConfig not ready. Discarding events in queue.' ) ;
408
+ this . queue = new Array < OdpEvent > ( ) ;
409
+ } else {
410
+ // in Browser/client-side context, give debug message but leave events in queue
412
411
this . logger . log ( LogLevel . DEBUG , 'ODPConfig not ready. Leaving events in queue.' ) ;
413
412
}
414
413
0 commit comments