featurehub-repository
Version:
Core package of API that exposes FeatureHub feature flags, values and configuration to client applications written in Typescript or Javascript.
164 lines • 5.54 kB
JavaScript
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());
});
};
import { FeatureValueType } from './models';
export class FeatureStateBaseHolder {
constructor(repository, key, existingHolder) {
this.listeners = [];
if (existingHolder !== null && existingHolder !== undefined) {
this.listeners = existingHolder.listeners;
}
this._repo = repository;
this._key = key;
}
withContext(param) {
const fsh = this._copy();
fsh._ctx = param;
return fsh;
}
isEnabled() {
return this.getBoolean() === true;
}
addListener(listener) {
if (this._ctx !== undefined) {
this.listeners.push((fs) => listener(this));
}
else {
this.listeners.push(listener);
}
}
getBoolean() {
return this._getValue(FeatureValueType.Boolean);
}
getFlag() {
return this.getBoolean();
}
getKey() {
return this._key;
}
getNumber() {
return this._getValue(FeatureValueType.Number);
}
getRawJson() {
return this._getValue(FeatureValueType.Json);
}
getString() {
return this._getValue(FeatureValueType.String);
}
isSet() {
const val = this._getValue();
return val !== undefined && val != null;
}
getFeatureState() {
return this.featureState();
}
setFeatureState(fs) {
var _a, _b;
const existingValue = this._getValue();
const existingLocked = (_a = this.featureState()) === null || _a === void 0 ? void 0 : _a.l;
this.internalFeatureState = fs;
const changed = existingLocked !== ((_b = this.featureState()) === null || _b === void 0 ? void 0 : _b.l) || existingValue !== this._getValue();
if (changed) {
this.notifyListeners();
}
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() {
return this.featureState() === undefined ? undefined : this.featureState().version;
}
isLocked() {
return this.featureState() === undefined ? undefined : this.featureState().l;
}
triggerListeners(feature) {
this.notifyListeners(feature);
}
notifyListeners(feature) {
return __awaiter(this, void 0, void 0, function* () {
this.listeners.forEach((l) => {
try {
l(feature || this);
}
catch (e) {
}
});
});
}
_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) {
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);
}
}
const featureState = this.featureState();
if (!featureState || (type != null && featureState.type !== type)) {
return undefined;
}
if (this._ctx != null) {
const matched = this._repo.apply(featureState.strategies, this._key, featureState.id, this._ctx);
if (matched.matched) {
return this._castType(type, matched.value);
}
}
return featureState === null || featureState === void 0 ? void 0 : featureState.value;
}
_castType(type, value) {
if (value == null) {
return undefined;
}
if (type === FeatureValueType.Boolean) {
return typeof value === 'boolean' ? value : ('true' === value.toString());
}
else if (type === FeatureValueType.String) {
return value.toString();
}
else if (type === FeatureValueType.Number) {
if (typeof value === 'number') {
return value;
}
if (value.includes('.')) {
return parseFloat(value);
}
return parseInt(value);
}
else if (type === FeatureValueType.Json) {
return value.toString();
}
else {
return value.toString();
}
}
}
//# sourceMappingURL=feature_state_holders.js.map