UNPKG

@splitsoftware/splitio-commons

Version:
76 lines (75 loc) 3.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usesSegmentsSync = exports.usesSegments = exports.AbstractSplitsCacheSync = void 0; var objectAssign_1 = require("../utils/lang/objectAssign"); var constants_1 = require("../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 = (0, objectAssign_1.objectAssign)({}, split); newSplit.killed = true; newSplit.defaultTreatment = defaultTreatment; newSplit.changeNumber = changeNumber; return this.addSplit(newSplit); } return false; }; return AbstractSplitsCacheSync; }()); exports.AbstractSplitsCacheSync = 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 */ 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.splits.usesSegments() || storage.rbSegments.usesSegments(); } exports.usesSegmentsSync = usesSegmentsSync;