-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathindex.browser.ts
More file actions
222 lines (194 loc) · 7.5 KB
/
index.browser.ts
File metadata and controls
222 lines (194 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Copyright 2016-2017, 2019-2024 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import logHelper from './modules/logging/logger';
import { getLogger, setErrorHandler, getErrorHandler, LogLevel } from './modules/logging';
import configValidator from './utils/config_validator';
import defaultErrorHandler from './plugins/error_handler';
import defaultEventDispatcher from './event_processor/event_dispatcher/default_dispatcher.browser';
import sendBeaconEventDispatcher from './event_processor/event_dispatcher/send_beacon_dispatcher.browser';
import * as enums from './utils/enums';
import * as loggerPlugin from './plugins/logger';
import { createNotificationCenter } from './notification_center';
import { OptimizelyDecideOption, Client, Config, OptimizelyOptions } from './shared_types';
import { BrowserOdpManager } from './odp/odp_manager.browser';
import Optimizely from './optimizely';
import { IUserAgentParser } from './odp/ua_parser/user_agent_parser';
import { getUserAgentParser } from './odp/ua_parser/ua_parser.browser';
import * as commonExports from './common_exports';
import { PollingConfigManagerConfig } from './project_config/config_manager_factory';
import { createPollingProjectConfigManager } from './project_config/config_manager_factory.browser';
import { createBatchEventProcessor, createForwardingEventProcessor } from './event_processor/event_processor_factory.browser';
const logger = getLogger();
logHelper.setLogHandler(loggerPlugin.createLogger());
logHelper.setLogLevel(LogLevel.INFO);
const MODULE_NAME = 'INDEX_BROWSER';
const DEFAULT_EVENT_BATCH_SIZE = 10;
const DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000;
let hasRetriedEvents = false;
/**
* Creates an instance of the Optimizely class
* @param {Config} config
* @return {Client|null} the Optimizely client object
* null on error
*/
const createInstance = function(config: Config): Client | null {
try {
// TODO warn about setting per instance errorHandler / logger / logLevel
let isValidInstance = false;
if (config.errorHandler) {
setErrorHandler(config.errorHandler);
}
if (config.logger) {
logHelper.setLogHandler(config.logger);
// respect the logger's shouldLog functionality
logHelper.setLogLevel(LogLevel.NOTSET);
}
if (config.logLevel !== undefined) {
logHelper.setLogLevel(config.logLevel);
}
try {
configValidator.validate(config);
isValidInstance = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (ex) {
logger.error(ex);
}
// let eventDispatcher;
// // prettier-ignore
// if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
// // only wrap the event dispatcher with pending events retry if the user didnt override
// eventDispatcher = new LocalStoragePendingEventsDispatcher({
// eventDispatcher: defaultEventDispatcher,
// });
// if (!hasRetriedEvents) {
// eventDispatcher.sendPendingEvents();
// hasRetriedEvents = true;
// }
// } else {
// eventDispatcher = config.eventDispatcher;
// }
// let closingDispatcher = config.closingEventDispatcher;
// if (!config.eventDispatcher && !closingDispatcher && window.navigator && 'sendBeacon' in window.navigator) {
// closingDispatcher = sendBeaconEventDispatcher;
// }
// let eventBatchSize = config.eventBatchSize;
// let eventFlushInterval = config.eventFlushInterval;
// if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
// logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
// eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
// }
// if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
// logger.warn(
// 'Invalid eventFlushInterval %s, defaulting to %s',
// config.eventFlushInterval,
// DEFAULT_EVENT_FLUSH_INTERVAL
// );
// eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
// }
const errorHandler = getErrorHandler();
const notificationCenter = createNotificationCenter({ logger: logger, errorHandler: errorHandler });
// const eventProcessorConfig = {
// dispatcher: eventDispatcher,
// closingDispatcher,
// flushInterval: eventFlushInterval,
// batchSize: eventBatchSize,
// maxQueueSize: config.eventMaxQueueSize || DEFAULT_EVENT_MAX_QUEUE_SIZE,
// notificationCenter,
// };
const odpExplicitlyOff = config.odpOptions?.disabled === true;
if (odpExplicitlyOff) {
logger.info(enums.LOG_MESSAGES.ODP_DISABLED);
}
const { clientEngine, clientVersion } = config;
const optimizelyOptions: OptimizelyOptions = {
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
...config,
// eventProcessor: eventProcessor.createEventProcessor(eventProcessorConfig),
logger,
errorHandler,
notificationCenter,
isValidInstance,
odpManager: odpExplicitlyOff ? undefined
: BrowserOdpManager.createInstance({ logger, odpOptions: config.odpOptions, clientEngine, clientVersion }),
};
const optimizely = new Optimizely(optimizelyOptions);
try {
if (typeof window.addEventListener === 'function') {
const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
window.addEventListener(
unloadEvent,
() => {
optimizely.close();
},
false
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e) {
logger.error(enums.LOG_MESSAGES.UNABLE_TO_ATTACH_UNLOAD, MODULE_NAME, e.message);
}
return optimizely;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e) {
logger.error(e);
return null;
}
};
const __internalResetRetryState = function(): void {
hasRetriedEvents = false;
};
/**
* Entry point into the Optimizely Browser SDK
*/
const setLogHandler = logHelper.setLogHandler;
const setLogLevel = logHelper.setLogLevel;
export {
loggerPlugin as logging,
defaultErrorHandler as errorHandler,
defaultEventDispatcher as eventDispatcher,
sendBeaconEventDispatcher,
enums,
setLogHandler as setLogger,
setLogLevel,
createInstance,
__internalResetRetryState,
OptimizelyDecideOption,
IUserAgentParser,
getUserAgentParser,
createPollingProjectConfigManager,
createForwardingEventProcessor,
createBatchEventProcessor,
};
export * from './common_exports';
export default {
...commonExports,
logging: loggerPlugin,
errorHandler: defaultErrorHandler,
eventDispatcher: defaultEventDispatcher,
sendBeaconEventDispatcher,
enums,
setLogger: setLogHandler,
setLogLevel,
createInstance,
__internalResetRetryState,
OptimizelyDecideOption,
getUserAgentParser,
createPollingProjectConfigManager,
createForwardingEventProcessor,
createBatchEventProcessor,
};
export * from './export_types';