Skip to content

Commit 170b26e

Browse files
author
uzair-folio3
committed
index.browser.js converted to ts
1 parent e08bfab commit 170b26e

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

packages/optimizely-sdk/lib/index.browser.js renamed to packages/optimizely-sdk/lib/index.browser.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {
16+
import {
1717
getLogger,
1818
setLogHandler,
1919
setLogLevel,
2020
setErrorHandler,
2121
getErrorHandler,
2222
LogLevel,
23+
ErrorHandler,
24+
LogHandler,
2325
} from '@optimizely/js-sdk-logging';
24-
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';
2526

27+
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';
2628
import { assign } from './utils/fns';
2729
import * as configValidator from './utils/config_validator';
2830
import defaultErrorHandler from './plugins/error_handler';
@@ -31,32 +33,37 @@ import * as enums from './utils/enums';
3133
import loggerPlugin from './plugins/logger';
3234
import Optimizely from './optimizely';
3335
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
36+
import * as projectConfig from './core/project_config';
3437

35-
var logger = getLogger();
38+
const logger = getLogger();
3639
setLogHandler(loggerPlugin.createLogger());
3740
setLogLevel(LogLevel.INFO);
3841

39-
var MODULE_NAME = 'INDEX_BROWSER';
40-
var DEFAULT_EVENT_BATCH_SIZE = 10;
41-
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
42-
43-
var hasRetriedEvents = false;
42+
const MODULE_NAME = 'INDEX_BROWSER';
43+
const DEFAULT_EVENT_BATCH_SIZE = 10;
44+
const DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
45+
46+
let hasRetriedEvents = false;
47+
48+
interface Config {
49+
datafile?: projectConfig.ProjectConfig;
50+
errorHandler?: ErrorHandler;
51+
eventDispatcher?: (...args: unknown[]) => unknown;
52+
logger?: LogHandler;
53+
logLevel?: LogLevel;
54+
userProfileService?: import('./shared_types').UserProfileService;
55+
eventBatchSize?: number;
56+
eventFlushInterval?: number;
57+
sdkKey?: string;
58+
isValidInstance?: boolean;
59+
}
4460

4561
/**
4662
* Creates an instance of the Optimizely class
47-
* @param {Object} config
48-
* @param {Object|string} config.datafile
49-
* @param {Object} config.errorHandler
50-
* @param {Object} config.eventDispatcher
51-
* @param {Object} config.logger
52-
* @param {Object} config.logLevel
53-
* @param {Object} config.userProfileService
54-
* @param {Object} config.eventBatchSize
55-
* @param {Object} config.eventFlushInterval
56-
* @param {string} config.sdkKey
57-
* @return {Object} the Optimizely object
63+
* @param {Config} config
64+
* @return {Optimizely} the Optimizely object
5865
*/
59-
var createInstance = function(config) {
66+
const createInstance = function (config: Config): Optimizely | null {
6067
try {
6168
config = config || {};
6269

@@ -81,7 +88,7 @@ var createInstance = function(config) {
8188
config.isValidInstance = false;
8289
}
8390

84-
var eventDispatcher;
91+
let eventDispatcher;
8592
// prettier-ignore
8693
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
8794
// only wrap the event dispatcher with pending events retry if the user didnt override
@@ -97,7 +104,7 @@ var createInstance = function(config) {
97104
eventDispatcher = config.eventDispatcher;
98105
}
99106

100-
config = assign(
107+
const optimizelyConfig = assign(
101108
{
102109
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
103110
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
@@ -110,29 +117,29 @@ var createInstance = function(config) {
110117
logger: logger,
111118
errorHandler: getErrorHandler(),
112119
}
113-
);
120+
) as projectConfig.ProjectConfig;
114121

115122
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
116123
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
117-
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
124+
optimizelyConfig.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
118125
}
119126
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
120127
logger.warn(
121128
'Invalid eventFlushInterval %s, defaulting to %s',
122129
config.eventFlushInterval,
123130
DEFAULT_EVENT_FLUSH_INTERVAL
124131
);
125-
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
132+
optimizelyConfig.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
126133
}
127134

128-
var optimizely = new Optimizely(config);
135+
const optimizely = new Optimizely(optimizelyConfig);
129136

130137
try {
131138
if (typeof window.addEventListener === 'function') {
132-
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
139+
const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
133140
window.addEventListener(
134141
unloadEvent,
135-
function() {
142+
function () {
136143
optimizely.close();
137144
},
138145
false
@@ -149,7 +156,7 @@ var createInstance = function(config) {
149156
}
150157
};
151158

152-
var __internalResetRetryState = function() {
159+
const __internalResetRetryState = function (): void {
153160
hasRetriedEvents = false;
154161
};
155162

@@ -164,8 +171,8 @@ export {
164171
setLogHandler as setLogger,
165172
setLogLevel,
166173
createInstance,
167-
__internalResetRetryState,
168-
}
174+
__internalResetRetryState,
175+
};
169176

170177
export default {
171178
logging: loggerPlugin,

0 commit comments

Comments
 (0)