@@ -22,7 +22,7 @@ import { ERROR_MESSAGES, ODP_USER_KEY, ODP_DEFAULT_EVENT_TYPE, ODP_EVENT_ACTION
2222import { OdpEvent } from './odp_event' ;
2323import { OdpConfig } from './odp_config' ;
2424import { OdpEventApiManager } from './odp_event_api_manager' ;
25- import { invalidOdpDataFound } from './odp_utils' ;
25+ import { invalidOdpDataFound , isBrowserContext } from './odp_utils' ;
2626
2727const MAX_RETRIES = 3 ;
2828const DEFAULT_BATCH_SIZE = 10 ;
@@ -54,6 +54,8 @@ export interface IOdpEventManager {
5454 identifyUser ( userId : string , vuid ?: string ) : void ;
5555
5656 sendEvent ( event : OdpEvent ) : void ;
57+
58+ flush ( ) : void ;
5759}
5860
5961/**
@@ -95,12 +97,12 @@ export class OdpEventManager implements IOdpEventManager {
9597 */
9698 private readonly queueSize : number ;
9799 /**
98- * Maximum number of events to process at once
100+ * Maximum number of events to process at once. Ignored in browser context
99101 * @private
100102 */
101103 private readonly batchSize : number ;
102104 /**
103- * Milliseconds between setTimeout() to process new batches
105+ * Milliseconds between setTimeout() to process new batches. Ignored in browser context
104106 * @private
105107 */
106108 private readonly flushInterval : number ;
@@ -140,27 +142,29 @@ export class OdpEventManager implements IOdpEventManager {
140142 this . clientEngine = clientEngine ;
141143 this . clientVersion = clientVersion ;
142144
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 ( ) ;
153146
147+ const defaultQueueSize = isBrowser ? DEFAULT_BROWSER_QUEUE_SIZE : DEFAULT_SERVER_QUEUE_SIZE ;
154148 this . queueSize = queueSize || defaultQueueSize ;
155149 this . batchSize = batchSize || DEFAULT_BATCH_SIZE ;
156- if ( flushInterval === 0 ) {
150+
151+ if ( flushInterval === 0 || isBrowser ) {
157152 // disable event batching
158153 this . batchSize = 1 ;
159154 this . flushInterval = 0 ;
160155 } else {
161156 this . flushInterval = flushInterval || DEFAULT_FLUSH_INTERVAL_MSECS ;
162157 }
163158
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+
164168 this . state = STATE . STOPPED ;
165169 }
166170
@@ -398,17 +402,12 @@ export class OdpEventManager implements IOdpEventManager {
398402 return true ;
399403 }
400404
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
412411 this . logger . log ( LogLevel . DEBUG , 'ODPConfig not ready. Leaving events in queue.' ) ;
413412 }
414413
0 commit comments