@@ -37,6 +37,24 @@ export function getStatus(client?: SplitIO.IBrowserClient): ISplitStatus {
3737 } ;
3838}
3939
40+ function resolveFallback ( flagName : string , withConfig : true , factory ?: SplitIO . IBrowserSDK ) : SplitIO . TreatmentWithConfig ;
41+ 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 {
43+ if ( factory && factory . settings . fallbackTreatments ) {
44+ const fallbacks = factory . settings . fallbackTreatments ;
45+
46+ const treatment = fallbacks . byFlag ?. [ flagName ] || fallbacks . global ;
47+
48+ if ( treatment ) {
49+ return isString ( treatment ) ?
50+ withConfig ? { treatment, config : null } : treatment :
51+ withConfig ? treatment : treatment . treatment ;
52+ }
53+ }
54+
55+ return withConfig ? CONTROL_WITH_CONFIG : CONTROL ;
56+ }
57+
4058/**
4159 * Manage client attributes binding
4260 */
@@ -45,9 +63,9 @@ export function initAttributes(client?: SplitIO.IBrowserClient, attributes?: Spl
4563 if ( client && attributes ) client . setAttributes ( attributes ) ;
4664}
4765
48- export function getControlTreatments ( featureFlagNames : unknown , withConfig : true ) : SplitIO . TreatmentsWithConfig ;
49- export function getControlTreatments ( featureFlagNames : unknown , withConfig : false ) : SplitIO . Treatments ;
50- export function getControlTreatments ( featureFlagNames : unknown , withConfig : boolean ) : SplitIO . Treatments | SplitIO . TreatmentsWithConfig {
66+ export function getControlTreatments ( featureFlagNames : unknown , withConfig : true , factory ?: SplitIO . IBrowserSDK ) : SplitIO . TreatmentsWithConfig ;
67+ 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 {
5169 // validate feature flag names
5270 if ( ! Array . isArray ( featureFlagNames ) ) return { } ;
5371
@@ -56,9 +74,10 @@ export function getControlTreatments(featureFlagNames: unknown, withConfig: bool
5674 . map ( ( featureFlagName ) => featureFlagName . trim ( ) )
5775 . filter ( ( featureFlagName ) => featureFlagName . length > 0 ) ;
5876
59- // return control treatments for each validated feature flag name
60- return ( featureFlagNames as string [ ] ) . reduce ( ( pValue : SplitIO . Treatments | SplitIO . TreatmentsWithConfig , cValue : string ) => {
61- pValue [ cValue ] = withConfig ? CONTROL_WITH_CONFIG : CONTROL ;
77+ // return control or fallback treatment for each validated feature flag name
78+ return ( featureFlagNames as string [ ] ) . reduce ( ( pValue : SplitIO . Treatments | SplitIO . TreatmentsWithConfig , featureFlagName : string ) => {
79+ // @ts -expect-error asd
80+ pValue [ featureFlagName ] = resolveFallback ( featureFlagName , withConfig , factory ) ;
6281 return pValue ;
6382 } , { } ) ;
6483}
@@ -79,13 +98,13 @@ function argsAreEqual(newArgs: any[], lastArgs: any[]): boolean {
7998 shallowEqual ( newArgs [ 5 ] , lastArgs [ 5 ] ) ; // flagSets
8099}
81100
82- function evaluateFeatureFlagsWithConfig ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names ?: SplitIO . SplitNames , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , flagSets ?: string [ ] , options ?: SplitIO . EvaluationOptions ) {
101+ 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 ) {
83102 return client && client . getStatus ( ) . isOperational && ( names || flagSets ) ?
84103 names ?
85104 client . getTreatmentsWithConfig ( names , attributes , options ) :
86105 client . getTreatmentsWithConfigByFlagSets ( flagSets ! , attributes , options ) :
87106 names ?
88- getControlTreatments ( names , true ) :
107+ getControlTreatments ( names , true , factory ) :
89108 { } // empty object when evaluating with flag sets and client is not ready
90109}
91110
@@ -97,34 +116,34 @@ export function memoizeGetTreatmentsWithConfig() {
97116 return memoizeOne ( evaluateFeatureFlagsWithConfig , argsAreEqual ) ;
98117}
99118
100- function evaluateFeatureFlags ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names ?: SplitIO . SplitNames , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , flagSets ?: string [ ] , options ?: SplitIO . EvaluationOptions ) {
119+ 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 ) {
101120 return client && client . getStatus ( ) . isOperational && ( names || flagSets ) ?
102121 names ?
103122 client . getTreatments ( names , attributes , options ) :
104123 client . getTreatmentsByFlagSets ( flagSets ! , attributes , options ) :
105124 names ?
106- getControlTreatments ( names , false ) :
125+ getControlTreatments ( names , false , factory ) :
107126 { } // empty object when evaluating with flag sets and client is not ready
108127}
109128
110129export function memoizeGetTreatments ( ) {
111130 return memoizeOne ( evaluateFeatureFlags , argsAreEqual ) ;
112131}
113132
114- function evaluateFeatureFlagWithConfig ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names : string [ ] , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , _flagSets ?: undefined , options ?: SplitIO . EvaluationOptions ) {
133+ function evaluateFeatureFlagWithConfig ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names : string [ ] , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , _flagSets ?: undefined , options ?: SplitIO . EvaluationOptions , factory ?: SplitIO . IBrowserSDK ) {
115134 return client && client . getStatus ( ) . isOperational ?
116135 client . getTreatmentWithConfig ( names [ 0 ] , attributes , options ) :
117- CONTROL_WITH_CONFIG
136+ resolveFallback ( names [ 0 ] , true , factory ) ;
118137}
119138
120139export function memoizeGetTreatmentWithConfig ( ) {
121140 return memoizeOne ( evaluateFeatureFlagWithConfig , argsAreEqual ) ;
122141}
123142
124- function evaluateFeatureFlag ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names : string [ ] , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , _flagSets ?: undefined , options ?: SplitIO . EvaluationOptions ) {
143+ function evaluateFeatureFlag ( client : SplitIO . IBrowserClient | undefined , _lastUpdate : number , names : string [ ] , attributes ?: SplitIO . Attributes , _clientAttributes ?: SplitIO . Attributes , _flagSets ?: undefined , options ?: SplitIO . EvaluationOptions , factory ?: SplitIO . IBrowserSDK ) {
125144 return client && client . getStatus ( ) . isOperational ?
126145 client . getTreatment ( names [ 0 ] , attributes , options ) :
127- CONTROL ;
146+ resolveFallback ( names [ 0 ] , false , factory ) ;
128147}
129148
130149export function memoizeGetTreatment ( ) {
0 commit comments