Skip to content

Commit

Permalink
Check that initSplitSdk action was dispatched before using new selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed May 22, 2024
1 parent 69bdc3d commit e4601de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export const ERROR_TRACK_NO_INITSPLITSDK = '[ERROR] To use "track" the SDK must

export const ERROR_MANAGER_NO_INITSPLITSDK = '[ERROR] To use the manager, the SDK must be first initialized with an "initSplitSdk" action';

export const ERROR_SELECTOR_NO_SPLITSTATE = '[ERROR] When using selectors, "splitState" value must be a proper splitio piece of state';
export const ERROR_SELECTOR_NO_INITSPLITSDK = '[ERROR] To use selectors, the SDK must be first initialized with an "initSplitSdk" action';

export const ERROR_SELECTOR_NO_SPLITSTATE = '[ERROR] To use selectors, "splitState" param must be a proper splitio piece of state';

export const ERROR_GETT_NO_PARAM_OBJECT = '[ERROR] "getTreatments" must be called with a param object containing a valid splitNames or flagSets properties';

Expand Down
30 changes: 20 additions & 10 deletions src/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ISplitState, ISplitStatus } from './types';
import { CONTROL, CONTROL_WITH_CONFIG, DEFAULT_SPLIT_STATE_SLICE, ERROR_SELECTOR_NO_SPLITSTATE } from './constants';
import { CONTROL, CONTROL_WITH_CONFIG, DEFAULT_SPLIT_STATE_SLICE, ERROR_SELECTOR_NO_INITSPLITSDK, ERROR_SELECTOR_NO_SPLITSTATE } from './constants';
import { getClient } from './asyncActions';
import { splitSdk } from './asyncActions';
import { getStatus, matching } from './utils';
Expand Down Expand Up @@ -32,15 +32,25 @@ export function selectTreatmentValue(splitState: ISplitState, featureFlagName: s
* @param {TreatmentWithConfig} defaultValue
*/
export function selectTreatmentWithConfig(splitState: ISplitState, featureFlagName: string, key?: SplitIO.SplitKey, defaultValue: SplitIO.TreatmentWithConfig = CONTROL_WITH_CONFIG): SplitIO.TreatmentWithConfig {
const splitTreatments = splitState && splitState.treatments ? splitState.treatments[featureFlagName] : console.error(ERROR_SELECTOR_NO_SPLITSTATE);
const treatment =
splitTreatments ?
key ?
splitTreatments[matching(key)] :
Object.values(splitTreatments)[0] :
undefined;
if (!splitState || !splitState.treatments) {
console.log(ERROR_SELECTOR_NO_SPLITSTATE);
return defaultValue;
}

return treatment ? treatment : defaultValue;
const splitTreatments = splitState.treatments[featureFlagName];

const treatment = splitTreatments ?
key ?
splitTreatments[matching(key)] :
Object.values(splitTreatments)[0] :
undefined;

if (!treatment) {
console.log(`[ERROR] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "${featureFlagName}" ${key ? `and key "${matching(key)}"` : ''}`);
return defaultValue;
}

return treatment;
}

/**
Expand Down Expand Up @@ -76,7 +86,7 @@ export function selectSplitTreatmentWithConfig(splitState: ISplitState, featureF
} & ISplitStatus {
const treatment = selectTreatmentWithConfig(splitState, featureFlagName, key, defaultValue);

const client = getClient(splitSdk, key, true);
const client = splitSdk.factory ? getClient(splitSdk, key, true) : console.log(ERROR_SELECTOR_NO_INITSPLITSDK);

const status = client ?
getStatus(client) :
Expand Down

0 comments on commit e4601de

Please sign in to comment.