Skip to content

Commit

Permalink
Update status property in state
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Sep 6, 2024
1 parent 9e8667d commit 94fd37a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 33 deletions.
47 changes: 34 additions & 13 deletions src/__tests__/reducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { splitReducer } from '../reducer';
import { initialStatus, splitReducer } from '../reducer';
import { splitReady, splitReadyWithEvaluations, splitReadyFromCache, splitReadyFromCacheWithEvaluations, splitTimedout, splitUpdate, splitUpdateWithEvaluations, splitDestroy, addTreatments } from '../actions';
import { ISplitState } from '../types';
import SplitIO from '@splitsoftware/splitio/types/splitio';
Expand All @@ -12,6 +12,7 @@ const initialState: ISplitState = {
isDestroyed: false,
lastUpdate: 0,
treatments: {},
status: {}
};

const key = 'userkey';
Expand Down Expand Up @@ -122,14 +123,19 @@ describe('Split reducer', () => {
splitReducer(initialState, action),
).toEqual({
...initialState,
isReady,
isReadyFromCache,
lastUpdate: action.type === 'ADD_TREATMENTS' ? initialState.lastUpdate : 1000,
treatments: {
test_split: {
[key]: treatments.test_split,
},
},
status: action.type === 'ADD_TREATMENTS' ? {} : {
[key]: {
...initialStatus,
isReady,
isReadyFromCache,
lastUpdate: 1000,
}
}
});

expect(initialState.treatments).toBe(initialTreatments); // control-assert initialState treatments object shouldn't be replaced
Expand All @@ -153,14 +159,19 @@ describe('Split reducer', () => {
reduxState,
).toEqual({
...initialState,
isReady,
isReadyFromCache,
lastUpdate: action.type === 'ADD_TREATMENTS' ? initialState.lastUpdate : 1000,
treatments: {
test_split: {
[key]: newTreatments.test_split,
},
},
status: action.type === 'ADD_TREATMENTS' ? {} : {
[key]: {
...initialStatus,
isReady,
isReadyFromCache,
lastUpdate: 1000,
}
}
});
});

Expand All @@ -183,14 +194,19 @@ describe('Split reducer', () => {
reduxState,
).toEqual({
...initialState,
isReady,
isReadyFromCache,
lastUpdate: action.type === 'ADD_TREATMENTS' ? initialState.lastUpdate : 1000,
treatments: {
test_split: {
[key]: newTreatments.test_split,
},
},
status: action.type === 'ADD_TREATMENTS' ? {} : {
[key]: {
...initialStatus,
isReady,
isReadyFromCache,
lastUpdate: 1000,
}
}
});
});

Expand All @@ -214,14 +230,19 @@ describe('Split reducer', () => {
reduxState,
).toEqual({
...initialState,
isReady,
isReadyFromCache,
lastUpdate: action.type === 'ADD_TREATMENTS' ? initialState.lastUpdate : 1000,
treatments: {
test_split: {
[key]: newTreatments.test_split,
},
},
status: action.type === 'ADD_TREATMENTS' ? {} : {
[key]: {
...initialStatus,
isReady,
isReadyFromCache,
lastUpdate: 1000,
}
}
});
});

Expand Down
53 changes: 33 additions & 20 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,62 @@ export const initialStatus = {
const initialState: ISplitState = {
...initialStatus,
treatments: {},
status: {},
};

function setStatus(state: ISplitState, patch: Partial<IStatus>) {
return {
function setStatus(state: ISplitState, patch: Partial<IStatus>, key?: string) {
return key ? {
...state,
status: {
...state.status,
[key]: state.status[key] ? {
...state.status[key],
...patch,
} : {
...initialStatus,
...patch,
}
},
} : {
...state,
...patch,
};
}

function setReady(state: ISplitState, timestamp: number) {
function setReady(state: ISplitState, timestamp: number, key?: string) {
return setStatus(state, {
isReady: true,
isTimedout: false,
lastUpdate: timestamp,
});
}, key);
}

function setReadyFromCache(state: ISplitState, timestamp: number) {
function setReadyFromCache(state: ISplitState, timestamp: number, key?: string) {
return setStatus(state, {
isReadyFromCache: true,
lastUpdate: timestamp,
});
}, key);
}

function setTimedout(state: ISplitState, timestamp: number) {
function setTimedout(state: ISplitState, timestamp: number, key?: string) {
return setStatus(state, {
isTimedout: true,
hasTimedout: true,
lastUpdate: timestamp,
});
}, key);
}

function setUpdated(state: ISplitState, timestamp: number) {
function setUpdated(state: ISplitState, timestamp: number, key?: string) {
return setStatus(state, {
lastUpdate: timestamp,
});
}, key);
}

function setDestroyed(state: ISplitState, timestamp: number) {
function setDestroyed(state: ISplitState, timestamp: number, key?: string) {
return setStatus(state, {
isDestroyed: true,
lastUpdate: timestamp,
});
}, key);
}

/**
Expand Down Expand Up @@ -98,37 +111,37 @@ export const splitReducer: Reducer<ISplitState> = function (

switch (type) {
case SPLIT_READY:
return setReady(state, timestamp);
return setReady(state, timestamp, key);

case SPLIT_READY_FROM_CACHE:
return setReadyFromCache(state, timestamp);
return setReadyFromCache(state, timestamp, key);

case SPLIT_TIMEDOUT:
return setTimedout(state, timestamp);
return setTimedout(state, timestamp, key);

case SPLIT_UPDATE:
return setUpdated(state, timestamp);
return setUpdated(state, timestamp, key);

case SPLIT_DESTROY:
return setDestroyed(state, timestamp);
return setDestroyed(state, timestamp, key);

case ADD_TREATMENTS: {
const result = { ...state };
return assignTreatments(result, key, treatments);
}

case SPLIT_READY_WITH_EVALUATIONS: {
const result = setReady(state, timestamp);
const result = setReady(state, timestamp, key);
return assignTreatments(result, key, treatments);
}

case SPLIT_READY_FROM_CACHE_WITH_EVALUATIONS: {
const result = setReadyFromCache(state, timestamp);
const result = setReadyFromCache(state, timestamp, key);
return assignTreatments(result, key, treatments);
}

case SPLIT_UPDATE_WITH_EVALUATIONS: {
const result = setUpdated(state, timestamp);
const result = setUpdated(state, timestamp, key);
return assignTreatments(result, key, treatments);
}

Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export interface ISplitState extends IStatus {
[key: string]: SplitIO.TreatmentWithConfig;
};
};
/**
* `status` is a nested object property that contains the readiness status of the non-default clients.
*/
status: {
[key: string]: IStatus;
};
}

export type IGetSplitState = (state: any) => ISplitState;
Expand Down

0 comments on commit 94fd37a

Please sign in to comment.