Skip to content

Commit

Permalink
README.md update and code comments polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed May 21, 2024
1 parent 40d6fe0 commit 5a41433
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React from 'react';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { splitReducer, initSplitSdk, getTreatments,
selectTreatmentValue, connectSplit } from '@splitsoftware/splitio-redux'
selectSplitTreatment, connectSplit } from '@splitsoftware/splitio-redux'

// Init Redux store
const store = createStore(
Expand All @@ -47,12 +47,12 @@ store.dispatch(getTreatments({ splitNames: 'FEATURE_FLAG_NAME' }))

// Connect your component to splitio's piece of state
const MyComponent = connectSplit()(({ splitio }) => {
// Check SDK readiness using isReady property
if (!splitio.isReady)
return <div>Loading SDK ...</div>;

// Select a treatment value
const treatment = selectTreatmentValue(splitio, 'FEATURE_FLAG_NAME')
const { treatment, isReady } = selectSplitTreatment(splitio, 'FEATURE_FLAG_NAME')

// Check SDK client readiness using isReady property
if (!isReady) return <div>Loading SDK ...</div>;

if (treatment === 'on') {
// return JSX for 'on' treatment
} else if (treatment === 'off') {
Expand Down
53 changes: 21 additions & 32 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@ export const getStateSlice = (sliceName: string) => (state: any) => state[sliceN
export const defaultGetSplitState = getStateSlice(DEFAULT_SPLIT_STATE_SLICE);

/**
* Selector function to extract a treatment evaluation from the Split state. It returns the treatment string value.
* This function extracts a treatment evaluation from the Split state. It returns the treatment string value.
* If a treatment is not found, for example, due to passing an invalid Split state or a nonexistent feature flag name or key, it returns the default value, which is `'control'` if not provided.
*
* @param {ISplitState} splitState
* @param {string} featureFlagName
* @param {SplitIO.SplitKey} key
* @param {string} defaultValue
*
* @deprecated Use selectSplitTreatment instead
*/
export function selectTreatmentValue(splitState: ISplitState, featureFlagName: string, key?: SplitIO.SplitKey, defaultValue: string = CONTROL): string {
return selectTreatmentWithConfig(splitState, featureFlagName, key, { treatment: defaultValue, config: null }).treatment;
}

/**
* Selector function to extract a treatment evaluation from the Split state. It returns a treatment object containing its value and configuration.
* This function extracts a treatment evaluation from the Split state. It returns a treatment object containing its value and configuration.
* If a treatment is not found, for example, due to passing an invalid Split state or a nonexistent feature flag name or key, it returns the default value, which is `{ treatment: 'control', configuration: null }` if not provided.
*
* @param {ISplitState} splitState
* @param {string} featureFlagName
* @param {SplitIO.SplitKey} key
* @param {TreatmentWithConfig} defaultValue
*
* @deprecated Use selectSplitTreatmentWithConfig instead
*/
export function selectTreatmentWithConfig(splitState: ISplitState, featureFlagName: string, key?: SplitIO.SplitKey, defaultValue: SplitIO.TreatmentWithConfig = CONTROL_WITH_CONFIG): SplitIO.TreatmentWithConfig {
// @TODO reuse `selectSplitTreatmentWithConfig`
const splitTreatments = splitState && splitState.treatments ? splitState.treatments[featureFlagName] : console.error(ERROR_SELECTOR_NO_SPLITSTATE);
const treatment =
splitTreatments ?
Expand All @@ -46,7 +42,8 @@ export function selectTreatmentWithConfig(splitState: ISplitState, featureFlagNa
}

/**
* Selector function to extract a treatment evaluation from the Split state. It returns the treatment string value.
* This function extracts a treatment evaluation from the Split state. It returns an object that contains the treatment string value and the status properties of the client: `isReady`, `isReadyFromCache`, `hasTimedout`, `isDestroyed`.
* If a treatment is not found, for example, due to passing an invalid Split state or a nonexistent feature flag name or key, it returns the default value, which is `'control'` if not provided.
*
* @param {ISplitState} splitState
* @param {string} featureFlagName
Expand All @@ -62,7 +59,8 @@ export function selectSplitTreatment(splitState: ISplitState, featureFlagName: s
}

/**
* Selector function to extract a treatment evaluation from the Split state. It returns a treatment object containing its value and configuration.
* This function extracts a treatment evaluation from the Split state. It returns an object that contains the treatment object and the status properties of the client: `isReady`, `isReadyFromCache`, `hasTimedout`, `isDestroyed`.
* If a treatment is not found, for example, due to passing an invalid Split state or a nonexistent feature flag name or key, it returns the default value as treatment, which is `{ treatment: 'control', configuration: null }` if not provided.
*
* @param {ISplitState} splitState
* @param {string} featureFlagName
Expand All @@ -72,33 +70,24 @@ export function selectSplitTreatment(splitState: ISplitState, featureFlagName: s
export function selectSplitTreatmentWithConfig(splitState: ISplitState, featureFlagName: string, key?: SplitIO.SplitKey, defaultValue: SplitIO.TreatmentWithConfig = CONTROL_WITH_CONFIG): {
treatment?: SplitIO.TreatmentWithConfig
} & ISplitStatus {
const client = getClient(splitSdk, key, true);
const treatment = selectTreatmentWithConfig(splitState, featureFlagName, key, defaultValue);

// @TODO what should return for user error (wrong key or initSplitSdk action not dispatched yet)
if (!client) return {
treatment: undefined,
isReady: false,
isReadyFromCache: false,
hasTimedout: false,
isDestroyed: false,
isTimedout: false,
lastUpdate: 0
};

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;
const client = getClient(splitSdk, key, true);

const status = getStatus(client);
const status = client ?
getStatus(client) :
{
isReady: false,
isReadyFromCache: false,
hasTimedout: false,
isDestroyed: false,
}

return {
...status,
treatment: treatment ? treatment : defaultValue,
treatment,
isTimedout: status.hasTimedout && !status.isReady,
lastUpdate: splitState.lastUpdate
// @TODO using main client lastUpdate for now
lastUpdate: client ? splitState.lastUpdate : 0
};
}

0 comments on commit 5a41433

Please sign in to comment.