-
Notifications
You must be signed in to change notification settings - Fork 83
feat(utils): Add PollingConfigCache and ClientCache #96
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 all commits
c97f078
f32901d
de24a61
4a9065c
b362208
3ee28f7
7fab73c
abf2f35
dd140e7
2b70657
2352327
8d153ea
fa29525
d74b475
b0cf778
6825f68
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 |
---|---|---|
|
@@ -13,61 +13,34 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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 logger = require('./plugins/logger'); | ||
var Optimizely = require('./optimizely'); | ||
|
||
var MODULE_NAME = 'INDEX'; | ||
var { PollingConfigCache } = require('./utils/config_cache'); | ||
var { ClientCache } = require('./utils/client_cache'); | ||
|
||
/** | ||
* Entry point into the Optimizely Node testing SDK | ||
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. This comment is no longer valid :) |
||
*/ | ||
module.exports = { | ||
/** | ||
* 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 | ||
* @return {Object} the Optimizely object | ||
*/ | ||
createInstance: function(config) { | ||
var logLevel = 'logLevel' in config ? config.logLevel : enums.LOG_LEVEL.INFO; | ||
var defaultLogger = logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO }); | ||
if (config) { | ||
try { | ||
configValidator.validate(config); | ||
config.isValidInstance = true; | ||
} catch (ex) { | ||
var errorMessage = MODULE_NAME + ':' + ex.message; | ||
if (config.logger) { | ||
config.logger.log(enums.LOG_LEVEL.ERROR, errorMessage); | ||
} else { | ||
defaultLogger.log(enums.LOG_LEVEL.ERROR, errorMessage); | ||
} | ||
config.isValidInstance = false; | ||
} | ||
} | ||
createInstance: Optimizely.createInstance, | ||
|
||
PollingConfigCache: PollingConfigCache(browserRequester), | ||
ClientCache, | ||
}; | ||
|
||
if (config.skipJSONValidation == null) { | ||
config.skipJSONValidation = true; | ||
} | ||
/** | ||
* The function that PollingConfigCache should use by default to update a config. | ||
*/ | ||
async function browserRequester(url, headers) { | ||
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. Maybe move this into |
||
// Currently broken, see https://optimizely.atlassian.net/browse/E2-3008 | ||
const response = await window.fetch(url, { headers, mode: 'cors' }); | ||
|
||
config = fns.assignIn({ | ||
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE, | ||
clientVersion: enums.CLIENT_VERSION, | ||
errorHandler: defaultErrorHandler, | ||
eventDispatcher: defaultEventDispatcher, | ||
logger: logger.createLogger({ logLevel: logLevel }) | ||
}, config); | ||
return { | ||
body: await response.text(), | ||
headers: Array.from(response.headers.entries()).reduce((acc, [k, v]) => { | ||
acc[k] = v; | ||
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. Please use proper variable names |
||
return acc; | ||
}, {}), | ||
statusCode: response.status, | ||
}; | ||
} | ||
|
||
return new Optimizely(config); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ var decisionService = require('../core/decision_service'); | |
var enums = require('../utils/enums'); | ||
var eventBuilder = require('../core/event_builder/index.js'); | ||
var eventTagsValidator = require('../utils/event_tags_validator'); | ||
var logger = require('../plugins/logger'); | ||
var notificationCenter = require('../core/notification_center'); | ||
var projectConfig = require('../core/project_config'); | ||
var projectConfigSchema = require('./project_config_schema'); | ||
|
@@ -34,6 +35,12 @@ var MODULE_NAME = 'OPTIMIZELY'; | |
var DECISION_SOURCES = enums.DECISION_SOURCES; | ||
var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES; | ||
|
||
var configValidator = require('../utils/config_validator'); | ||
var defaultErrorHandler = require('../plugins/error_handler'); | ||
var defaultEventDispatcher = require('../plugins/event_dispatcher/index.browser'); | ||
var MODULE_NAME = 'INDEX'; | ||
|
||
|
||
/** | ||
* The Optimizely class | ||
* @param {Object} config | ||
|
@@ -660,4 +667,52 @@ Optimizely.prototype.getFeatureVariableString = function(featureKey, variableKey | |
return this._getFeatureVariableForType(featureKey, variableKey, FEATURE_VARIABLE_TYPES.STRING, userId, attributes); | ||
}; | ||
|
||
module.exports = Optimizely; | ||
/** | ||
* 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 | ||
* @return {Object} the Optimizely object | ||
*/ | ||
function createInstance(config) { | ||
var logLevel = 'logLevel' in config ? config.logLevel : enums.LOG_LEVEL.INFO; | ||
var defaultLogger = logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO }); | ||
if (config) { | ||
try { | ||
configValidator.validate(config); | ||
config.isValidInstance = true; | ||
} catch (ex) { | ||
var errorMessage = MODULE_NAME + ':' + ex.message; | ||
if (config.logger) { | ||
config.logger.log(enums.LOG_LEVEL.ERROR, errorMessage); | ||
} else { | ||
defaultLogger.log(enums.LOG_LEVEL.ERROR, errorMessage); | ||
} | ||
config.isValidInstance = false; | ||
} | ||
} | ||
|
||
if (config.skipJSONValidation == null) { | ||
config.skipJSONValidation = true; | ||
} | ||
|
||
config = fns.assignIn({ | ||
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE, | ||
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. This needs to vary by entrypoint because it lets the backend know whether it is a node or browser client. |
||
clientVersion: enums.CLIENT_VERSION, | ||
errorHandler: defaultErrorHandler, | ||
eventDispatcher: defaultEventDispatcher, | ||
logger: logger.createLogger({ logLevel: logLevel }) | ||
}, config); | ||
|
||
return new Optimizely(config); | ||
} | ||
|
||
module.exports = { | ||
Optimizely, | ||
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. Any reason you export the class as well? I don't think you are using it. |
||
createInstance, | ||
}; | ||
|
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 this is supposed to be an overridable component, I think it should be in the
plugins
directory as with the rest of the overridable components. Also, I was thinking we can even make this it's own package and later provide an entry point in here that allows users to not include that package (not that we can't do it without making it a separate package). Just drawing inspiration from other open source projects that split up in multiple packages likeReact
andApollo