UNPKG

featurehub-repository

Version:

Core package of API that exposes FeatureHub feature flags, values and configuration to client applications written in Typescript or Javascript.

168 lines 5.78 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.FeatureStateBaseHolder = void 0; const models_1 = require("./models"); 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(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, _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 === 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) { return value.toString(); } else { return value.toString(); } } } exports.FeatureStateBaseHolder = FeatureStateBaseHolder; //# sourceMappingURL=feature_state_holders.js.map