Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Allow returning arbitrary data from the handleAction function #7

Merged
merged 4 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/handle-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,35 @@ test('handles specific action with payload', () => {
expect(newState2).toEqual({ counter: 0 });
expect(newState2).toBe(state);
});

test('handles specific action with payload by returning value directly', () => {
const ac1 = createAction<{ num: number }>('foo3');
const state: { readonly counter: number } = { counter: 0 };
const re = handleAction<typeof state>(ac1, (draft, { payload }) => ({
counter: draft.counter + payload.num,
}), state);
const newState1 = re(state, ac1({ num: 7 }));
expect(newState1).toEqual({ counter: 7 });
expect(newState1).not.toBe(state);
});

test('handles specific action with payload and ignores directly returned value if draft is mutated', () => {
const ac1 = createAction<{ num: number }>('foo4');
const state: { readonly counter: number } = { counter: 0 };
const re = handleAction<typeof state>(ac1, (draft, { payload }) => {
draft.counter += payload.num;
return 'unintended return value';
}, state);
const newState1 = re(state, ac1({ num: 10 }));
expect(newState1).toEqual({ counter: 10 });
expect(newState1).not.toBe(state);
});

test('handles specific action and uses previous state if directly return value is undefined', () => {
const ac1 = createAction<void>('foo5');
const state: { readonly baz: number } = { baz: 0 };
const re = handleAction<typeof state>(ac1, () => undefined, state);
const newState1 = re(state, ac1());
expect(newState1).toEqual({ baz: 0 });
expect(newState1).toBe(state);
});
10 changes: 8 additions & 2 deletions src/handle-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ export default function handleAction<S, AC extends TsActionCreator<any> = any>(
return (state: S | undefined, action: ReturnType<AC>) => {
if (action.type === ac.type && state) {
const draft = createDraft(state);
re(draft, action);
return finishDraft(draft);
const reResult = re(draft, action);
const finishedDraft = finishDraft(draft);

if (finishedDraft === state && reResult !== undefined) {
return reResult;
}

return finishedDraft;
}
return (state || s) as any;
};
Expand Down