@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
62 lines (61 loc) • 2.68 kB
JavaScript
import { isObject } from '../utils/lang';
import { isConsumerMode } from '../utils/settingsValidation/mode';
/**
* Validates if the given rollout plan is valid.
*/
export function validateRolloutPlan(log, settings) {
var mode = settings.mode, initialRolloutPlan = settings.initialRolloutPlan;
if (isConsumerMode(mode)) {
log.warn('storage: initial rollout plan is ignored in consumer mode');
return;
}
if (isObject(initialRolloutPlan) && isObject(initialRolloutPlan.splitChanges))
return initialRolloutPlan;
log.error('storage: invalid rollout plan provided');
return;
}
/**
* Sets the given synchronous storage with the provided rollout plan snapshot.
* If `matchingKey` is provided, the storage is handled as a client-side storage (segments and largeSegments are instances of MySegmentsCache).
* Otherwise, the storage is handled as a server-side storage (segments is an instance of SegmentsCache).
*/
export function setRolloutPlan(log, rolloutPlan, storage, matchingKey) {
var definitions = storage.definitions, rbSegments = storage.rbSegments, segments = storage.segments, largeSegments = storage.largeSegments;
var _a = rolloutPlan.splitChanges, ff = _a.ff, rbs = _a.rbs;
log.debug("storage: set feature flags and segments".concat(matchingKey ? " for key ".concat(matchingKey) : ''));
if (definitions && ff) {
definitions.clear();
definitions.update(ff.d || [], [], ff.t);
}
if (rbSegments && rbs) {
rbSegments.clear();
rbSegments.update(rbs.d || [], [], rbs.t);
}
var segmentChanges = rolloutPlan.segmentChanges;
if (matchingKey) { // add memberships data (client-side)
var memberships = rolloutPlan.memberships && rolloutPlan.memberships[matchingKey];
if (!memberships && segmentChanges) {
memberships = {
ms: {
k: segmentChanges.filter(function (segment) {
return segment.added.indexOf(matchingKey) > -1;
}).map(function (segment) { return ({ n: segment.name }); })
}
};
}
if (memberships) {
if (memberships.ms)
segments.resetSegments(memberships.ms);
if (memberships.ls && largeSegments)
largeSegments.resetSegments(memberships.ls);
}
}
else { // add segments data (server-side)
if (segmentChanges) {
segments.clear();
segmentChanges.forEach(function (segment) {
segments.update(segment.name, segment.added, segment.removed, segment.till);
});
}
}
}