@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
72 lines (71 loc) • 3.32 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 IDefinitionsCacheSync interface
* to minimize the effort required to implement this interface.
*/
var AbstractDefinitionsCacheSync = /** @class */ (function () {
function AbstractDefinitionsCacheSync() {
}
AbstractDefinitionsCacheSync.prototype.update = function (toAdd, toRemove, changeNumber) {
var _this = this;
var updated = toAdd.map(function (addedFF) { return _this.add(addedFF); }).some(function (result) { return result; });
updated = toRemove.map(function (removedFF) { return _this.remove(removedFF); }).some(function (result) { return result; }) || updated;
this.setChangeNumber(changeNumber);
return updated;
};
AbstractDefinitionsCacheSync.prototype.getMany = function (names) {
var _this = this;
var definitions = {};
names.forEach(function (name) {
definitions[name] = _this.get(name);
});
return definitions;
};
AbstractDefinitionsCacheSync.prototype.getAll = function () {
var _this = this;
return this.getNames().map(function (key) { return _this.get(key); });
};
/**
* Kill `name` definition and set `defaultTreatment` and `changeNumber`.
* Used for SPLIT_KILL push notifications.
*
* @returns `true` if the operation successed updating the definition, or `false` if no definition is updated,
* for instance, if the `changeNumber` is old, or if the definition is not found (e.g., `/splitchanges` hasn't been fetched yet), or if the storage fails to apply the update.
*/
AbstractDefinitionsCacheSync.prototype.killLocally = function (name, defaultTreatment, changeNumber) {
var definition = this.get(name);
if (definition && (!definition.changeNumber || definition.changeNumber < changeNumber)) {
var newDefinition = objectAssign({}, definition);
newDefinition.killed = true;
newDefinition.defaultTreatment = defaultTreatment;
newDefinition.changeNumber = changeNumber;
return this.add(newDefinition);
}
return false;
};
return AbstractDefinitionsCacheSync;
}());
export { AbstractDefinitionsCacheSync };
/**
* Given a parsed definition, it returns a boolean flagging if its conditions use segments matchers (rules & whitelists).
* This util is intended to simplify the implementation of `definitionsCache::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;
}
export function usesSegmentsSync(storage) {
return storage.definitions.usesSegments() || storage.rbSegments.usesSegments();
}