-
Notifications
You must be signed in to change notification settings - Fork 83
converted entry level modules to ts #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address and fix all issues.
|
||
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why left blank line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its probably a line break between module imports and internal imports
|
||
let hasRetriedEvents = false; | ||
|
||
interface Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document it. we should export as well? not sure.
@@ -97,7 +104,7 @@ var createInstance = function(config) { | |||
eventDispatcher = config.eventDispatcher; | |||
} | |||
|
|||
config = assign( | |||
const optimizelyConfig = assign( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why changed to optimizelyConfig
. OptimizelyConfig
is different than what is expected here.
@@ -110,29 +117,29 @@ var createInstance = function(config) { | |||
logger: logger, | |||
errorHandler: getErrorHandler(), | |||
} | |||
); | |||
) as OptimizelyConfig; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not correct.
*/ | ||
var createInstance = function(config) { | ||
const createInstance = function (config: Config): Optimizely | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function(config: Config)
should be without space.
const DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s | ||
const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000; | ||
|
||
interface Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is being used in all index level files, we should move it to shared_types.
@@ -77,6 +79,26 @@ export interface OptimizelyVariable { | |||
value: string; | |||
} | |||
|
|||
export interface OptimizelyConfig { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it defined here.
Please use the right term. [OptimizelyConfig](https://github.com/optimizely/javascript-sdk/blob/master/packages/optimizely-sdk/lib/core/optimizely_config/index.js)
is different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please address.
@@ -149,7 +140,7 @@ var createInstance = function(config) { | |||
} | |||
}; | |||
|
|||
var __internalResetRetryState = function() { | |||
const __internalResetRetryState = function (): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have default indentation without space
please fix at all places.
import * as enums from './utils/enums'; | ||
import { assign } from './utils/fns'; | ||
import Optimizely from './optimizely'; | ||
import configValidator from './utils/config_validator'; | ||
import * as configValidator from './utils/config_validator'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not modified in other files?
@@ -77,6 +79,39 @@ export interface OptimizelyVariable { | |||
value: string; | |||
} | |||
|
|||
export interface Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add documentation for both Config
and ConfigObj
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall. I have some concerns on the naming of certain variables and Interfaces. Secondly, as a general rule of typescript conversion. Please feel free to simplify some code to use latest javascript constructs. A lot of code can be modernized and simplified as now we are using typescript. For example: using spread operators instead of assign, using arrow functions where they make code look simpler etc.
|
||
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its probably a line break between module imports and internal imports
@@ -97,7 +88,7 @@ var createInstance = function(config) { | |||
eventDispatcher = config.eventDispatcher; | |||
} | |||
|
|||
config = assign( | |||
const internalConfig = assign( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are now in TS, you dont need to use assign. you can probably use spread syntax to override values.
@@ -97,7 +88,7 @@ var createInstance = function(config) { | |||
eventDispatcher = config.eventDispatcher; | |||
} | |||
|
|||
config = assign( | |||
const internalConfig = assign( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable name can be better. modifiedConfig
or updatedConfig
. You can try thinking of a better name but internalConfig
kind of looks misleading.
@@ -110,29 +101,29 @@ var createInstance = function(config) { | |||
logger: logger, | |||
errorHandler: getErrorHandler(), | |||
} | |||
); | |||
) as ConfigObj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conversions as ConfigObj
might not be needed. Are you sure its throwing error without this.?
@@ -83,7 +73,7 @@ var createInstance = function(config) { | |||
config.isValidInstance = false; | |||
} | |||
|
|||
config = assign( | |||
const internalConfig = assign( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use spread operator here too instead of assign. Also rename internalConfig to something better
/** | ||
* Config required to create optimizely object | ||
*/ | ||
export interface ConfigObj { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see why you need two different configs/options but ConfigObj
is not a good name. Two sets of feedback on this one.
- One can be inherited from the other because outer options are a subset of internal options.
- Names should be relevant.
Config
makes sense if its the only interface we use.ConfigObj
does not make sense at all. And when we have two different objects with many similar properties, bothConfig
andConfigObj
look like inappropriate choices. One choice could beSDKOptions
for the outer interface andOptimizelyOptions
for the internal . You can give it a thought and try to come up with better and more appropriate names.
* @param {string} config.sdkKey | ||
* @return {Object} the Optimizely object | ||
* @param {Config} config | ||
* @return {Optimizely} the Optimizely object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doc comment should be updated to reflect that it can also return null.
errorHandler: getErrorHandler(), | ||
}; | ||
|
||
const modifiedConfig = {...additionalEntities, ...config, ...optimizelyLoggers} as OptimizelyOptions; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid using as
type cast here by updating isValidInstance
in SDKOptions
object to not be optional.
window.addEventListener( | ||
unloadEvent, | ||
function() { | ||
function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend using arrow function here
errorHandler: getErrorHandler(), | ||
}; | ||
|
||
const modifiedConfig = {...additionalEntities, ...config, ...optimizelyLoggers}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updatedConfig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the names additionalEntities
and optimizelyLoggers
do not make much sense. Instead of building interim objects, you can add those directly when building the modifiedConfig
or updatedConfig
object. something like
const modifiedConfig = {
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
eventDispatcher: eventDispatcher,
...config,
logger: logger,
errorHandler: getErrorHandler(),
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also you can probably name it optimizelyOptions
instead of modifiedConfig
errorHandler: getErrorHandler(), | ||
} | ||
); | ||
const additionalEntities = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to put more appropriate name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detailed comments provided in browser entry file also apply to the other two entry points. I have NOT duplicated comments to all three entry points.
errorHandler: getErrorHandler(), | ||
}; | ||
|
||
const modifiedConfig = {...additionalEntities, ...config, ...optimizelyLoggers}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the names additionalEntities
and optimizelyLoggers
do not make much sense. Instead of building interim objects, you can add those directly when building the modifiedConfig
or updatedConfig
object. something like
const modifiedConfig = {
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
eventDispatcher: eventDispatcher,
...config,
logger: logger,
errorHandler: getErrorHandler(),
}
|
||
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; | ||
modifiedConfig.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declaring this object above with such detail and then mutating some of the elements makes it confusing. more cleaner code could be to assign eventBatchSize
to a local variable and then declare the optimizelyOptions
once at the end.
} | ||
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; | ||
modifiedConfig.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same comment about eventBatchSize
applies here too
|
||
try { | ||
if (typeof window.addEventListener === 'function') { | ||
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; | ||
const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nonblocking: Can we move these string values somewhere inside constants
() => { | ||
optimizely.close(); | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
() => { | |
optimizely.close(); | |
}, | |
optimizely.close, |
errorHandler: getErrorHandler(), | ||
}; | ||
|
||
const modifiedConfig = {...additionalEntities, ...config, ...optimizelyLoggers}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also you can probably name it optimizelyOptions
instead of modifiedConfig
} | ||
|
||
/** | ||
* Config required to create optimizely object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably change it to Options required
export interface SDKOptions { | ||
datafile?: string; | ||
errorHandler?: ErrorHandler; | ||
eventDispatcher?: EventDispatcher; | ||
logger?: LogHandler; | ||
logLevel?: LogLevel; | ||
userProfileService?: UserProfileService; | ||
eventBatchSize?: number; | ||
eventFlushInterval?: number; | ||
sdkKey?: string; | ||
isValidInstance: boolean; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should probably add one line documentation comment to each option here. This might help while autogenerating docs
clientEngine: string; | ||
clientVersion?: string; | ||
errorHandler: ErrorHandler; | ||
eventDispatcher: EventDispatcher; | ||
isValidInstance: boolean; | ||
datafile?: string; | ||
// TODO[OASIS-6649]: Don't use object type | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
jsonSchemaValidator?: object; | ||
sdkKey?: string; | ||
userProfileService?: UserProfileService | null; | ||
UNSTABLE_conditionEvaluators?: unknown; | ||
eventFlushInterval?: number; | ||
eventBatchSize?: number; | ||
datafileOptions?: DatafileOptions; | ||
eventMaxQueueSize?: number; | ||
logger: LogHandler; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Sort them alphabetically maybe?
datafile?: string; | ||
errorHandler?: ErrorHandler; | ||
eventDispatcher?: EventDispatcher; | ||
logger?: LogHandler; | ||
logLevel?: LogLevel; | ||
userProfileService?: UserProfileService; | ||
eventBatchSize?: number; | ||
eventFlushInterval?: number; | ||
sdkKey?: string; | ||
isValidInstance: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Sort them alphabetically maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM, two small requests
function() { | ||
optimizely.close(); | ||
}, | ||
optimizely.close, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested this change? I think it won't work. close
is not bound to optimizely
, so when it is invoked it may not have the right this
value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I asked uzair to change it this way to make it look simple and clean but it didn't occur to me that it will mess up with this
. @ozayr-zaviar ! please change it back to what it was or an arrow function whatever makes sense.
export interface OptimizelyOptions { | ||
UNSTABLE_conditionEvaluators?: unknown; | ||
clientEngine: string; | ||
clientVersion?: string; | ||
datafile?: string; | ||
datafileOptions?: DatafileOptions; | ||
errorHandler: ErrorHandler; | ||
eventBatchSize?: number; | ||
eventDispatcher: EventDispatcher; | ||
eventFlushInterval?: number; | ||
eventMaxQueueSize?: number; | ||
isValidInstance: boolean; | ||
// TODO[OASIS-6649]: Don't use object type | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
jsonSchemaValidator?: object; | ||
logger: LogHandler; | ||
sdkKey?: string; | ||
userProfileService?: UserProfileService | null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think OptimizelyOptions
should be defined in lib/optimizely/index.ts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Great Work @ozayr-zaviar
Summary
packages\optimizely-sdk\lib\index.node.js
to TSpackages\optimizely-sdk\lib\index.browser.js
to TSpackages\optimizely-sdk\lib\index.react_native.js
to TSTest plan