Skip to content

Commit 202939c

Browse files
refactor: reorganize utils.ts with better code structure and documentation
1 parent 94dbb33 commit 202939c

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

MIGRATION-GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ React SDK v2.0.0 has a few breaking changes that you should consider when migrat
99
Since v2.6.0, there are 4 hooks variants to evaluate feature flags, to better cover the different evaluation methods available in the JavaScript SDK client:
1010

1111
- `useTreatment`: returns a treatment value for a given feature flag name. It calls `client.getTreatment()` method under the hood.
12-
- `useTreatmentWithConfig`: returns a single treatment value and its configuration for a given feature flag name. It calls `client.getTreatmentWithConfig()` method under the hood.
12+
- `useTreatmentWithConfig`: returns a treatment value and its configuration for a given feature flag name. It calls `client.getTreatmentWithConfig()` method under the hood.
1313
- `useTreatments`: returns an object with treatment values for multiple feature flag names. It calls `client.getTreatments()` or `client.getTreatmentsByFlagSets()` methods under the hood, depending if the `names` or `flagSets` option is provided.
1414
- `useTreatmentsWithConfig`: returns an object with treatment values and their configurations for multiple feature flag names. It calls `client.getTreatmentsWithConfig()` or `client.getTreatmentsWithConfigByFlagSets()` methods under the hood, depending if the `names` or `flagSets` option is provided.
1515

src/utils.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import shallowEqual from 'shallowequal';
33
import { CONTROL, CONTROL_WITH_CONFIG } from './constants';
44
import { ISplitStatus } from './types';
55

6-
// Utils used to access singleton instances of Split factories and clients
7-
8-
export interface IFactoryWithLazyInit extends SplitIO.IBrowserSDK {
9-
config: SplitIO.IBrowserSettings;
10-
init(): void;
6+
function isString(val: unknown): val is string {
7+
return typeof val === 'string' || val instanceof String;
118
}
129

13-
// idempotent operation
10+
// Utils used to access singleton instances of Split factories and clients:
11+
1412
export function getSplitClient(factory: SplitIO.IBrowserSDK, key?: SplitIO.SplitKey): SplitIO.IBrowserClient {
1513
// factory.client is an idempotent operation
1614
const client = key !== undefined ? factory.client(key) : factory.client();
@@ -22,7 +20,6 @@ export function getSplitClient(factory: SplitIO.IBrowserSDK, key?: SplitIO.Split
2220
return client;
2321
}
2422

25-
// Util used to get client status.
2623
export function getStatus(client?: SplitIO.IBrowserClient): ISplitStatus {
2724
return client ?
2825
client.getStatus() :
@@ -37,9 +34,18 @@ export function getStatus(client?: SplitIO.IBrowserClient): ISplitStatus {
3734
};
3835
}
3936

37+
// Manage client attributes binding
38+
// @TODO should reset attributes rather than set/merge them, to keep SFP and hooks pure.
39+
export function initAttributes(client?: SplitIO.IBrowserClient, attributes?: SplitIO.Attributes) {
40+
if (client && attributes) client.setAttributes(attributes);
41+
}
42+
43+
// Utils used to retrieve treatments when the client is not operational:
44+
4045
function resolveFallback(flagName: string, withConfig: true, factory?: SplitIO.IBrowserSDK): SplitIO.TreatmentWithConfig;
4146
function resolveFallback(flagName: string, withConfig: false, factory?: SplitIO.IBrowserSDK): SplitIO.Treatment;
42-
function resolveFallback(flagName: string, withConfig: boolean, factory?: SplitIO.IBrowserSDK): SplitIO.Treatment | SplitIO.TreatmentWithConfig {
47+
function resolveFallback(flagName: string, withConfig: boolean, factory?: SplitIO.IBrowserSDK): SplitIO.Treatment | SplitIO.TreatmentWithConfig;
48+
function resolveFallback(flagName: string, withConfig: boolean, factory?: SplitIO.IBrowserSDK) {
4349
if (factory && factory.settings.fallbackTreatments) {
4450
const fallbacks = factory.settings.fallbackTreatments;
4551

@@ -55,17 +61,9 @@ function resolveFallback(flagName: string, withConfig: boolean, factory?: SplitI
5561
return withConfig ? CONTROL_WITH_CONFIG : CONTROL;
5662
}
5763

58-
/**
59-
* Manage client attributes binding
60-
*/
61-
// @TODO should reset attributes rather than set/merge them, to keep SFP and hooks pure.
62-
export function initAttributes(client?: SplitIO.IBrowserClient, attributes?: SplitIO.Attributes) {
63-
if (client && attributes) client.setAttributes(attributes);
64-
}
65-
6664
export function getControlTreatments(featureFlagNames: unknown, withConfig: true, factory?: SplitIO.IBrowserSDK): SplitIO.TreatmentsWithConfig;
6765
export function getControlTreatments(featureFlagNames: unknown, withConfig: false, factory?: SplitIO.IBrowserSDK): SplitIO.Treatments;
68-
export function getControlTreatments(featureFlagNames: unknown, withConfig: boolean, factory?: SplitIO.IBrowserSDK): SplitIO.Treatments | SplitIO.TreatmentsWithConfig {
66+
export function getControlTreatments(featureFlagNames: unknown, withConfig: boolean, factory?: SplitIO.IBrowserSDK) {
6967
// validate feature flag names
7068
if (!Array.isArray(featureFlagNames)) return {};
7169

@@ -76,18 +74,15 @@ export function getControlTreatments(featureFlagNames: unknown, withConfig: bool
7674

7775
// return control or fallback treatment for each validated feature flag name
7876
return (featureFlagNames as string[]).reduce((pValue: SplitIO.Treatments | SplitIO.TreatmentsWithConfig, featureFlagName: string) => {
79-
// @ts-expect-error asd
8077
pValue[featureFlagName] = resolveFallback(featureFlagName, withConfig, factory);
8178
return pValue;
8279
}, {});
8380
}
8481

8582
/**
86-
* Checks if a given value is a string.
83+
* Utils to memoize `client.getTreatments*` method calls to avoid duplicated impressions.
84+
* The result treatments are the same given the same `client` instance, `lastUpdate` timestamp, and list of feature flag names and attributes.
8785
*/
88-
function isString(val: unknown): val is string {
89-
return typeof val === 'string' || val instanceof String;
90-
}
9186

9287
function argsAreEqual(newArgs: any[], lastArgs: any[]): boolean {
9388
return newArgs[0] === lastArgs[0] && // client
@@ -108,10 +103,6 @@ function evaluateFeatureFlagsWithConfig(client: SplitIO.IBrowserClient | undefin
108103
{} // empty object when evaluating with flag sets and client is not ready
109104
}
110105

111-
/**
112-
* Gets a memoized version of the `client.getTreatmentsWithConfig` method.
113-
* It is used to avoid duplicated impressions, because the result treatments are the same given the same `client` instance, `lastUpdate` timestamp, and list of feature flag `names` and `attributes`.
114-
*/
115106
export function memoizeGetTreatmentsWithConfig() {
116107
return memoizeOne(evaluateFeatureFlagsWithConfig, argsAreEqual);
117108
}

0 commit comments

Comments
 (0)