-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathengine.ts
141 lines (123 loc) · 3.68 KB
/
engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { selectVariant } from "./variant";
import type { Variant } from "unleash-client/lib/variant";
import type { Context } from "unleash-client/lib/context";
import type { Constraint, Segment } from "unleash-client/lib/strategy/strategy";
import type {
ClientFeaturesResponse,
FeatureInterface,
} from "unleash-client/lib/feature";
import {
defaultStrategies,
type Strategy,
type StrategyTransportInterface,
} from "./client/strategy";
import { getDefaultVariant } from "./variant";
function processFeatures(
rawFeatures?: ClientFeaturesResponse
): Map<string, FeatureInterface> {
const processedFeatures = new Map<string, FeatureInterface>();
if (rawFeatures) {
rawFeatures.features.forEach((feature) => {
processedFeatures.set(feature.name, feature);
});
}
return processedFeatures;
}
function processSegments(
rawFeatures?: ClientFeaturesResponse
): Map<number, Segment> {
const processedSegments = new Map<number, Segment>();
if (rawFeatures && rawFeatures.segments) {
rawFeatures.segments.forEach((segment) => {
processedSegments.set(segment.id, segment);
});
}
return processedSegments;
}
export class ToggleEngine {
features: Map<string, FeatureInterface>;
strategies: Strategy[];
segments: Map<number, Segment>;
constructor(rawFeatures: ClientFeaturesResponse) {
this.features = processFeatures(rawFeatures);
this.strategies = [...defaultStrategies];
this.segments = processSegments(rawFeatures);
}
private getStrategy(name: string): Strategy | undefined {
return this.strategies.find(
(strategy: Strategy): boolean => strategy.name === name
);
}
*yieldConstraintsFor(
strategy: StrategyTransportInterface
): IterableIterator<Constraint | undefined> {
if (strategy.constraints) {
yield* strategy.constraints;
}
const segments = strategy.segments?.map((segmentId) =>
this.segments.get(segmentId)
);
if (!segments) {
return;
}
yield* this.yieldSegmentConstraints(segments);
}
yieldSegmentConstraints(segments: (Segment | undefined)[]) {
let constraints: Array<Constraint | undefined> = [];
for (const segment of segments) {
if (segment) {
constraints = constraints.concat(segment.constraints);
} else {
constraints.push(undefined);
}
}
return constraints;
}
getValue(name: string, context: Context): Variant | undefined {
let strategyVariant: Variant | undefined = undefined;
const feature = this.features.get(name);
if (!feature?.enabled) {
return undefined;
}
const hasEnabledStrategy = feature?.strategies?.some(
(strategySelector): boolean => {
const strategy = this.getStrategy(strategySelector.name);
if (!strategy) {
return false;
}
const constraints = this.yieldConstraintsFor(strategySelector);
const result = strategy.getResult(
strategySelector.parameters,
context,
constraints,
strategySelector.variants
);
if (result.enabled) {
strategyVariant = result.variant;
return true;
}
return false;
}
);
if (strategyVariant) {
return strategyVariant;
}
if (
(feature?.strategies?.length === 0 || hasEnabledStrategy) &&
feature?.variants
) {
const featureVariant = selectVariant(feature, context);
if (featureVariant) {
return {
name: featureVariant.name,
payload: featureVariant.payload,
enabled: true,
};
}
}
if (hasEnabledStrategy || !feature?.strategies?.length) {
return getDefaultVariant();
}
return undefined;
}
}