UNPKG

@splitsoftware/splitio-commons

Version:
77 lines (76 loc) 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usesSegmentsSync = exports.usesSegments = exports.AbstractDefinitionsCacheSync = void 0; var objectAssign_1 = require("../utils/lang/objectAssign"); var constants_1 = require("../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 = (0, objectAssign_1.objectAssign)({}, definition); newDefinition.killed = true; newDefinition.defaultTreatment = defaultTreatment; newDefinition.changeNumber = changeNumber; return this.add(newDefinition); } return false; }; return AbstractDefinitionsCacheSync; }()); exports.AbstractDefinitionsCacheSync = 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 */ 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 === constants_1.IN_SEGMENT || matcher === constants_1.IN_LARGE_SEGMENT) return true; } } var excluded = ruleEntity.excluded; if (excluded && excluded.segments && excluded.segments.length > 0) return true; return false; } exports.usesSegments = usesSegments; function usesSegmentsSync(storage) { return storage.definitions.usesSegments() || storage.rbSegments.usesSegments(); } exports.usesSegmentsSync = usesSegmentsSync;