-
Notifications
You must be signed in to change notification settings - Fork 83
[FSSDK-11101] refactor handling of no config availability #998
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import { OdpManager } from '../odp/odp_manager'; | |
import { VuidManager } from '../vuid/vuid_manager'; | ||
import { OdpEvent } from '../odp/event_manager/odp_event'; | ||
import { OptimizelySegmentOption } from '../odp/segment_manager/optimizely_segment_option'; | ||
import { BaseService } from '../service'; | ||
|
||
import { | ||
UserAttributes, | ||
|
@@ -71,7 +72,7 @@ import { | |
ODP_EVENT_FAILED_ODP_MANAGER_MISSING, | ||
UNABLE_TO_GET_VUID_VUID_MANAGER_NOT_AVAILABLE, | ||
UNRECOGNIZED_DECIDE_OPTION, | ||
INVALID_OBJECT, | ||
NO_PROJECT_CONFIG_FAILURE, | ||
EVENT_KEY_NOT_FOUND, | ||
NOT_TRACKING_USER, | ||
VARIABLE_REQUESTED_WITH_WRONG_TYPE, | ||
|
@@ -267,11 +268,9 @@ export default class Optimizely implements Client { | |
|
||
/** | ||
* Returns a truthy value if this instance currently has a valid project config | ||
* object, and the initial configuration object that was passed into the | ||
* constructor was also valid. | ||
* @return {boolean} | ||
*/ | ||
isValidInstance(): boolean { | ||
private hasProjectConfig(): boolean { | ||
return !!this.projectConfigManager.getConfig(); | ||
} | ||
|
||
|
@@ -284,20 +283,16 @@ export default class Optimizely implements Client { | |
*/ | ||
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'activate'); | ||
const configObj = this.projectConfigManager.getConfig(); | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'activate'); | ||
return null; | ||
} | ||
|
||
if (!this.validateInputs({ experiment_key: experimentKey, user_id: userId }, attributes)) { | ||
return this.notActivatingExperiment(experimentKey, userId); | ||
} | ||
|
||
const configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
return null; | ||
} | ||
|
||
try { | ||
const variationKey = this.getVariation(experimentKey, userId, attributes); | ||
if (variationKey === null) { | ||
|
@@ -394,20 +389,16 @@ export default class Optimizely implements Client { | |
return; | ||
} | ||
|
||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'track'); | ||
const configObj = this.projectConfigManager.getConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'track'); | ||
return; | ||
} | ||
|
||
if (!this.validateInputs({ user_id: userId, event_key: eventKey }, attributes, eventTags)) { | ||
return; | ||
} | ||
|
||
const configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
return; | ||
} | ||
|
||
|
||
if (!projectConfig.eventWithKeyExists(configObj, eventKey)) { | ||
this.logger?.warn(EVENT_KEY_NOT_FOUND, eventKey); | ||
|
@@ -453,8 +444,9 @@ export default class Optimizely implements Client { | |
*/ | ||
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getVariation'); | ||
const configObj = this.projectConfigManager.getConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getVariation'); | ||
return null; | ||
} | ||
|
||
|
@@ -463,11 +455,6 @@ export default class Optimizely implements Client { | |
return null; | ||
} | ||
|
||
const configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
return null; | ||
} | ||
|
||
const experiment = configObj.experimentKeyMap[experimentKey]; | ||
if (!experiment || experiment.isRollout) { | ||
this.logger?.debug(INVALID_EXPERIMENT_KEY_INFO, experimentKey); | ||
|
@@ -624,20 +611,16 @@ export default class Optimizely implements Client { | |
*/ | ||
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'isFeatureEnabled'); | ||
const configObj = this.projectConfigManager.getConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'isFeatureEnabled'); | ||
return false; | ||
} | ||
|
||
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) { | ||
return false; | ||
} | ||
|
||
const configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
return false; | ||
} | ||
|
||
const feature = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger); | ||
if (!feature) { | ||
return false; | ||
|
@@ -704,17 +687,14 @@ export default class Optimizely implements Client { | |
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[] { | ||
try { | ||
const enabledFeatures: string[] = []; | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getEnabledFeatures'); | ||
return enabledFeatures; | ||
} | ||
|
||
if (!this.validateInputs({ user_id: userId })) { | ||
const configObj = this.projectConfigManager.getConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getEnabledFeatures'); | ||
return enabledFeatures; | ||
} | ||
|
||
const configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
if (!this.validateInputs({ user_id: userId })) { | ||
return enabledFeatures; | ||
} | ||
|
||
|
@@ -752,8 +732,8 @@ export default class Optimizely implements Client { | |
attributes?: UserAttributes | ||
): FeatureVariableValue { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getFeatureVariable'); | ||
if (!this.hasProjectConfig()) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariable'); | ||
return null; | ||
} | ||
return this.getFeatureVariableForType(featureKey, variableKey, null, userId, attributes); | ||
|
@@ -946,8 +926,8 @@ export default class Optimizely implements Client { | |
attributes?: UserAttributes | ||
): boolean | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableBoolean'); | ||
if (!this.hasProjectConfig()) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableBoolean'); | ||
return null; | ||
} | ||
return this.getFeatureVariableForType( | ||
|
@@ -984,8 +964,8 @@ export default class Optimizely implements Client { | |
attributes?: UserAttributes | ||
): number | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableDouble'); | ||
if (!this.hasProjectConfig()) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableDouble'); | ||
return null; | ||
} | ||
return this.getFeatureVariableForType( | ||
|
@@ -1022,8 +1002,8 @@ export default class Optimizely implements Client { | |
attributes?: UserAttributes | ||
): number | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableInteger'); | ||
if (!this.hasProjectConfig()) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableInteger'); | ||
return null; | ||
} | ||
return this.getFeatureVariableForType( | ||
|
@@ -1060,8 +1040,8 @@ export default class Optimizely implements Client { | |
attributes?: UserAttributes | ||
): string | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableString'); | ||
if (!this.hasProjectConfig()) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableString'); | ||
return null; | ||
} | ||
return this.getFeatureVariableForType( | ||
|
@@ -1093,8 +1073,8 @@ export default class Optimizely implements Client { | |
*/ | ||
getFeatureVariableJSON(featureKey: string, variableKey: string, userId: string, attributes: UserAttributes): unknown { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getFeatureVariableJSON'); | ||
if (!this.hasProjectConfig()) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getFeatureVariableJSON'); | ||
return null; | ||
} | ||
return this.getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.JSON, userId, attributes); | ||
|
@@ -1120,17 +1100,14 @@ export default class Optimizely implements Client { | |
attributes?: UserAttributes | ||
): { [variableKey: string]: unknown } | null { | ||
try { | ||
if (!this.isValidInstance()) { | ||
this.logger?.error(INVALID_OBJECT, 'getAllFeatureVariables'); | ||
return null; | ||
} | ||
const configObj = this.projectConfigManager.getConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) { | ||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'getAllFeatureVariables'); | ||
return null; | ||
} | ||
|
||
const configObj = this.projectConfigManager.getConfig(); | ||
if (!configObj) { | ||
if (!this.validateInputs({ feature_key: featureKey, user_id: userId }, attributes)) { | ||
return null; | ||
} | ||
|
||
|
@@ -1425,8 +1402,8 @@ export default class Optimizely implements Client { | |
decide(user: OptimizelyUserContext, key: string, options: OptimizelyDecideOption[] = []): OptimizelyDecision { | ||
const configObj = this.projectConfigManager.getConfig(); | ||
|
||
if (!this.isValidInstance() || !configObj) { | ||
this.logger?.error(INVALID_OBJECT, 'decide'); | ||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decide'); | ||
return newErrorDecision(key, user, [DECISION_MESSAGES.SDK_NOT_READY]); | ||
} | ||
|
||
|
@@ -1570,8 +1547,8 @@ export default class Optimizely implements Client { | |
|
||
const configObj = this.projectConfigManager.getConfig() | ||
|
||
if (!this.isValidInstance() || !configObj) { | ||
this.logger?.error(INVALID_OBJECT, 'decideForKeys'); | ||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decideForKeys'); | ||
return decisionMap; | ||
} | ||
if (keys.length === 0) { | ||
|
@@ -1638,10 +1615,10 @@ export default class Optimizely implements Client { | |
user: OptimizelyUserContext, | ||
options: OptimizelyDecideOption[] = [] | ||
): { [key: string]: OptimizelyDecision } { | ||
const configObj = this.projectConfigManager.getConfig(); | ||
const decisionMap: { [key: string]: OptimizelyDecision } = {}; | ||
if (!this.isValidInstance() || !configObj) { | ||
this.logger?.error(INVALID_OBJECT, 'decideAll'); | ||
const configObj = this.projectConfigManager.getConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
if (!configObj) { | ||
this.errorReporter.report(NO_PROJECT_CONFIG_FAILURE, 'decideAll'); | ||
return decisionMap; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.