@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
108 lines (107 loc) • 4.73 kB
JavaScript
import { isFiniteNumber, isNaNNumber, toNumber } from '../../utils/lang';
import { setToArray } from '../../utils/lang/sets';
import { usesSegments } from '../AbstractDefinitionsCacheSync';
import { LOG_PREFIX } from './constants';
var RBSegmentsCacheInLocal = /** @class */ (function () {
function RBSegmentsCacheInLocal(settings, keys, storage) {
this.keys = keys;
this.log = settings.log;
this.storage = storage;
}
RBSegmentsCacheInLocal.prototype.clear = function () {
var _this = this;
this.getNames().forEach(function (name) { return _this.remove(name); });
this.storage.removeItem(this.keys.buildRBSegmentsTillKey());
};
RBSegmentsCacheInLocal.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.setChangeNumber(changeNumber);
return updated;
};
RBSegmentsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
try {
this.storage.setItem(this.keys.buildRBSegmentsTillKey(), changeNumber + '');
this.storage.setItem(this.keys.buildLastUpdatedKey(), Date.now() + '');
}
catch (e) {
this.log.error(LOG_PREFIX + e);
}
};
RBSegmentsCacheInLocal.prototype.updateSegmentCount = function (diff) {
var segmentsCountKey = this.keys.buildDefinitionsWithSegmentCountKey();
var count = toNumber(this.storage.getItem(segmentsCountKey)) + diff;
if (count > 0)
this.storage.setItem(segmentsCountKey, count + '');
else
this.storage.removeItem(segmentsCountKey);
};
RBSegmentsCacheInLocal.prototype.add = function (rbSegment) {
var name = rbSegment.name;
var rbSegmentKey = this.keys.buildRBSegmentKey(name);
var rbSegmentFromStorage = this.storage.getItem(rbSegmentKey);
var previous = rbSegmentFromStorage ? JSON.parse(rbSegmentFromStorage) : null;
this.storage.setItem(rbSegmentKey, JSON.stringify(rbSegment));
var usesSegmentsDiff = 0;
if (previous && usesSegments(previous))
usesSegmentsDiff--;
if (usesSegments(rbSegment))
usesSegmentsDiff++;
if (usesSegmentsDiff !== 0)
this.updateSegmentCount(usesSegmentsDiff);
return true;
};
RBSegmentsCacheInLocal.prototype.remove = function (name) {
var rbSegment = this.get(name);
if (!rbSegment)
return false;
this.storage.removeItem(this.keys.buildRBSegmentKey(name));
if (usesSegments(rbSegment))
this.updateSegmentCount(-1);
return true;
};
RBSegmentsCacheInLocal.prototype.getNames = function () {
var len = this.storage.length;
var accum = [];
var cur = 0;
while (cur < len) {
var key = this.storage.key(cur);
if (key != null && this.keys.isRBSegmentKey(key))
accum.push(this.keys.extractKey(key));
cur++;
}
return accum;
};
RBSegmentsCacheInLocal.prototype.get = function (name) {
var item = this.storage.getItem(this.keys.buildRBSegmentKey(name));
return item && JSON.parse(item);
};
RBSegmentsCacheInLocal.prototype.getAll = function () {
var _this = this;
return this.getNames().map(function (key) { return _this.get(key); });
};
RBSegmentsCacheInLocal.prototype.contains = function (names) {
var namesArray = setToArray(names);
var namesInStorage = this.getNames();
return namesArray.every(function (name) { return namesInStorage.indexOf(name) !== -1; });
};
RBSegmentsCacheInLocal.prototype.getChangeNumber = function () {
var n = -1;
var value = this.storage.getItem(this.keys.buildRBSegmentsTillKey());
if (value !== null) {
value = parseInt(value, 10);
return isNaNNumber(value) ? n : value;
}
return n;
};
RBSegmentsCacheInLocal.prototype.usesSegments = function () {
var storedCount = this.storage.getItem(this.keys.buildDefinitionsWithSegmentCountKey());
var definitionsWithSegmentsCount = storedCount === null ? 0 : toNumber(storedCount);
return isFiniteNumber(definitionsWithSegmentsCount) ?
definitionsWithSegmentsCount > 0 :
true;
};
return RBSegmentsCacheInLocal;
}());
export { RBSegmentsCacheInLocal };