UNPKG

featurehub-javascript-client-sdk

Version:
211 lines 7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientEvalFeatureContext = exports.ServerEvalFeatureContext = exports.BaseClientContext = void 0; const feature_hub_config_1 = require("./feature_hub_config"); class BaseClientContext { constructor(repository) { this._attributes = new Map(); this._repository = repository; } userKey(value) { this._attributes.set('userkey', [value]); return this; } sessionKey(value) { this._attributes.set('session', [value]); return this; } country(value) { this._attributes.set('country', [value]); return this; } device(value) { this._attributes.set('device', [value]); return this; } platform(value) { this._attributes.set('platform', [value]); return this; } version(version) { this._attributes.set('version', [version]); return this; } attribute_value(key, value) { return this.attributeValue(key, value); } attributeValue(key, value) { this._attributes.set(key, [value]); return this; } attribute_values(key, values) { return this.attributeValues(key, values); } attributeValues(key, values) { this._attributes.set(key, values); return this; } clear() { this._attributes.clear(); return this; } getAttr(key, defaultValue) { if (this._attributes.has(key)) { return this._attributes.get(key)[0]; } return defaultValue; } getAttrs(key) { if (this._attributes.has(key)) { return this._attributes.get(key); } return []; } defaultPercentageKey() { return this._attributes.has('session') ? this.getAttr('session') : this.getAttr('userkey'); } isEnabled(name) { return this.feature(name).isEnabled(); } isSet(name) { return this.feature(name).isSet(); } getNumber(name, def) { const fsh = this.feature(name); return fsh.isSet() ? fsh.getNumber() : def; } getString(name, def) { const fsh = this.feature(name); return fsh.isSet() ? fsh.getString() : def; } getJson(name, def) { const fsh = this.feature(name); if (fsh.isSet()) { const val = fsh.getRawJson(); return JSON.parse(val); } else { return def; } } getRawJson(name, def) { const fsh = this.feature(name); return fsh.isSet() ? fsh.getRawJson() : def; } getFlag(name, def) { const fsh = this.feature(name); return fsh.isSet() ? fsh.getBoolean() : def; } getBoolean(name, def) { const fsh = this.feature(name); return fsh.isSet() ? fsh.getBoolean() : def; } repository() { return this._repository; } logAnalyticsEvent(action, other, user) { if (user == null) { user = this.getAttr('userkey'); } if (user != null) { if (other == null) { other = new Map(); } other.set('cid', user); } this._repository.logAnalyticsEvent(action, other); } } exports.BaseClientContext = BaseClientContext; class ServerEvalFeatureContext extends BaseClientContext { constructor(repository, edgeServiceSupplier, config) { super(repository); this._clientCount = 0; this._edgeServiceSupplier = edgeServiceSupplier; this._config = config; } addClient() { this._clientCount += 1; } removeClient() { this._clientCount -= 1; return this._clientCount <= 0; } build() { return __awaiter(this, void 0, void 0, function* () { try { const newHeader = Array.from(this._attributes.entries()).map((key) => key[0] + '=' + encodeURIComponent(key[1].join(','))).sort().join(','); if (newHeader !== this._xHeader) { this._xHeader = newHeader; this._repository.notReady(); if (this._currentEdge != null && this._currentEdge.requiresReplacementOnHeaderChange()) { this._currentEdge.close(); this._currentEdge = undefined; } } if (this._currentEdge === undefined) { this._currentEdge = this._edgeServiceSupplier(); } if (this._currentEdge !== undefined) { yield this._currentEdge.contextChange(this._xHeader); } } catch (e) { if (e) { feature_hub_config_1.fhLog.error('Failed to connect to FeatureHHub Edge to refresh context', e); } } return this; }); } close() { if (this._clientCount <= 1 && this._config !== undefined) { feature_hub_config_1.fhLog.trace('closing because client count is ', this._clientCount); this._config.close(); } else if (this._currentEdge) { feature_hub_config_1.fhLog.trace('closing because directly requested close.'); this._currentEdge.close(); } } edgeService() { return this._currentEdge; } feature(name) { return this._repository.feature(name); } } exports.ServerEvalFeatureContext = ServerEvalFeatureContext; class ClientEvalFeatureContext extends BaseClientContext { constructor(repository, edgeService) { super(repository); this._edgeService = edgeService; } build() { var _a; return __awaiter(this, void 0, void 0, function* () { (_a = this._edgeService.poll()) === null || _a === void 0 ? void 0 : _a.then(() => { }).catch(() => { }); return this; }); } close() { this._edgeService.close(); } edgeService() { return this._edgeService; } feature(name) { return this._repository.feature(name).withContext(this); } } exports.ClientEvalFeatureContext = ClientEvalFeatureContext; //# sourceMappingURL=context_impl.js.map