Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- `useSplitTreatments` optimizes feature flag evaluations by using the `useMemo` hook to memoize `getTreatmentsWithConfig` method calls from the SDK. This avoids re-evaluating feature flags when the hook is called with the same options and the feature flag definitions have not changed.
- They fixed a bug in the deprecated `useClient` and `useTreatments` hooks, which caused them to not re-render and re-evaluate feature flags when they access a different SDK client than the context and its status updates (i.e., when it emits SDK_READY or other event).
- Added TypeScript types and interfaces to the library index exports, allowing them to be imported from the library index, e.g., `import type { ISplitFactoryProps } from '@splitsoftware/splitio-react'` (Related to issue https://github.com/splitio/react-client/issues/162).
- Added `defaultTreatment` property to the `SplitView` object returned by the `split` and `splits` methods of the SDK manager (Related to issue https://github.com/splitio/javascript-commons/issues/225).
- Updated type declarations of the library components to not restrict the type of the `children` prop to ReactElement, allowing to pass any valid ReactNode value (Related to issue https://github.com/splitio/react-client/issues/164).
- Updated linter and other dependencies for vulnerability fixes.
- Bugfixing - Removed conditional code within hooks to adhere to the rules of hooks and prevent React warnings. Previously, this code checked for the availability of the hooks API (available in React version 16.8.0 or above) and logged an error message. Now, using hooks with React versions below 16.8.0 will throw an error.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getControlTreatmentsWithConfig, CONTROL_WITH_CONFIG } from '../constants';
import { CONTROL_WITH_CONFIG } from '../constants';
import { getControlTreatmentsWithConfig } from '../utils';
import SplitIO from '@splitsoftware/splitio/types/splitio';

describe('getControlTreatmentsWithConfig', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/withSplitTreatments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { sdkBrowser } from './testUtils/sdkConfigs';
import { withSplitFactory } from '../withSplitFactory';
import { withSplitClient } from '../withSplitClient';
import { withSplitTreatments } from '../withSplitTreatments';
import { getControlTreatmentsWithConfig } from '../constants';
import { getControlTreatmentsWithConfig } from '../utils';

describe('withSplitTreatments', () => {

Expand Down
16 changes: 0 additions & 16 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { validateFeatureFlags } from './utils';

// The string below is a marker and will be replaced by the real version number. DO NOT CHANGE
export const VERSION: string = 'react-' + 'REACT_SDK_VERSION_NUMBER';

Expand All @@ -15,20 +13,6 @@ export const CONTROL_WITH_CONFIG: SplitIO.TreatmentWithConfig = {
config: null,
};

export const getControlTreatmentsWithConfig = (featureFlagNames: unknown): SplitIO.TreatmentsWithConfig => {
// validate featureFlags Names
const validatedFeatureFlagNames = validateFeatureFlags(featureFlagNames);

// return empty object if the returned value is false
if (!validatedFeatureFlagNames) return {};

// return control treatments for each validated feature flag name
return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
pValue[cValue] = CONTROL_WITH_CONFIG;
return pValue;
}, {});
};

// Warning and error messages
export const WARN_SF_CONFIG_AND_FACTORY: string = '[WARN] Both a config and factory props were provided to SplitFactory. Config prop will be ignored.';

Expand Down
32 changes: 23 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import memoizeOne from 'memoize-one';
import shallowEqual from 'shallowequal';
import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client';
import { VERSION, WARN_NAMES_AND_FLAGSETS, getControlTreatmentsWithConfig } from './constants';
import { CONTROL_WITH_CONFIG, VERSION, WARN_NAMES_AND_FLAGSETS } from './constants';
import { ISplitStatus } from './types';

// Utils used to access singleton instances of Split factories and clients, and to gracefully shutdown all clients together.
Expand Down Expand Up @@ -97,9 +97,16 @@ export function getStatus(client: SplitIO.IBrowserClient | null): ISplitStatus {
};
}

/**
* Manage client attributes binding
*/
export function initAttributes(client: SplitIO.IBrowserClient | null, attributes?: SplitIO.Attributes) {
if (client && attributes) client.setAttributes(attributes);
}

// Input validation utils that will be replaced eventually

export function validateFeatureFlags(maybeFeatureFlags: unknown, listName = 'feature flag names'): false | string[] {
function validateFeatureFlags(maybeFeatureFlags: unknown, listName = 'feature flag names'): false | string[] {
if (Array.isArray(maybeFeatureFlags) && maybeFeatureFlags.length > 0) {
const validatedArray: string[] = [];
// Remove invalid values
Expand All @@ -116,13 +123,6 @@ export function validateFeatureFlags(maybeFeatureFlags: unknown, listName = 'fea
return false;
}

/**
* Manage client attributes binding
*/
export function initAttributes(client: SplitIO.IBrowserClient | null, attributes?: SplitIO.Attributes) {
if (client && attributes) client.setAttributes(attributes);
}

const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;

function validateFeatureFlag(maybeFeatureFlag: unknown, item = 'feature flag name'): false | string {
Expand All @@ -146,6 +146,20 @@ function validateFeatureFlag(maybeFeatureFlag: unknown, item = 'feature flag nam
return false;
}

export function getControlTreatmentsWithConfig(featureFlagNames: unknown): SplitIO.TreatmentsWithConfig {
// validate featureFlags Names
const validatedFeatureFlagNames = validateFeatureFlags(featureFlagNames);

// return empty object if the returned value is false
if (!validatedFeatureFlagNames) return {};

// return control treatments for each validated feature flag name
return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
pValue[cValue] = CONTROL_WITH_CONFIG;
return pValue;
}, {});
}

/**
* Removes duplicate items on an array of strings.
*/
Expand Down