11/**
2- * Copyright 2016-2017, 2019-2022 Optimizely
2+ * Copyright 2016-2017, 2019-2022, 2023 Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -25,12 +25,13 @@ import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-pr
2525import configValidator from './utils/config_validator' ;
2626import defaultErrorHandler from './plugins/error_handler' ;
2727import defaultEventDispatcher from './plugins/event_dispatcher/index.browser' ;
28+ import sendBeaconEventDispatcher from './plugins/event_dispatcher/send_beacon_dispatcher' ;
2829import * as enums from './utils/enums' ;
2930import * as loggerPlugin from './plugins/logger' ;
3031import Optimizely from './optimizely' ;
3132import eventProcessorConfigValidator from './utils/event_processor_config_validator' ;
3233import { createNotificationCenter } from './core/notification_center' ;
33- import { default as eventProcessor } from './plugins/event_processor' ;
34+ import eventProcessor from './plugins/event_processor' ;
3435import { OptimizelyDecideOption , Client , Config } from './shared_types' ;
3536import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager' ;
3637
@@ -91,6 +92,13 @@ const createInstance = function(config: Config): Client | null {
9192 eventDispatcher = config . eventDispatcher ;
9293 }
9394
95+ let closingDispatcher = config . closingEventDispatcher ;
96+
97+ if ( ! config . eventDispatcher && ! closingDispatcher && window . navigator && 'sendBeacon' in window . navigator ) {
98+ closingDispatcher = sendBeaconEventDispatcher ;
99+ }
100+
101+
94102 let eventBatchSize = config . eventBatchSize ;
95103 let eventFlushInterval = config . eventFlushInterval ;
96104
@@ -112,6 +120,7 @@ const createInstance = function(config: Config): Client | null {
112120
113121 const eventProcessorConfig = {
114122 dispatcher : eventDispatcher ,
123+ closingDispatcher,
115124 flushInterval : eventFlushInterval ,
116125 batchSize : eventBatchSize ,
117126 maxQueueSize : config . eventMaxQueueSize || DEFAULT_EVENT_MAX_QUEUE_SIZE ,
@@ -164,6 +173,7 @@ export {
164173 loggerPlugin as logging ,
165174 defaultErrorHandler as errorHandler ,
166175 defaultEventDispatcher as eventDispatcher ,
176+ sendBeaconEventDispatcher ,
167177 enums ,
168178 setLogHandler as setLogger ,
169179 setLogLevel ,
@@ -176,6 +186,7 @@ export default {
176186 logging : loggerPlugin ,
177187 errorHandler : defaultErrorHandler ,
178188 eventDispatcher : defaultEventDispatcher ,
189+ sendBeaconEventDispatcher,
179190 enums,
180191 setLogger : setLogHandler ,
181192 setLogLevel,
0 commit comments