@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
68 lines (67 loc) • 3.07 kB
JavaScript
import { objectAssign } from '../utils/lang/objectAssign';
import { IN_SEGMENT, IN_LARGE_SEGMENT } from '../utils/constants';
/**
* This class provides a skeletal implementation of the ISplitsCacheSync interface
* to minimize the effort required to implement this interface.
*/
var AbstractSplitsCacheSync = /** @class */ (function () {
function AbstractSplitsCacheSync() {
}
AbstractSplitsCacheSync.prototype.update = function (toAdd, toRemove, changeNumber) {
var _this = this;
this.setChangeNumber(changeNumber);
var updated = toAdd.map(function (addedFF) { return _this.addSplit(addedFF); }).some(function (result) { return result; });
return toRemove.map(function (removedFF) { return _this.removeSplit(removedFF.name); }).some(function (result) { return result; }) || updated;
};
AbstractSplitsCacheSync.prototype.getSplits = function (names) {
var _this = this;
var splits = {};
names.forEach(function (name) {
splits[name] = _this.getSplit(name);
});
return splits;
};
AbstractSplitsCacheSync.prototype.getAll = function () {
var _this = this;
return this.getSplitNames().map(function (key) { return _this.getSplit(key); });
};
/**
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
* Used for SPLIT_KILL push notifications.
*
* @returns `true` if the operation successed updating the split, or `false` if no split is updated,
* for instance, if the `changeNumber` is old, or if the split is not found (e.g., `/splitchanges` hasn't been fetched yet), or if the storage fails to apply the update.
*/
AbstractSplitsCacheSync.prototype.killLocally = function (name, defaultTreatment, changeNumber) {
var split = this.getSplit(name);
if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
var newSplit = objectAssign({}, split);
newSplit.killed = true;
newSplit.defaultTreatment = defaultTreatment;
newSplit.changeNumber = changeNumber;
return this.addSplit(newSplit);
}
return false;
};
return AbstractSplitsCacheSync;
}());
export { AbstractSplitsCacheSync };
/**
* Given a parsed split, it returns a boolean flagging if its conditions use segments matchers (rules & whitelists).
* This util is intended to simplify the implementation of `splitsCache::usesSegments` method
*/
export function usesSegments(ruleEntity) {
var conditions = ruleEntity.conditions || [];
for (var i = 0; i < conditions.length; i++) {
var matchers = conditions[i].matcherGroup.matchers;
for (var j = 0; j < matchers.length; j++) {
var matcher = matchers[j].matcherType;
if (matcher === IN_SEGMENT || matcher === IN_LARGE_SEGMENT)
return true;
}
}
var excluded = ruleEntity.excluded;
if (excluded && excluded.segments && excluded.segments.length > 0)
return true;
return false;
}