From 66a9740ddabfd15fe309b535d5ab9f8e0c0ee98b Mon Sep 17 00:00:00 2001 From: Emiliano Sanchez Date: Wed, 6 Dec 2023 02:03:44 -0300 Subject: [PATCH] Rename 'isStorageSync' to 'isConsumerMode' function for reuse. Rename 'mode' to 'validateMode' function for consistency with other validation utilities --- src/sdkClient/client.ts | 16 ++++++++-------- src/sdkClient/clientInputValidation.ts | 10 +++++----- src/sdkManager/index.ts | 12 ++++++------ src/trackers/eventTracker.ts | 8 ++++---- src/trackers/impressionObserver/utils.ts | 9 --------- src/utils/settingsValidation/index.ts | 4 ++-- src/utils/settingsValidation/mode.ts | 9 ++++++++- src/utils/settingsValidation/splitFilters.ts | 4 ++-- 8 files changed, 35 insertions(+), 37 deletions(-) delete mode 100644 src/trackers/impressionObserver/utils.ts diff --git a/src/sdkClient/client.ts b/src/sdkClient/client.ts index 8da54dd3..cea1a265 100644 --- a/src/sdkClient/client.ts +++ b/src/sdkClient/client.ts @@ -9,7 +9,7 @@ import { IEvaluationResult } from '../evaluator/types'; import { SplitIO, ImpressionDTO } from '../types'; import { IMPRESSION, IMPRESSION_QUEUEING } from '../logger/constants'; import { ISdkFactoryContext } from '../sdkFactory/types'; -import { isStorageSync } from '../trackers/impressionObserver/utils'; +import { isConsumerMode } from '../utils/settingsValidation/mode'; import { Method } from '../sync/submitters/types'; const treatmentNotReady = { treatment: CONTROL, label: SDK_NOT_READY }; @@ -43,9 +43,9 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl const evaluation = readinessManager.isReady() || readinessManager.isReadyFromCache() ? evaluateFeature(log, key, featureFlagName, attributes, storage) : - isStorageSync(settings) ? // If the SDK is not ready, treatment may be incorrect due to having splits but not segments data, or storage is not connected - treatmentNotReady : - Promise.resolve(treatmentNotReady); // Promisify if async + isConsumerMode(mode) ? // 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) : // Promisify if async + treatmentNotReady; return thenable(evaluation) ? evaluation.then((res) => wrapUp(res)) : wrapUp(evaluation); } @@ -71,9 +71,9 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl const evaluations = readinessManager.isReady() || readinessManager.isReadyFromCache() ? evaluateFeatures(log, key, featureFlagNames, attributes, storage) : - isStorageSync(settings) ? // If the SDK is not ready, treatment may be incorrect due to having splits but not segments data, or storage is not connected - treatmentsNotReady(featureFlagNames) : - Promise.resolve(treatmentsNotReady(featureFlagNames)); // Promisify if async + isConsumerMode(mode) ? // 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)) : // Promisify if async + treatmentsNotReady(featureFlagNames); return thenable(evaluations) ? evaluations.then((res) => wrapUp(res)) : wrapUp(evaluations); } @@ -100,7 +100,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl const evaluations = readinessManager.isReady() || readinessManager.isReadyFromCache() ? evaluateFeaturesByFlagSets(log, key, flagSetNames, attributes, storage, methodName) : - isStorageSync(settings) ? {} : Promise.resolve({}); // Promisify if async + isConsumerMode(mode) ? Promise.resolve({}) : {}; // Promisify if async return thenable(evaluations) ? evaluations.then((res) => wrapUp(res)) : wrapUp(evaluations); } diff --git a/src/sdkClient/clientInputValidation.ts b/src/sdkClient/clientInputValidation.ts index c5b49b49..1fc614eb 100644 --- a/src/sdkClient/clientInputValidation.ts +++ b/src/sdkClient/clientInputValidation.ts @@ -16,7 +16,7 @@ import { CONTROL, CONTROL_WITH_CONFIG, GET_TREATMENT, GET_TREATMENTS, GET_TREATM import { IReadinessManager } from '../readiness/types'; import { MaybeThenable } from '../dtos/types'; import { ISettings, SplitIO } from '../types'; -import { isStorageSync } from '../trackers/impressionObserver/utils'; +import { isConsumerMode } from '../utils/settingsValidation/mode'; import { validateFlagSets } from '../utils/settingsValidation/splitFilters'; /** @@ -25,8 +25,8 @@ import { validateFlagSets } from '../utils/settingsValidation/splitFilters'; */ export function clientInputValidationDecorator(settings: ISettings, client: TClient, readinessManager: IReadinessManager): TClient { - const log = settings.log; - const isSync = isStorageSync(settings); + const { log, mode } = settings; + const isAsync = isConsumerMode(mode); /** * Avoid repeating this validations code @@ -59,7 +59,7 @@ export function clientInputValidationDecorator(value: T): MaybeThenable { - return isSync ? value : Promise.resolve(value); + return isAsync ? Promise.resolve(value) : value; } function getTreatment(maybeKey: SplitIO.SplitKey, maybeFeatureFlagName: string, maybeAttributes?: SplitIO.Attributes) { @@ -159,7 +159,7 @@ export function clientInputValidationDecorator) { - return [CONSUMER_MODE, CONSUMER_PARTIAL_MODE].indexOf(settings.mode) === -1 ? true : false; -} diff --git a/src/utils/settingsValidation/index.ts b/src/utils/settingsValidation/index.ts index 700ada43..3e23b927 100644 --- a/src/utils/settingsValidation/index.ts +++ b/src/utils/settingsValidation/index.ts @@ -1,5 +1,5 @@ import { merge, get } from '../lang'; -import { mode } from './mode'; +import { validateMode } from './mode'; import { validateSplitFilters } from './splitFilters'; import { STANDALONE_MODE, OPTIMIZED, LOCALHOST_MODE, DEBUG } from '../constants'; import { validImpressionsMode } from './impressionsMode'; @@ -146,7 +146,7 @@ export function settingsValidation(config: unknown, validationParams: ISettingsV // ensure a valid SDK mode // @ts-ignore, modify readonly prop - withDefaults.mode = mode(withDefaults.core.authorizationKey, withDefaults.mode); + withDefaults.mode = validateMode(withDefaults.core.authorizationKey, withDefaults.mode); // ensure a valid Storage based on mode defined. // @ts-ignore, modify readonly prop diff --git a/src/utils/settingsValidation/mode.ts b/src/utils/settingsValidation/mode.ts index 8480a24b..bb4d7ded 100644 --- a/src/utils/settingsValidation/mode.ts +++ b/src/utils/settingsValidation/mode.ts @@ -1,6 +1,6 @@ import { LOCALHOST_MODE, STANDALONE_MODE, PRODUCER_MODE, CONSUMER_MODE, CONSUMER_PARTIAL_MODE } from '../constants'; -export function mode(key: string, mode: string) { +export function validateMode(key: string, mode: string) { // Leaving the comparison as is, in case we change the mode name but not the setting. if (key === 'localhost') return LOCALHOST_MODE; @@ -8,3 +8,10 @@ export function mode(key: string, mode: string) { return mode; } + +/** + * Storage is async if mode is consumer or partial consumer + */ +export function isConsumerMode(mode: string) { + return CONSUMER_MODE === mode || CONSUMER_PARTIAL_MODE === mode; +} diff --git a/src/utils/settingsValidation/splitFilters.ts b/src/utils/settingsValidation/splitFilters.ts index ebe76610..8f4c10b6 100644 --- a/src/utils/settingsValidation/splitFilters.ts +++ b/src/utils/settingsValidation/splitFilters.ts @@ -1,4 +1,3 @@ -import { CONSUMER_MODE, CONSUMER_PARTIAL_MODE } from '../constants'; import { validateSplits } from '../inputValidation/splits'; import { ISplitFiltersValidation } from '../../dtos/types'; import { SplitIO } from '../../types'; @@ -6,6 +5,7 @@ import { ILogger } from '../../logger/types'; import { WARN_SPLITS_FILTER_IGNORED, WARN_SPLITS_FILTER_EMPTY, WARN_SPLITS_FILTER_INVALID, SETTINGS_SPLITS_FILTER, LOG_PREFIX_SETTINGS, ERROR_SETS_FILTER_EXCLUSIVE, WARN_LOWERCASE_FLAGSET, WARN_INVALID_FLAGSET, WARN_FLAGSET_NOT_CONFIGURED } from '../../logger/constants'; import { objectAssign } from '../lang/objectAssign'; import { find, uniq } from '../lang'; +import { isConsumerMode } from './mode'; // Split filters metadata. // Ordered according to their precedency when forming the filter query string: `&names=&prefixes=` @@ -149,7 +149,7 @@ export function validateSplitFilters(log: ILogger, maybeSplitFilters: any, mode: // do nothing if `splitFilters` param is not a non-empty array or mode is not STANDALONE if (!maybeSplitFilters) return res; // Warn depending on the mode - if (mode === CONSUMER_MODE || mode === CONSUMER_PARTIAL_MODE) { + if (isConsumerMode(mode)) { log.warn(WARN_SPLITS_FILTER_IGNORED); return res; }