-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: strategy variants #482
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ea5c3a
feat: strategy variants
kwasniew d5b605b
fix: linter
kwasniew 152f33d
fix: handle empty variants
kwasniew 8dc2ccc
feat: multi variants
kwasniew f721d1c
feat: make overrides optional
kwasniew 62bc557
test: specification bump
kwasniew 2c79ba1
chore: lint
kwasniew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ import { EventEmitter } from 'events'; | |
import { Strategy, StrategyTransportInterface } from './strategy'; | ||
import { FeatureInterface } from './feature'; | ||
import { RepositoryInterface } from './repository'; | ||
import { Variant, getDefaultVariant, VariantDefinition, selectVariant } from './variant'; | ||
import { | ||
Variant, VariantDefinition, | ||
getDefaultVariant, selectVariant, selectVariantDefinition, | ||
} from './variant'; | ||
import { Context } from './context'; | ||
import { Constraint, Segment } from './strategy/strategy'; | ||
import { createImpressionEvent, UnleashEvents } from './events'; | ||
|
@@ -55,7 +58,7 @@ export default class UnleashClient extends EventEmitter { | |
|
||
isEnabled(name: string, context: Context, fallback: Function): boolean { | ||
const feature = this.repository.getToggle(name); | ||
const enabled = this.isFeatureEnabled(feature, context, fallback); | ||
const [enabled] = this.isFeatureEnabled(feature, context, fallback); | ||
if (feature?.impressionData) { | ||
this.emit( | ||
UnleashEvents.Impression, | ||
|
@@ -74,40 +77,50 @@ export default class UnleashClient extends EventEmitter { | |
feature: FeatureInterface | undefined, | ||
context: Context, | ||
fallback: Function, | ||
): boolean { | ||
): [boolean, VariantDefinition | undefined] { | ||
if (!feature) { | ||
return fallback(); | ||
return [fallback(), undefined]; | ||
} | ||
|
||
if (!feature || !feature.enabled) { | ||
return false; | ||
return [false, undefined]; | ||
} | ||
|
||
if (!Array.isArray(feature.strategies)) { | ||
const msg = `Malformed feature, strategies not an array, is a ${typeof feature.strategies}`; | ||
this.emit(UnleashEvents.Error, new Error(msg)); | ||
return false; | ||
return [false, undefined]; | ||
} | ||
|
||
if (feature.strategies.length === 0) { | ||
return feature.enabled; | ||
return [feature.enabled, undefined]; | ||
} | ||
|
||
return ( | ||
let featureVariant = undefined; | ||
|
||
return [( | ||
feature.strategies.length > 0 && | ||
|
||
feature.strategies.some((strategySelector): boolean => { | ||
const strategy = this.getStrategy(strategySelector.name); | ||
if (!strategy) { | ||
this.warnOnce(strategySelector.name, feature.name, feature.strategies); | ||
return false; | ||
} | ||
const constraints = this.yieldConstraintsFor(strategySelector); | ||
return strategy.isEnabledWithConstraints(strategySelector.parameters, context, constraints); | ||
const enabled = | ||
strategy.isEnabledWithConstraints(strategySelector.parameters, context, constraints); | ||
const variantParam = strategySelector?.variants; | ||
|
||
if (enabled && Array.isArray(variantParam) && variantParam.length > 0) { | ||
featureVariant = selectVariantDefinition(feature.name, variantParam, context); | ||
} | ||
return enabled; | ||
}) | ||
); | ||
), featureVariant]; | ||
} | ||
|
||
*yieldConstraintsFor( | ||
* yieldConstraintsFor( | ||
strategy: StrategyTransportInterface, | ||
): IterableIterator<Constraint | undefined> { | ||
if (strategy.constraints) { | ||
|
@@ -120,7 +133,7 @@ export default class UnleashClient extends EventEmitter { | |
yield* this.yieldSegmentConstraints(segments); | ||
} | ||
|
||
*yieldSegmentConstraints( | ||
* yieldSegmentConstraints( | ||
segments: (Segment | undefined)[], | ||
): IterableIterator<Constraint | undefined> { | ||
// eslint-disable-next-line no-restricted-syntax | ||
|
@@ -169,23 +182,32 @@ export default class UnleashClient extends EventEmitter { | |
fallbackVariant?: Variant, | ||
): Variant { | ||
const fallback = fallbackVariant || getDefaultVariant(); | ||
if ( | ||
typeof feature === 'undefined' || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is too early since variants can be on the strategy |
||
!feature.variants || | ||
!Array.isArray(feature.variants) || | ||
feature.variants.length === 0 | ||
) { | ||
|
||
if (typeof feature === 'undefined') { | ||
return fallback; | ||
} | ||
|
||
let enabled = true; | ||
let strategyVariant = undefined; | ||
if (checkToggle) { | ||
enabled = this.isFeatureEnabled(feature, context, () => | ||
[enabled, strategyVariant] = this.isFeatureEnabled(feature, context, () => | ||
fallbackVariant ? fallbackVariant.enabled : false, | ||
); | ||
if (!enabled) { | ||
return fallback; | ||
} | ||
} | ||
|
||
if (strategyVariant) { | ||
return { | ||
name: strategyVariant.name, | ||
payload: strategyVariant.payload, | ||
enabled: !checkToggle || enabled, | ||
}; | ||
} | ||
|
||
if (!enabled || | ||
!feature.variants || | ||
!Array.isArray(feature.variants) || | ||
feature.variants.length === 0) { | ||
return fallback; | ||
} | ||
|
||
const variant: VariantDefinition | null = selectVariant(feature, context); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in addition to boolean true/false was want to know the actual variant that the strategy resolved to if it was provided.