@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
67 lines (66 loc) • 2.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RBSegmentsCacheInMemory = void 0;
var sets_1 = require("../../utils/lang/sets");
var AbstractDefinitionsCacheSync_1 = require("../AbstractDefinitionsCacheSync");
var RBSegmentsCacheInMemory = /** @class */ (function () {
function RBSegmentsCacheInMemory() {
this.cache = {};
this.changeNumber = -1;
this.segmentsCount = 0;
}
RBSegmentsCacheInMemory.prototype.clear = function () {
this.cache = {};
this.changeNumber = -1;
this.segmentsCount = 0;
};
RBSegmentsCacheInMemory.prototype.update = function (toAdd, toRemove, changeNumber) {
var _this = this;
var updated = toAdd.map(function (toAdd) { return _this.add(toAdd); }).some(function (result) { return result; });
updated = toRemove.map(function (toRemove) { return _this.remove(toRemove); }).some(function (result) { return result; }) || updated;
this.changeNumber = changeNumber;
return updated;
};
RBSegmentsCacheInMemory.prototype.add = function (rbSegment) {
var name = rbSegment.name;
var previous = this.get(name);
if (previous && (0, AbstractDefinitionsCacheSync_1.usesSegments)(previous))
this.segmentsCount--;
this.cache[name] = rbSegment;
if ((0, AbstractDefinitionsCacheSync_1.usesSegments)(rbSegment))
this.segmentsCount++;
return true;
};
RBSegmentsCacheInMemory.prototype.remove = function (name) {
var rbSegment = this.get(name);
if (!rbSegment)
return false;
delete this.cache[name];
if ((0, AbstractDefinitionsCacheSync_1.usesSegments)(rbSegment))
this.segmentsCount--;
return true;
};
RBSegmentsCacheInMemory.prototype.getNames = function () {
return Object.keys(this.cache);
};
RBSegmentsCacheInMemory.prototype.get = function (name) {
return this.cache[name] || null;
};
RBSegmentsCacheInMemory.prototype.getAll = function () {
var _this = this;
return this.getNames().map(function (key) { return _this.get(key); });
};
RBSegmentsCacheInMemory.prototype.contains = function (names) {
var namesArray = (0, sets_1.setToArray)(names);
var namesInStorage = this.getNames();
return namesArray.every(function (name) { return namesInStorage.indexOf(name) !== -1; });
};
RBSegmentsCacheInMemory.prototype.getChangeNumber = function () {
return this.changeNumber;
};
RBSegmentsCacheInMemory.prototype.usesSegments = function () {
return this.segmentsCount > 0;
};
return RBSegmentsCacheInMemory;
}());
exports.RBSegmentsCacheInMemory = RBSegmentsCacheInMemory;