1- import memoizeOne from 'memoize-one' ;
21import shallowEqual from 'shallowequal' ;
32import { CONTROL , CONTROL_WITH_CONFIG } from './constants' ;
43import { ISplitStatus } from './types' ;
@@ -40,12 +39,12 @@ export function initAttributes(client?: SplitIO.IBrowserClient, attributes?: Spl
4039 if ( client && attributes ) client . setAttributes ( attributes ) ;
4140}
4241
43- // Utils used to retrieve treatments when the client is not operational:
42+ // Utils used to retrieve fallback or control treatments when the client is not operational:
4443
45- function resolveFallback ( flagName : string , withConfig : true , factory ?: SplitIO . IBrowserSDK ) : SplitIO . TreatmentWithConfig ;
46- function resolveFallback ( flagName : string , withConfig : false , factory ?: SplitIO . IBrowserSDK ) : SplitIO . Treatment ;
47- function resolveFallback ( flagName : string , withConfig : boolean , factory ?: SplitIO . IBrowserSDK ) : SplitIO . Treatment | SplitIO . TreatmentWithConfig ;
48- function resolveFallback ( flagName : string , withConfig : boolean , factory ?: SplitIO . IBrowserSDK ) {
44+ export function getTreatment ( flagName : string , withConfig : true , factory ?: SplitIO . IBrowserSDK ) : SplitIO . TreatmentWithConfig ;
45+ export function getTreatment ( flagName : string , withConfig : false , factory ?: SplitIO . IBrowserSDK ) : SplitIO . Treatment ;
46+ export function getTreatment ( flagName : string , withConfig : boolean , factory ?: SplitIO . IBrowserSDK ) : SplitIO . Treatment | SplitIO . TreatmentWithConfig ;
47+ export function getTreatment ( flagName : string , withConfig : boolean , factory ?: SplitIO . IBrowserSDK ) {
4948 if ( factory && factory . settings . fallbackTreatments ) {
5049 const fallbacks = factory . settings . fallbackTreatments ;
5150
@@ -61,9 +60,9 @@ function resolveFallback(flagName: string, withConfig: boolean, factory?: SplitI
6160 return withConfig ? CONTROL_WITH_CONFIG : CONTROL ;
6261}
6362
64- export function getControlTreatments ( featureFlagNames : unknown , withConfig : true , factory ?: SplitIO . IBrowserSDK ) : SplitIO . TreatmentsWithConfig ;
65- export function getControlTreatments ( featureFlagNames : unknown , withConfig : false , factory ?: SplitIO . IBrowserSDK ) : SplitIO . Treatments ;
66- export function getControlTreatments ( featureFlagNames : unknown , withConfig : boolean , factory ?: SplitIO . IBrowserSDK ) {
63+ export function getTreatments ( featureFlagNames : unknown , withConfig : true , factory ?: SplitIO . IBrowserSDK ) : SplitIO . TreatmentsWithConfig ;
64+ export function getTreatments ( featureFlagNames : unknown , withConfig : false , factory ?: SplitIO . IBrowserSDK ) : SplitIO . Treatments ;
65+ export function getTreatments ( featureFlagNames : unknown , withConfig : boolean , factory ?: SplitIO . IBrowserSDK ) {
6766 // validate feature flag names
6867 if ( ! Array . isArray ( featureFlagNames ) ) return { } ;
6968
@@ -74,7 +73,7 @@ export function getControlTreatments(featureFlagNames: unknown, withConfig: bool
7473
7574 // return control or fallback treatment for each validated feature flag name
7675 return ( featureFlagNames as string [ ] ) . reduce ( ( pValue : SplitIO . Treatments | SplitIO . TreatmentsWithConfig , featureFlagName : string ) => {
77- pValue [ featureFlagName ] = resolveFallback ( featureFlagName , withConfig , factory ) ;
76+ pValue [ featureFlagName ] = getTreatment ( featureFlagName , withConfig , factory ) ;
7877 return pValue ;
7978 } , { } ) ;
8079}
@@ -84,59 +83,11 @@ export function getControlTreatments(featureFlagNames: unknown, withConfig: bool
8483 * The result treatments are the same given the same `client` instance, `lastUpdate` timestamp, and list of feature flag names and attributes.
8584 */
8685
87- function argsAreEqual ( newArgs : any [ ] , lastArgs : any [ ] ) : boolean {
86+ export function argsAreEqual ( newArgs : any [ ] , lastArgs : any [ ] ) : boolean {
8887 return newArgs [ 0 ] === lastArgs [ 0 ] && // client
8988 newArgs [ 1 ] === lastArgs [ 1 ] && // lastUpdate
9089 shallowEqual ( newArgs [ 2 ] , lastArgs [ 2 ] ) && // names
9190 shallowEqual ( newArgs [ 3 ] , lastArgs [ 3 ] ) && // attributes
9291 shallowEqual ( newArgs [ 4 ] , lastArgs [ 4 ] ) && // client attributes
9392 shallowEqual ( newArgs [ 5 ] , lastArgs [ 5 ] ) ; // flagSets
9493}
95-
96- function evaluateFeatureFlagsWithConfig ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names ?: SplitIO . SplitNames , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , flagSets ?: string [ ] , options ?: SplitIO . EvaluationOptions , factory ?: SplitIO . IBrowserSDK ) {
97- return client && client . getStatus ( ) . isOperational && ( names || flagSets ) ?
98- names ?
99- client . getTreatmentsWithConfig ( names , attributes , options ) :
100- client . getTreatmentsWithConfigByFlagSets ( flagSets ! , attributes , options ) :
101- names ?
102- getControlTreatments ( names , true , factory ) :
103- { } // empty object when evaluating with flag sets and client is not ready
104- }
105-
106- export function memoizeGetTreatmentsWithConfig ( ) {
107- return memoizeOne ( evaluateFeatureFlagsWithConfig , argsAreEqual ) ;
108- }
109-
110- function evaluateFeatureFlags ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names ?: SplitIO . SplitNames , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , flagSets ?: string [ ] , options ?: SplitIO . EvaluationOptions , factory ?: SplitIO . IBrowserSDK ) {
111- return client && client . getStatus ( ) . isOperational && ( names || flagSets ) ?
112- names ?
113- client . getTreatments ( names , attributes , options ) :
114- client . getTreatmentsByFlagSets ( flagSets ! , attributes , options ) :
115- names ?
116- getControlTreatments ( names , false , factory ) :
117- { } // empty object when evaluating with flag sets and client is not ready
118- }
119-
120- export function memoizeGetTreatments ( ) {
121- return memoizeOne ( evaluateFeatureFlags , argsAreEqual ) ;
122- }
123-
124- function evaluateFeatureFlagWithConfig ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names : string [ ] , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , _flagSets ?: undefined , options ?: SplitIO . EvaluationOptions , factory ?: SplitIO . IBrowserSDK ) {
125- return client && client . getStatus ( ) . isOperational ?
126- client . getTreatmentWithConfig ( names [ 0 ] , attributes , options ) :
127- resolveFallback ( names [ 0 ] , true , factory ) ;
128- }
129-
130- export function memoizeGetTreatmentWithConfig ( ) {
131- return memoizeOne ( evaluateFeatureFlagWithConfig , argsAreEqual ) ;
132- }
133-
134- function evaluateFeatureFlag ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names : string [ ] , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , _flagSets ?: undefined , options ?: SplitIO . EvaluationOptions , factory ?: SplitIO . IBrowserSDK ) {
135- return client && client . getStatus ( ) . isOperational ?
136- client . getTreatment ( names [ 0 ] , attributes , options ) :
137- resolveFallback ( names [ 0 ] , false , factory ) ;
138- }
139-
140- export function memoizeGetTreatment ( ) {
141- return memoizeOne ( evaluateFeatureFlag , argsAreEqual ) ;
142- }
0 commit comments