From 9f35c2187f5691d547b105c881c5cda003477607 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Wed, 26 Jul 2023 12:23:39 +0200 Subject: [PATCH] fix: sync enabled and variant status (#4345) --- .../playground/feature-evaluator/client.ts | 40 ++++++++++++------- .../feature-evaluator/feature-evaluator.ts | 5 +++ .../features/playground/playground-service.ts | 6 ++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/lib/features/playground/feature-evaluator/client.ts b/src/lib/features/playground/feature-evaluator/client.ts index 6a9d7bcc50..0a621f3614 100644 --- a/src/lib/features/playground/feature-evaluator/client.ts +++ b/src/lib/features/playground/feature-evaluator/client.ts @@ -199,7 +199,7 @@ export default class UnleashClient { context: Context, fallbackVariant?: Variant, ): Variant { - return this.resolveVariant(name, context, true, fallbackVariant); + return this.resolveVariant(name, context, fallbackVariant); } // This function is intended to close an issue in the proxy where feature enabled @@ -208,16 +208,28 @@ export default class UnleashClient { forceGetVariant( name: string, context: Context, + forcedResult: Pick< + FeatureStrategiesEvaluationResult, + 'result' | 'variant' + >, fallbackVariant?: Variant, ): Variant { - return this.resolveVariant(name, context, false, fallbackVariant); + return this.resolveVariant( + name, + context, + fallbackVariant, + forcedResult, + ); } private resolveVariant( name: string, context: Context, - checkToggle: boolean, fallbackVariant?: Variant, + forcedResult?: Pick< + FeatureStrategiesEvaluationResult, + 'result' | 'variant' + >, ): Variant { const fallback = fallbackVariant || getDefaultVariant(); const feature = this.repository.getToggle(name); @@ -227,18 +239,18 @@ export default class UnleashClient { } let enabled = true; - if (checkToggle) { - const result = this.isFeatureEnabled(feature, context, () => + const result = + forcedResult ?? + this.isFeatureEnabled(feature, context, () => fallbackVariant ? fallbackVariant.enabled : false, ); - enabled = result.result === true; - const strategyVariant = result.variant; - if (enabled && strategyVariant) { - return strategyVariant; - } - if (!enabled) { - return fallback; - } + enabled = result.result === true; + const strategyVariant = result.variant; + if (enabled && strategyVariant) { + return strategyVariant; + } + if (!enabled) { + return fallback; } if ( @@ -261,7 +273,7 @@ export default class UnleashClient { return { name: variant.name, payload: variant.payload, - enabled: !checkToggle || enabled, + enabled, }; } } diff --git a/src/lib/features/playground/feature-evaluator/feature-evaluator.ts b/src/lib/features/playground/feature-evaluator/feature-evaluator.ts index 0299e03181..c56340906c 100644 --- a/src/lib/features/playground/feature-evaluator/feature-evaluator.ts +++ b/src/lib/features/playground/feature-evaluator/feature-evaluator.ts @@ -104,6 +104,10 @@ export class FeatureEvaluator { forceGetVariant( name: string, + forcedResults: Pick< + FeatureStrategiesEvaluationResult, + 'result' | 'variant' + >, context: Context = {}, fallbackVariant?: Variant, ): Variant { @@ -111,6 +115,7 @@ export class FeatureEvaluator { return this.client.forceGetVariant( name, enhancedContext, + forcedResults, fallbackVariant, ); } diff --git a/src/lib/features/playground/playground-service.ts b/src/lib/features/playground/playground-service.ts index fcef309da1..ef4495c1c0 100644 --- a/src/lib/features/playground/playground-service.ts +++ b/src/lib/features/playground/playground-service.ts @@ -176,7 +176,11 @@ export class PlaygroundService { data: strategyEvaluationResult.strategies, }, projectId: featureProject[feature.name], - variant: client.getVariant(feature.name, clientContext), + variant: client.forceGetVariant( + feature.name, + strategyEvaluationResult, + clientContext, + ), name: feature.name, environment, context,