This repository was archived by the owner on Jun 21, 2023. It is now read-only.
forked from optimizely/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.react_native.js
130 lines (117 loc) · 4.36 KB
/
index.react_native.js
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
/**
* Copyright 2019, 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
*
* http://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.
*/
var logging = require('@optimizely/js-sdk-logging');
var fns = require('./utils/fns');
var configValidator = require('./utils/config_validator');
var defaultErrorHandler = require('./plugins/error_handler');
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.browser');
var enums = require('./utils/enums');
var loggerPlugin = require('./plugins/logger/index.react_native');
var Optimizely = require('./optimizely');
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
var logger = logging.getLogger();
logging.setLogHandler(loggerPlugin.createLogger());
logging.setLogLevel(logging.LogLevel.INFO);
var DEFAULT_EVENT_BATCH_SIZE = 10;
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
/**
* Entry point into the Optimizely Javascript SDK for React Native
*/
module.exports = {
logging: loggerPlugin,
errorHandler: defaultErrorHandler,
eventDispatcher: defaultEventDispatcher,
enums: enums,
setLogger: logging.setLogHandler,
setLogLevel: logging.setLogLevel,
/**
* Creates an instance of the Optimizely class
* @param {Object} config
* @param {Object} config.datafile
* @param {Object} config.errorHandler
* @param {Object} config.eventDispatcher
* @param {Object} config.logger
* @param {Object} config.logLevel
* @param {Object} config.userProfileService
* @param {Object} config.eventBatchSize
* @param {Object} config.eventFlushInterval
* @return {Object} the Optimizely object
*/
createInstance: function(config) {
try {
config = config || {};
// TODO warn about setting per instance errorHandler / logger / logLevel
if (config.errorHandler) {
logging.setErrorHandler(config.errorHandler);
}
if (config.logger) {
logging.setLogHandler(config.logger);
// respect the logger's shouldLog functionality
logging.setLogLevel(logging.LogLevel.NOTSET);
}
if (config.logLevel !== undefined) {
logging.setLogLevel(config.logLevel);
}
try {
configValidator.validate(config);
config.isValidInstance = true;
} catch (ex) {
logger.error(ex);
config.isValidInstance = false;
}
// Explicitly check for null or undefined
// prettier-ignore
if (config.skipJSONValidation == null) { // eslint-disable-line eqeqeq
config.skipJSONValidation = true;
}
config = fns.assign(
{
clientEngine: enums.REACT_NATIVE_JS_CLIENT_ENGINE,
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
eventDispatcher: defaultEventDispatcher,
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
},
config,
{
// always get the OptimizelyLogger facade from logging
logger: logger,
errorHandler: logging.getErrorHandler(),
}
);
// If client engine is react, convert it to react native
if (config.clientEngine === enums.REACT_CLIENT_ENGINE) {
config.clientEngine = enums.REACT_NATIVE_CLIENT_ENGINE
}
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
}
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
logger.warn(
'Invalid eventFlushInterval %s, defaulting to %s',
config.eventFlushInterval,
DEFAULT_EVENT_FLUSH_INTERVAL
);
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
}
var optimizely = new Optimizely(config);
return optimizely;
} catch (e) {
logger.error(e);
return null;
}
},
};