@splitsoftware/splitio-react
Version:
A React library to easily integrate and use Split JS SDK
83 lines (82 loc) • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.argsAreEqual = exports.getTreatments = exports.getTreatment = exports.initAttributes = exports.getStatus = exports.getSplitClient = void 0;
var tslib_1 = require("tslib");
var shallowequal_1 = (0, tslib_1.__importDefault)(require("shallowequal"));
var constants_1 = require("./constants");
function isString(val) {
return typeof val === 'string' || val instanceof String;
}
// Utils used to access singleton instances of Split factories and clients:
function getSplitClient(factory, key) {
// factory.client is an idempotent operation
var client = key !== undefined ? factory.client(key) : factory.client();
// Remove EventEmitter warning emitted when using multiple SDK hooks or components.
// Unlike JS SDK, users don't need to access the client directly, making the warning irrelevant.
client.setMaxListeners && client.setMaxListeners(0);
return client;
}
exports.getSplitClient = getSplitClient;
function getStatus(client) {
return client ?
client.getStatus() :
{
isReady: false,
isReadyFromCache: false,
isTimedout: false,
hasTimedout: false,
isDestroyed: false,
isOperational: false,
lastUpdate: 0,
};
}
exports.getStatus = getStatus;
// Manage client attributes binding
// @TODO should reset attributes rather than set/merge them, to keep SFP and hooks pure.
function initAttributes(client, attributes) {
if (client && attributes)
client.setAttributes(attributes);
}
exports.initAttributes = initAttributes;
function getTreatment(flagName, withConfig, factory) {
var _a;
if (factory && factory.settings.fallbackTreatments) {
var fallbacks = factory.settings.fallbackTreatments;
var treatment = ((_a = fallbacks.byFlag) === null || _a === void 0 ? void 0 : _a[flagName]) || fallbacks.global;
if (treatment) {
return isString(treatment) ?
withConfig ? { treatment: treatment, config: null } : treatment :
withConfig ? treatment : treatment.treatment;
}
}
return withConfig ? constants_1.CONTROL_WITH_CONFIG : constants_1.CONTROL;
}
exports.getTreatment = getTreatment;
function getTreatments(featureFlagNames, withConfig, factory) {
// validate feature flag names
if (!Array.isArray(featureFlagNames))
return {};
featureFlagNames = featureFlagNames
.filter(function (featureFlagName) { return isString(featureFlagName); })
.map(function (featureFlagName) { return featureFlagName.trim(); })
.filter(function (featureFlagName) { return featureFlagName.length > 0; });
// return control or fallback treatment for each validated feature flag name
return featureFlagNames.reduce(function (pValue, featureFlagName) {
pValue[featureFlagName] = getTreatment(featureFlagName, withConfig, factory);
return pValue;
}, {});
}
exports.getTreatments = getTreatments;
/**
* Utils to memoize `client.getTreatments*` method calls to avoid duplicated impressions.
* The result treatments are the same given the same `client` instance, `lastUpdate` timestamp, and list of feature flag names and attributes.
*/
function argsAreEqual(newArgs, lastArgs) {
return newArgs[0] === lastArgs[0] && // client
newArgs[1] === lastArgs[1] && // lastUpdate
(0, shallowequal_1.default)(newArgs[2], lastArgs[2]) && // names
(0, shallowequal_1.default)(newArgs[3], lastArgs[3]) && // attributes
(0, shallowequal_1.default)(newArgs[4], lastArgs[4]) && // client attributes
(0, shallowequal_1.default)(newArgs[5], lastArgs[5]); // flagSets
}
exports.argsAreEqual = argsAreEqual;