@splitsoftware/splitio-react
Version:
A React library to easily integrate and use Split JS SDK
73 lines (72 loc) • 3.25 kB
JavaScript
import shallowEqual from 'shallowequal';
import { CONTROL, CONTROL_WITH_CONFIG } from './constants';
function isString(val) {
return typeof val === 'string' || val instanceof String;
}
// Utils used to access singleton instances of Split factories and clients:
export 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;
}
export function getStatus(client) {
return client ?
client.getStatus() :
{
isReady: false,
isReadyFromCache: false,
isTimedout: false,
hasTimedout: false,
isDestroyed: false,
isOperational: false,
lastUpdate: 0,
};
}
// Manage client attributes binding
// @TODO should reset attributes rather than set/merge them, to keep SFP and hooks pure.
export function initAttributes(client, attributes) {
if (client && attributes)
client.setAttributes(attributes);
}
export 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 ? CONTROL_WITH_CONFIG : CONTROL;
}
export 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;
}, {});
}
/**
* 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.
*/
export function argsAreEqual(newArgs, lastArgs) {
return newArgs[0] === lastArgs[0] && // client
newArgs[1] === lastArgs[1] && // lastUpdate
shallowEqual(newArgs[2], lastArgs[2]) && // names
shallowEqual(newArgs[3], lastArgs[3]) && // attributes
shallowEqual(newArgs[4], lastArgs[4]) && // client attributes
shallowEqual(newArgs[5], lastArgs[5]); // flagSets
}