UNPKG

@splitsoftware/splitio-commons

Version:
42 lines (41 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.conditionContext = void 0; var engineUtils_1 = require("./engineUtils"); var thenable_1 = require("../../utils/promise/thenable"); var labels_1 = require("../../utils/labels"); // Build Evaluation object if and only if matchingResult is true function match(log, matchingResult, bucketingKey, seed, treatments, label) { if (matchingResult) { return treatments ? // Feature flag { treatment: (0, engineUtils_1.getTreatment)(log, bucketingKey, seed, treatments), label: label } : // Rule-based segment true; } // else we should notify the engine to continue evaluating return undefined; } // Condition factory function conditionContext(log, matcherEvaluator, treatments, label, conditionType) { return function conditionEvaluator(key, seed, trafficAllocation, trafficAllocationSeed, attributes, splitEvaluator) { // Whitelisting has more priority than traffic allocation, so we don't apply this filtering to those conditions. if (conditionType === 'ROLLOUT' && !(0, engineUtils_1.shouldApplyRollout)(trafficAllocation, key.bucketingKey, trafficAllocationSeed)) { return { treatment: undefined, label: labels_1.NOT_IN_SPLIT }; } // matcherEvaluator could be Async, this relays on matchers return value, so we need // to verify for thenable before play with the result. // Also, we pass splitEvaluator function in case we have a matcher that needs to evaluate another split, // as well as the entire key object for the same reason. var matches = matcherEvaluator(key, attributes, splitEvaluator); if ((0, thenable_1.thenable)(matches)) { return matches.then(function (result) { return match(log, result, key.bucketingKey, seed, treatments, label); }); } return match(log, matches, key.bucketingKey, seed, treatments, label); }; } exports.conditionContext = conditionContext;