Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (state: S | undefined, action: ReturnType) => {
if (action.type === ac.type && state) {
if (isDraftable(state)) {
const draft = createDraft(state);
const reResult = re(draft, action);
const finishedDraft = finishDraft(draft);
if (finishedDraft === state && reResult !== undefined) {
return reResult;
}
return finishedDraft;
}
// Support primitive-returning reducers
return re(state as Draft<s>, action);
}
return (state || s) as any;
};
}</s>
test('For basic data type, we can not modify state', () => {
expect(model.data).toBe('foo');
model.changeData();
expect(model.data).toBe('bar');
expect(model.lastState).toBe('foo');
expect(isDraftable(model.lastState)).toBeFalsy();
});
protected changeState(effect: Function, state: any, action: ActionResponseHandle | ActionNormalHandle): any {
if (isDraftable(state)) {
const draft = createDraft(state);
const responseDraft = effect(draft, action);
if (responseDraft === undefined) {
state = finishDraft(draft);
} else if (isDraft(responseDraft)) {
state = finishDraft(responseDraft);
} else {
state = responseDraft;
}
} else {
state = effect(state, action);
if (state === undefined) {
throw new StateReturnRequiredError(action.type);
}