featurehub-javascript-client-sdk
Version:
FeatureHub client/browser SDK
155 lines • 5.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EdgeFeatureHubConfig = void 0;
const client_feature_repository_1 = require("./client_feature_repository");
const feature_hub_config_1 = require("./feature_hub_config");
const context_impl_1 = require("./context_impl");
const polling_sdk_1 = require("./polling_sdk");
class EdgeFeatureHubConfig {
static config(url, apiKey) {
if (EdgeFeatureHubConfig._singleton) {
if (EdgeFeatureHubConfig._singleton._originalUrl == url && EdgeFeatureHubConfig._singleton._apiKey == apiKey) {
return EdgeFeatureHubConfig._singleton;
}
EdgeFeatureHubConfig._singleton.forceClose();
}
EdgeFeatureHubConfig._singleton = new EdgeFeatureHubConfig(url, apiKey);
return EdgeFeatureHubConfig._singleton;
}
constructor(host, apiKey) {
this._edgeServices = [];
this._initialized = false;
this._apiKey = apiKey;
this._host = host;
feature_hub_config_1.fhLog.trace('creating new featurehub config.');
if (apiKey == null || host == null) {
throw new Error('apiKey and host must not be null');
}
this._apiKeys = [apiKey];
this._clientEval = this._apiKey.includes('*');
if (!this._host.endsWith('/')) {
this._host += '/';
}
if (this._host.endsWith('/features/')) {
this._host = this._host.substring(0, this._host.length - ('/features/'.length - 1));
}
this._originalUrl = host;
this._url = this._host + 'features/' + this._apiKey;
}
addReadynessListener(listener) {
return this.addReadinessListener(listener);
}
addAnalyticCollector(collector) {
this.repository().addAnalyticCollector(collector);
}
addValueInterceptor(interceptor) {
this.repository().addValueInterceptor(interceptor);
}
get readyness() {
return this.repository().readyness;
}
get readiness() {
return this.repository().readyness;
}
feature(name) {
if (this.clientEvaluated()) {
throw new Error('You cannot use this method for client evaluated keys, please get a context with .newContext()');
}
return this.newContext().feature(name);
}
apiKey(apiKey) {
this._apiKeys.push(apiKey);
return this;
}
clientEvaluated() {
return this._clientEval;
}
getApiKeys() {
return Object.assign([], this._apiKeys);
}
getHost() {
return this._host;
}
newContext(repository, edgeService) {
repository = repository || this.repository();
edgeService = edgeService || this.edgeServiceProvider();
if (this._clientEval) {
return new context_impl_1.ClientEvalFeatureContext(repository, this.getOrCreateEdgeService(edgeService, repository));
}
if (!this._clientContext) {
this._clientContext =
new context_impl_1.ServerEvalFeatureContext(repository, () => this.getOrCreateEdgeService(edgeService, repository));
}
this._clientContext.addClient();
return this._clientContext;
}
getOrCreateEdgeService(edgeServSupplier, repository) {
if (this._edgeServices.length === 0) {
return this.createEdgeService(edgeServSupplier, repository);
}
return this._edgeServices[0];
}
createEdgeService(edgeServSupplier, repository) {
const es = edgeServSupplier(repository || this.repository(), this);
this._initialized = true;
this._edgeServices.push(es);
return es;
}
close() {
if (this._clientContext) {
if (this._clientContext.removeClient()) {
this.forceClose();
this._clientContext = undefined;
}
}
else {
this.forceClose();
}
}
forceClose() {
this._edgeServices.forEach((es) => {
es.close();
});
this._edgeServices.length = 0;
this._initialized = false;
}
get closed() { return !this._initialized; }
get initialized() { return this._initialized; }
init() {
if (!this._initialized) {
this.repository();
this.getOrCreateEdgeService(this.edgeServiceProvider()).poll().catch((e) => feature_hub_config_1.fhLog.error(`Failed to connect to FeatureHub Edge ${e}`));
}
return this;
}
edgeServiceProvider(edgeServ) {
if (edgeServ != null) {
this._edgeService = edgeServ;
}
else if (this._edgeService == null) {
this._edgeService = EdgeFeatureHubConfig.defaultEdgeServiceSupplier;
}
return this._edgeService;
}
repository(repository) {
if (repository != null) {
this._repository = repository;
}
else if (this._repository == null) {
this._repository = new client_feature_repository_1.ClientFeatureRepository();
}
return this._repository;
}
url() {
return this._url;
}
addReadinessListener(listener, ignoreNotReadyOnRegister) {
return this.repository().addReadinessListener(listener, ignoreNotReadyOnRegister);
}
removeReadinessListener(listener) {
this.repository().removeReadinessListener(listener);
}
}
exports.EdgeFeatureHubConfig = EdgeFeatureHubConfig;
EdgeFeatureHubConfig.defaultEdgeServiceSupplier = (repository, config) => new polling_sdk_1.FeatureHubPollingClient(repository, config, 30000);
//# sourceMappingURL=edge_featurehub_config.js.map