UNPKG

featurehub-javascript-client-sdk

Version:
235 lines 7.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FeatureStateBaseHolder = void 0; const models_1 = require("./models"); const listener_utils_1 = require("./listener_utils"); const feature_hub_config_1 = require("./feature_hub_config"); class FeatureStateBaseHolder { constructor(repository, key, existingHolder) { this.listeners = new Map(); if (existingHolder !== null && existingHolder !== undefined) { this.listeners = existingHolder.listeners; } this._repo = repository; this._key = key; } get key() { return this.getKey(); } get str() { return this.getString(); } get flag() { return this.getFlag(); } get num() { return this.getNumber(); } get rawJson() { return this.getRawJson(); } get exists() { return this.internalFeatureState !== undefined; } get locked() { return this.isLocked(); } get enabled() { return this.isEnabled(); } get version() { return this.getVersion(); } get type() { return this.getType(); } withContext(param) { const fsh = this._copy(); fsh._ctx = param; return fsh; } isEnabled() { return this.getBoolean() === true; } addListener(listener) { const pos = listener_utils_1.ListenerUtils.newListenerKey(this.listeners); if (this._ctx !== undefined) { this.listeners.set(pos, { listener: () => listener(this), holder: this }); } else { this.listeners.set(pos, { listener: listener, holder: this }); } return pos; } removeListener(handle) { listener_utils_1.ListenerUtils.removeListener(this.listeners, handle); } getBoolean() { return this._getValue(models_1.FeatureValueType.Boolean); } getFlag() { return this.getBoolean(); } getKey() { return this._key; } getNumber() { return this._getValue(models_1.FeatureValueType.Number); } getRawJson() { return this._getValue(models_1.FeatureValueType.Json); } getString() { return this._getValue(models_1.FeatureValueType.String); } isSet() { const val = this._getValue(); return val !== undefined && val != null; } getFeatureState() { return this.featureState(); } setFeatureState(fs) { var _a; const existingValue = this._getValue(); const existingLocked = this.locked; const listenerValues = new Map(); this.listeners.forEach((value, key) => { listenerValues.set(key, { value: value.holder.value }); }); this.internalFeatureState = fs; const changedLocked = existingLocked !== ((_a = this.featureState()) === null || _a === void 0 ? void 0 : _a.l); let changed = changedLocked || existingValue !== this._getValue(fs === null || fs === void 0 ? void 0 : fs.type); this.listeners.forEach((value, key) => { const original = listenerValues.get(key); if (changedLocked || (original === null || original === void 0 ? void 0 : original.value) !== value.holder.value) { changed = true; try { value.listener(value.holder); } catch (e) { feature_hub_config_1.fhLog.error('Failed to trigger listener', e); } } }); return changed; } copy() { return this._copy(); } analyticsCopy() { const c = this._copy(); c.internalFeatureState = this.internalFeatureState; return c; } getType() { var _a; return (_a = this.featureState()) === null || _a === void 0 ? void 0 : _a.type; } getVersion() { var _a; const version1 = (_a = this.featureState()) === null || _a === void 0 ? void 0 : _a.version; return version1 !== undefined ? version1 : -1; } isLocked() { return this.featureState() === undefined ? false : this.featureState().l; } triggerListeners(feature) { this.listeners.forEach((l) => { try { l.listener(feature || this); } catch (_) { } }); } _copy() { const bh = new FeatureStateBaseHolder(this._repo, this._key, this); bh.parentHolder = this; return bh; } featureState() { if (this.internalFeatureState !== undefined) { return this.internalFeatureState; } if (this.parentHolder !== undefined) { return this.parentHolder.featureState(); } return this.internalFeatureState; } _getValue(type, parseJson = false) { var _a; if (!type) { type = this.getType(); } if (!type) { return undefined; } if (!this.isLocked()) { const intercept = this._repo.valueInterceptorMatched(this._key); if (intercept === null || intercept === void 0 ? void 0 : intercept.value) { return this._castType(type, intercept.value, parseJson); } } const featureState = this.featureState(); if (!featureState || (featureState.type !== type)) { return undefined; } if (this._ctx != null && ((_a = featureState.strategies) === null || _a === void 0 ? void 0 : _a.length)) { const matched = this._repo.apply(featureState.strategies || [], this._key, featureState.id, this._ctx); if (matched.matched) { return this._castType(type, matched.value, parseJson); } } return featureState === null || featureState === void 0 ? void 0 : featureState.value; } _castType(type, value, parseJson = false) { if (value == null) { return undefined; } if (type === models_1.FeatureValueType.Boolean) { return typeof value === 'boolean' ? value : ('true' === value.toString()); } else if (type === models_1.FeatureValueType.String) { return value.toString(); } else if (type === models_1.FeatureValueType.Number) { if (typeof value === 'number') { return value; } if (value.includes('.')) { return parseFloat(value); } return parseInt(value); } else if (type === models_1.FeatureValueType.Json) { if (parseJson) { try { return JSON.parse(value.toString()); } catch (_a) { return {}; } } return value.toString(); } else { return value.toString(); } } get value() { return this._getValue(this.getType(), true); } get featureProperties() { var _a, _b; return (_b = (_a = this.featureState()) === null || _a === void 0 ? void 0 : _a.fp) !== null && _b !== void 0 ? _b : undefined; } } exports.FeatureStateBaseHolder = FeatureStateBaseHolder; //# sourceMappingURL=feature_state_holders.js.map