Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/evaluator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function evaluateFeature(
splitName: string,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions
): MaybeThenable<IEvaluationResult> {
let parsedSplit;

Expand All @@ -47,6 +48,7 @@ export function evaluateFeature(
split,
attributes,
storage,
options,
)).catch(
// Exception on async `getSplit` storage. For example, when the storage is redis or
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
Expand All @@ -60,6 +62,7 @@ export function evaluateFeature(
parsedSplit,
attributes,
storage,
options,
);
}

Expand All @@ -69,6 +72,7 @@ export function evaluateFeatures(
splitNames: string[],
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
let parsedSplits;

Expand All @@ -80,13 +84,13 @@ export function evaluateFeatures(
}

return thenable(parsedSplits) ?
parsedSplits.then(splits => getEvaluations(log, key, splitNames, splits, attributes, storage))
parsedSplits.then(splits => getEvaluations(log, key, splitNames, splits, attributes, storage, options))
.catch(() => {
// Exception on async `getSplits` storage. For example, when the storage is redis or
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
return treatmentsException(splitNames);
}) :
getEvaluations(log, key, splitNames, parsedSplits, attributes, storage);
getEvaluations(log, key, splitNames, parsedSplits, attributes, storage, options);
}

export function evaluateFeaturesByFlagSets(
Expand All @@ -96,6 +100,7 @@ export function evaluateFeaturesByFlagSets(
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
method: string,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
let storedFlagNames: MaybeThenable<Set<string>[]>;

Expand All @@ -111,7 +116,7 @@ export function evaluateFeaturesByFlagSets(
}

return featureFlags.size ?
evaluateFeatures(log, key, setToArray(featureFlags), attributes, storage) :
evaluateFeatures(log, key, setToArray(featureFlags), attributes, storage, options) :
{};
}

Expand All @@ -138,6 +143,7 @@ function getEvaluation(
splitJSON: ISplit | null,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<IEvaluationResult> {
let evaluation: MaybeThenable<IEvaluationResult> = {
treatment: CONTROL,
Expand All @@ -154,14 +160,14 @@ function getEvaluation(
return evaluation.then(result => {
result.changeNumber = splitJSON.changeNumber;
result.config = splitJSON.configurations && splitJSON.configurations[result.treatment] || null;
result.impressionsDisabled = splitJSON.impressionsDisabled;
result.impressionsDisabled = options?.impressionsDisabled || splitJSON.impressionsDisabled;

return result;
});
} else {
evaluation.changeNumber = splitJSON.changeNumber;
evaluation.config = splitJSON.configurations && splitJSON.configurations[evaluation.treatment] || null;
evaluation.impressionsDisabled = splitJSON.impressionsDisabled;
evaluation.impressionsDisabled = options?.impressionsDisabled || splitJSON.impressionsDisabled;
}
}

Expand All @@ -175,6 +181,7 @@ function getEvaluations(
splits: Record<string, ISplit | null>,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
const result: Record<string, IEvaluationResult> = {};
const thenables: Promise<void>[] = [];
Expand All @@ -184,7 +191,8 @@ function getEvaluations(
key,
splits[splitName],
attributes,
storage
storage,
options
);
if (thenable(evaluation)) {
thenables.push(evaluation.then(res => {
Expand Down
6 changes: 3 additions & 3 deletions src/sdkClient/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
};

const evaluation = readinessManager.isReady() || readinessManager.isReadyFromCache() ?
evaluateFeature(log, key, featureFlagName, attributes, storage) :
evaluateFeature(log, key, featureFlagName, attributes, storage, options) :
isAsync ? // If the SDK is not ready, treatment may be incorrect due to having splits but not segments data, or storage is not connected
Promise.resolve(treatmentNotReady) :
treatmentNotReady;
Expand Down Expand Up @@ -81,7 +81,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
};

const evaluations = readinessManager.isReady() || readinessManager.isReadyFromCache() ?
evaluateFeatures(log, key, featureFlagNames, attributes, storage) :
evaluateFeatures(log, key, featureFlagNames, attributes, storage, options) :
isAsync ? // If the SDK is not ready, treatment may be incorrect due to having splits but not segments data, or storage is not connected
Promise.resolve(treatmentsNotReady(featureFlagNames)) :
treatmentsNotReady(featureFlagNames);
Expand Down Expand Up @@ -110,7 +110,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl
};

const evaluations = readinessManager.isReady() || readinessManager.isReadyFromCache() ?
evaluateFeaturesByFlagSets(log, key, flagSetNames, attributes, storage, methodName) :
evaluateFeaturesByFlagSets(log, key, flagSetNames, attributes, storage, methodName, options) :
isAsync ?
Promise.resolve({}) :
{};
Expand Down
8 changes: 8 additions & 0 deletions types/splitio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,16 @@ declare namespace SplitIO {
* Evaluation options object for getTreatment methods.
*/
type EvaluationOptions = {
/**
* Whether the evaluation/s will track impressions or not.
*
* @defaultValue `false`
*/
impressionsDisabled?: boolean;
/**
* Optional properties to append to the generated impression object sent to Split backend.
*
* @defaultValue `undefined`
*/
properties?: Properties;
}
Expand Down
Loading