@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
52 lines (51 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractSplitsCacheAsync = void 0;
var objectAssign_1 = require("../utils/lang/objectAssign");
/**
* This class provides a skeletal implementation of the ISplitsCacheAsync interface
* to minimize the effort required to implement this interface.
*/
var AbstractSplitsCacheAsync = /** @class */ (function () {
function AbstractSplitsCacheAsync() {
}
AbstractSplitsCacheAsync.prototype.update = function (toAdd, toRemove, changeNumber) {
var _this = this;
return Promise.all([
this.setChangeNumber(changeNumber),
Promise.all(toAdd.map(function (addedFF) { return _this.addSplit(addedFF); })),
Promise.all(toRemove.map(function (removedFF) { return _this.removeSplit(removedFF.name); }))
]).then(function (_a) {
var added = _a[1], removed = _a[2];
return added.some(function (result) { return result; }) || removed.some(function (result) { return result; });
});
};
// @TODO revisit segment-related methods ('usesSegments', 'getRegisteredSegments', 'registerSegments')
// noop, just keeping the interface. This is used by standalone client-side API only, and so only implemented by InMemory and InLocalStorage.
AbstractSplitsCacheAsync.prototype.usesSegments = function () {
return Promise.resolve(true);
};
/**
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
* Used for SPLIT_KILL push notifications.
*
* @returns a promise that is resolved once the split kill operation is performed. The fulfillment value is a boolean: `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.
* The promise will never be rejected.
*/
AbstractSplitsCacheAsync.prototype.killLocally = function (name, defaultTreatment, changeNumber) {
var _this = this;
return this.getSplit(name).then(function (split) {
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;
}).catch(function () { return false; });
};
return AbstractSplitsCacheAsync;
}());
exports.AbstractSplitsCacheAsync = AbstractSplitsCacheAsync;