UNPKG

@splitsoftware/splitio-react

Version:

A React library to easily integrate and use Split JS SDK

47 lines (46 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTreatmentsWithConfig = void 0; var tslib_1 = require("tslib"); var React = (0, tslib_1.__importStar)(require("react")); var memoize_one_1 = (0, tslib_1.__importDefault)(require("memoize-one")); var utils_1 = require("./utils"); var useSplitClient_1 = require("./useSplitClient"); function evaluateFeatureFlagsWithConfig(client, _lastUpdate, names, attributes, _clientAttributes, flagSets, options, factory) { return client && client.getStatus().isOperational && (names || flagSets) ? names ? client.getTreatmentsWithConfig(names, attributes, options) : client.getTreatmentsWithConfigByFlagSets(flagSets, attributes, options) : names ? (0, utils_1.getTreatments)(names, true, factory) : {}; // empty object when evaluating with flag sets and client is not ready } function memoizeGetTreatmentsWithConfig() { return (0, memoize_one_1.default)(evaluateFeatureFlagsWithConfig, utils_1.argsAreEqual); } /** * `useTreatmentsWithConfig` is a hook that returns an Split Context object extended with a `treatments` property object that contains feature flag evaluations. * It uses the `useSplitClient` hook to access the client, and invokes the `client.getTreatmentsWithConfig()` method if the `names` option is provided, * or the `client.getTreatmentsWithConfigByFlagSets()` method if the `flagSets` option is provided. * * @param options - An options object with a list of feature flag names or flag sets to evaluate, and an optional `attributes` and `splitKey` values to configure the client. * @returns A Split Context object extended with a TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if feature flag names do not exist. * * @example * ```js * const { treatments: { feature_1, feature_2 }, isReady, isReadyFromCache, hasTimedout, lastUpdate, ... } = useTreatmentsWithConfig({ names: ['feature_1', 'feature_2']}); * ``` * * @see {@link https://developer.harness.io/docs/feature-management-experimentation/sdks-and-infrastructure/client-side-sdks/javascript-sdk/#get-treatments-with-configurations} */ function useTreatmentsWithConfig(options) { var context = (0, useSplitClient_1.useSplitClient)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, options), { attributes: undefined })); var factory = context.factory, client = context.client, lastUpdate = context.lastUpdate; var names = options.names, flagSets = options.flagSets, attributes = options.attributes, properties = options.properties; var getTreatmentsWithConfig = React.useMemo(memoizeGetTreatmentsWithConfig, []); // Shallow copy `client.getAttributes` result for memoization, as it returns the same reference unless `client.clearAttributes` is invoked. // Note: the same issue occurs with the `names` and `attributes` arguments if they are mutated directly by the user instead of providing a new object. var treatments = getTreatmentsWithConfig(client, lastUpdate, names, attributes, client ? (0, tslib_1.__assign)({}, client.getAttributes()) : {}, flagSets, properties && { properties: properties }, factory); return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, context), { treatments: treatments }); } exports.useTreatmentsWithConfig = useTreatmentsWithConfig;