Skip to content

Commit 65ad71c

Browse files
Merge pull request #178 from splitio/issue_176_circular_dependency
Refactor to avoid circular dependency (issue #176)
2 parents 311fc55 + ab8bbaf commit 65ad71c

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `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.
1616
- 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).
1717
- 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).
18+
- 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).
1819
- 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).
1920
- Updated linter and other dependencies for vulnerability fixes.
2021
- 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.

src/__tests__/constants.test.ts renamed to src/__tests__/utils.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getControlTreatmentsWithConfig, CONTROL_WITH_CONFIG } from '../constants';
1+
import { CONTROL_WITH_CONFIG } from '../constants';
2+
import { getControlTreatmentsWithConfig } from '../utils';
23
import SplitIO from '@splitsoftware/splitio/types/splitio';
34

45
describe('getControlTreatmentsWithConfig', () => {

src/__tests__/withSplitTreatments.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { sdkBrowser } from './testUtils/sdkConfigs';
1212
import { withSplitFactory } from '../withSplitFactory';
1313
import { withSplitClient } from '../withSplitClient';
1414
import { withSplitTreatments } from '../withSplitTreatments';
15-
import { getControlTreatmentsWithConfig } from '../constants';
15+
import { getControlTreatmentsWithConfig } from '../utils';
1616

1717
describe('withSplitTreatments', () => {
1818

src/constants.ts

-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { validateFeatureFlags } from './utils';
2-
31
// The string below is a marker and will be replaced by the real version number. DO NOT CHANGE
42
export const VERSION: string = 'react-' + 'REACT_SDK_VERSION_NUMBER';
53

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

18-
export const getControlTreatmentsWithConfig = (featureFlagNames: unknown): SplitIO.TreatmentsWithConfig => {
19-
// validate featureFlags Names
20-
const validatedFeatureFlagNames = validateFeatureFlags(featureFlagNames);
21-
22-
// return empty object if the returned value is false
23-
if (!validatedFeatureFlagNames) return {};
24-
25-
// return control treatments for each validated feature flag name
26-
return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
27-
pValue[cValue] = CONTROL_WITH_CONFIG;
28-
return pValue;
29-
}, {});
30-
};
31-
3216
// Warning and error messages
3317
export const WARN_SF_CONFIG_AND_FACTORY: string = '[WARN] Both a config and factory props were provided to SplitFactory. Config prop will be ignored.';
3418

src/utils.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import memoizeOne from 'memoize-one';
22
import shallowEqual from 'shallowequal';
33
import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client';
4-
import { VERSION, WARN_NAMES_AND_FLAGSETS, getControlTreatmentsWithConfig } from './constants';
4+
import { CONTROL_WITH_CONFIG, VERSION, WARN_NAMES_AND_FLAGSETS } from './constants';
55
import { ISplitStatus } from './types';
66

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

100+
/**
101+
* Manage client attributes binding
102+
*/
103+
export function initAttributes(client: SplitIO.IBrowserClient | null, attributes?: SplitIO.Attributes) {
104+
if (client && attributes) client.setAttributes(attributes);
105+
}
106+
100107
// Input validation utils that will be replaced eventually
101108

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

119-
/**
120-
* Manage client attributes binding
121-
*/
122-
export function initAttributes(client: SplitIO.IBrowserClient | null, attributes?: SplitIO.Attributes) {
123-
if (client && attributes) client.setAttributes(attributes);
124-
}
125-
126126
const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;
127127

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

149+
export function getControlTreatmentsWithConfig(featureFlagNames: unknown): SplitIO.TreatmentsWithConfig {
150+
// validate featureFlags Names
151+
const validatedFeatureFlagNames = validateFeatureFlags(featureFlagNames);
152+
153+
// return empty object if the returned value is false
154+
if (!validatedFeatureFlagNames) return {};
155+
156+
// return control treatments for each validated feature flag name
157+
return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
158+
pValue[cValue] = CONTROL_WITH_CONFIG;
159+
return pValue;
160+
}, {});
161+
}
162+
149163
/**
150164
* Removes duplicate items on an array of strings.
151165
*/

0 commit comments

Comments
 (0)