-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathindex.browser.ts
More file actions
134 lines (118 loc) · 4.42 KB
/
index.browser.ts
File metadata and controls
134 lines (118 loc) · 4.42 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
/**
* 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 configValidator from './utils/config_validator';
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 { OptimizelyDecideOption, Client, Config } from './shared_types';
import Optimizely from './optimizely';
import { UserAgentParser } 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';
import { createVuidManager } from './vuid/vuid_manager_factory.browser';
import { createOdpManager } from './odp/odp_manager_factory.browser';
import { UNABLE_TO_ATTACH_UNLOAD } from 'error_message';
import { extractLogger, createLogger } from './logging/logger_factory';
import { extractErrorNotifier, createErrorNotifier } from './error/error_notifier_factory';
import { LoggerFacade } from './logging/logger';
import { Maybe } from './utils/type';
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 {
let logger: Maybe<LoggerFacade>;
try {
configValidator.validate(config);
const { clientEngine, clientVersion } = config;
logger = config.logger ? extractLogger(config.logger) : undefined;
const errorNotifier = config.errorNotifier ? extractErrorNotifier(config.errorNotifier) : undefined;
const optimizelyOptions = {
...config,
clientEngine: clientEngine || enums.JAVASCRIPT_CLIENT_ENGINE,
clientVersion: clientVersion || enums.CLIENT_VERSION,
logger,
errorNotifier,
};
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(UNABLE_TO_ATTACH_UNLOAD, 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;
};
export {
defaultEventDispatcher as eventDispatcher,
sendBeaconEventDispatcher,
enums,
createInstance,
__internalResetRetryState,
OptimizelyDecideOption,
UserAgentParser as IUserAgentParser,
getUserAgentParser,
createPollingProjectConfigManager,
createForwardingEventProcessor,
createBatchEventProcessor,
createOdpManager,
createVuidManager,
createLogger,
createErrorNotifier,
};
export * from './common_exports';
export default {
...commonExports,
eventDispatcher: defaultEventDispatcher,
sendBeaconEventDispatcher,
enums,
createInstance,
__internalResetRetryState,
OptimizelyDecideOption,
getUserAgentParser,
createPollingProjectConfigManager,
createForwardingEventProcessor,
createBatchEventProcessor,
createOdpManager,
createVuidManager,
};
export * from './export_types';