@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
119 lines (118 loc) • 5.05 kB
JavaScript
import { __extends } from "tslib";
import { AbstractDefinitionsCacheSync, usesSegments } from '../AbstractDefinitionsCacheSync';
import { isFiniteNumber } from '../../utils/lang';
/**
* Default IDefinitionsCacheSync implementation that stores split definitions in memory.
*/
var DefinitionsCacheInMemory = /** @class */ (function (_super) {
__extends(DefinitionsCacheInMemory, _super);
function DefinitionsCacheInMemory(splitFiltersValidation) {
var _this = _super.call(this) || this;
_this.definitionsCache = {};
_this.ttCache = {};
_this.changeNumber = -1;
_this.segmentsCount = 0;
_this.setsCache = {};
_this.setsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
return _this;
}
DefinitionsCacheInMemory.prototype.clear = function () {
this.definitionsCache = {};
this.ttCache = {};
this.changeNumber = -1;
this.segmentsCount = 0;
this.setsCache = {};
};
DefinitionsCacheInMemory.prototype.add = function (definition) {
var name = definition.name;
var previousDefinition = this.get(name);
if (previousDefinition) { // We had this Split already
var previousTtName = previousDefinition.trafficTypeName;
this.ttCache[previousTtName]--;
if (!this.ttCache[previousTtName])
delete this.ttCache[previousTtName];
this.removeFromFlagSets(previousDefinition.name, previousDefinition.sets);
// Subtract from segments count for the previous version of this Split
if (usesSegments(previousDefinition))
this.segmentsCount--;
}
// Store the Split.
this.definitionsCache[name] = definition;
// Update TT cache
var ttName = definition.trafficTypeName;
this.ttCache[ttName] = (this.ttCache[ttName] || 0) + 1;
this.addToFlagSets(definition);
// Add to segments count for the new version of the Split
if (usesSegments(definition))
this.segmentsCount++;
return true;
};
DefinitionsCacheInMemory.prototype.remove = function (name) {
var definition = this.get(name);
if (!definition)
return false;
// Delete the Split
delete this.definitionsCache[name];
var ttName = definition.trafficTypeName;
this.ttCache[ttName]--; // Update tt cache
if (!this.ttCache[ttName])
delete this.ttCache[ttName];
this.removeFromFlagSets(definition.name, definition.sets);
// Update the segments count.
if (usesSegments(definition))
this.segmentsCount--;
return true;
};
DefinitionsCacheInMemory.prototype.get = function (name) {
return this.definitionsCache[name] || null;
};
DefinitionsCacheInMemory.prototype.setChangeNumber = function (changeNumber) {
this.changeNumber = changeNumber;
return true;
};
DefinitionsCacheInMemory.prototype.getChangeNumber = function () {
return this.changeNumber;
};
DefinitionsCacheInMemory.prototype.getNames = function () {
return Object.keys(this.definitionsCache);
};
DefinitionsCacheInMemory.prototype.trafficTypeExists = function (trafficType) {
return isFiniteNumber(this.ttCache[trafficType]) && this.ttCache[trafficType] > 0;
};
DefinitionsCacheInMemory.prototype.usesSegments = function () {
return this.getChangeNumber() === -1 || this.segmentsCount > 0;
};
DefinitionsCacheInMemory.prototype.getNamesBySets = function (sets) {
var _this = this;
return sets.map(function (set) { return _this.setsCache[set] || new Set(); });
};
DefinitionsCacheInMemory.prototype.addToFlagSets = function (featureFlag) {
var _this = this;
if (!featureFlag.sets)
return;
featureFlag.sets.forEach(function (featureFlagSet) {
if (_this.setsFilter.length > 0 && !_this.setsFilter.some(function (filterFlagSet) { return filterFlagSet === featureFlagSet; }))
return;
if (!_this.setsCache[featureFlagSet])
_this.setsCache[featureFlagSet] = new Set([]);
_this.setsCache[featureFlagSet].add(featureFlag.name);
});
};
DefinitionsCacheInMemory.prototype.removeFromFlagSets = function (featureFlagName, sets) {
var _this = this;
if (!sets)
return;
sets.forEach(function (set) {
_this.removeNames(set, featureFlagName);
});
};
DefinitionsCacheInMemory.prototype.removeNames = function (setName, featureFlagName) {
if (!this.setsCache[setName])
return;
this.setsCache[setName].delete(featureFlagName);
if (this.setsCache[setName].size === 0)
delete this.setsCache[setName];
};
return DefinitionsCacheInMemory;
}(AbstractDefinitionsCacheSync));
export { DefinitionsCacheInMemory };