UNPKG

@splitsoftware/splitio-commons

Version:
49 lines (48 loc) 2.67 kB
import { objectAssign } from '../utils/lang/objectAssign'; /** * This class provides a skeletal implementation of the IDefinitionsCacheAsync interface * to minimize the effort required to implement this interface. */ var AbstractDefinitionsCacheAsync = /** @class */ (function () { function AbstractDefinitionsCacheAsync() { } AbstractDefinitionsCacheAsync.prototype.update = function (toAdd, toRemove, changeNumber) { var _this = this; return Promise.all([ this.setChangeNumber(changeNumber), Promise.all(toAdd.map(function (addedFF) { return _this.add(addedFF); })), Promise.all(toRemove.map(function (removedFF) { return _this.remove(removedFF); })) ]).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. AbstractDefinitionsCacheAsync.prototype.usesSegments = function () { return Promise.resolve(true); }; /** * Kill `name` definition and set `defaultTreatment` and `changeNumber`. * Used for SPLIT_KILL push notifications. * * @returns a promise that is resolved once the definition kill operation is performed. The fulfillment value is a boolean: `true` if the operation succeeded 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. * The promise will never be rejected. */ AbstractDefinitionsCacheAsync.prototype.killLocally = function (name, defaultTreatment, changeNumber) { var _this = this; return this.get(name).then(function (definition) { 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; }).catch(function () { return false; }); }; return AbstractDefinitionsCacheAsync; }()); export { AbstractDefinitionsCacheAsync };