Skip to content

Commit fc89992

Browse files
Refactor evaluation handling
1 parent d4e5cd5 commit fc89992

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

src/evaluator/index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ import { ILogger } from '../logger/types';
1010
import { returnSetsUnion, setToArray } from '../utils/lang/sets';
1111
import { WARN_FLAGSET_WITHOUT_FLAGS } from '../logger/constants';
1212

13-
const treatmentException = {
13+
const EVALUATION_EXCEPTION = {
1414
treatment: CONTROL,
1515
label: EXCEPTION,
1616
config: null
1717
};
1818

19+
let EVALUATION_NOT_FOUND = {
20+
treatment: CONTROL,
21+
label: SPLIT_NOT_FOUND,
22+
config: null
23+
};
24+
1925
function treatmentsException(splitNames: string[]) {
2026
const evaluations: Record<string, IEvaluationResult> = {};
2127
splitNames.forEach(splitName => {
22-
evaluations[splitName] = treatmentException;
28+
evaluations[splitName] = EVALUATION_EXCEPTION;
2329
});
2430
return evaluations;
2531
}
@@ -38,7 +44,7 @@ export function evaluateFeature(
3844
parsedSplit = storage.splits.getSplit(splitName);
3945
} catch (e) {
4046
// Exception on sync `getSplit` storage. Not possible ATM with InMemory and InLocal storages.
41-
return treatmentException;
47+
return EVALUATION_EXCEPTION;
4248
}
4349

4450
if (thenable(parsedSplit)) {
@@ -52,7 +58,7 @@ export function evaluateFeature(
5258
)).catch(
5359
// Exception on async `getSplit` storage. For example, when the storage is redis or
5460
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
55-
() => treatmentException
61+
() => EVALUATION_EXCEPTION
5662
);
5763
}
5864

@@ -145,15 +151,11 @@ function getEvaluation(
145151
storage: IStorageSync | IStorageAsync,
146152
options?: SplitIO.EvaluationOptions,
147153
): MaybeThenable<IEvaluationResult> {
148-
let evaluation: MaybeThenable<IEvaluationResult> = {
149-
treatment: CONTROL,
150-
label: SPLIT_NOT_FOUND,
151-
config: null
152-
};
154+
153155

154156
if (splitJSON) {
155157
const split = engineParser(log, splitJSON, storage);
156-
evaluation = split.getTreatment(key, attributes, evaluateFeature);
158+
const evaluation = split.getTreatment(key, attributes, evaluateFeature);
157159

158160
// If the storage is async and the evaluated flag uses segments or dependencies, evaluation is thenable
159161
if (thenable(evaluation)) {
@@ -171,9 +173,11 @@ function getEvaluation(
171173
// @ts-expect-error impressionsDisabled is not exposed in the public typings yet.
172174
evaluation.impressionsDisabled = options?.impressionsDisabled || splitJSON.impressionsDisabled;
173175
}
176+
177+
return evaluation;
174178
}
175179

176-
return evaluation;
180+
return EVALUATION_NOT_FOUND;
177181
}
178182

179183
function getEvaluations(
@@ -217,12 +221,11 @@ export function evaluateDefaultTreatment(
217221
try {
218222
parsedSplit = storage.splits.getSplit(splitName);
219223
} catch (e) {
220-
// Exception on sync `getSplit` storage. Not possible ATM with InMemory and InLocal storages.
221-
return treatmentException;
224+
return EVALUATION_EXCEPTION;
222225
}
223226

224227
return thenable(parsedSplit) ?
225-
parsedSplit.then(getDefaultTreatment).catch(() => treatmentException) :
228+
parsedSplit.then(getDefaultTreatment).catch(() => EVALUATION_EXCEPTION) :
226229
getDefaultTreatment(parsedSplit);
227230
}
228231

@@ -238,9 +241,5 @@ function getDefaultTreatment(
238241
};
239242
}
240243

241-
return {
242-
treatment: CONTROL,
243-
label: SPLIT_NOT_FOUND,
244-
config: null
245-
};
244+
return EVALUATION_NOT_FOUND;
246245
}

0 commit comments

Comments
 (0)