Skip to content

Commit

Permalink
Pass extraArguments in createThunk
Browse files Browse the repository at this point in the history
  • Loading branch information
klis87 committed Aug 4, 2020
1 parent 0478144 commit 0651517
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ than passing as param to action).

But what about constants? This is the main benefit of `create-thunk`. Imagine a thunk like that:
```js
const doSth = () => (dispatch, getState) => {
const doSth = () => (dispatch, getState, extraArguments) => {
const state = getState();
dispatch({ type: 'EXTRA_ACTION' });
return dispatch({ type: 'DO_STH', x: state.x });
Expand All @@ -83,7 +83,7 @@ import { createAction, createThunk } from 'redux-smart-actions';

const extraAction = createAction('EXTRA_ACTION');

const doSth = createThunk('DO_STH', () => (dispatch, getState) => {
const doSth = createThunk('DO_STH', () => (dispatch, getState, extraArguments) => {
const state = getState();
dispatch(extraAction());
return { x: state.x };
Expand Down
8 changes: 6 additions & 2 deletions src/create-thunk.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const createThunk = (name, thunk) => {
const thunkCreator = (...params) => (dispatch, getState) => {
const actionToDispatch = thunk(...params)(dispatch, getState);
const thunkCreator = (...params) => (dispatch, getState, extraArguments) => {
const actionToDispatch = thunk(...params)(
dispatch,
getState,
extraArguments,
);

if (!actionToDispatch) {
return null;
Expand Down
15 changes: 15 additions & 0 deletions src/create-thunk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ describe('createThunk', () => {
store.dispatch(thunkCreator());
expect(store.getActions()).toEqual([]);
});

it('is compatible with thunk extra arguments', () => {
const mockStore = configureStore([
thunkMiddleware.withExtraArgument('test'),
]);
const store = mockStore({});
const thunkCreator = createThunk(
'NAME',
() => (dispatch, getState, test) => {
return { test };
},
);
store.dispatch(thunkCreator());
expect(store.getActions()).toEqual([{ type: 'NAME', test: 'test' }]);
});
});
14 changes: 12 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ export function createThunk<Output = {}, Input extends any[] = any>(
name: string,
thunk: (
...params: Input
) => (dispatch: (action: any) => any, getState: () => any) => Output,
) => (
dispatch: (action: any) => any,
getState: () => any,
extraArguments: any,
) => Output,
): (
...params: Input
) => (
dispatch: (action: any) => any,
getState: () => any,
extraArguments: any,
) => Output & { type: string };

export function createSmartAction<Output = {}, Input extends any[] = any>(
Expand All @@ -22,10 +27,15 @@ export function createSmartAction<Output = {}, Input extends any[] = any>(
export function createSmartThunk<Output = {}, Input extends any[] = any>(
thunk: (
...params: Input
) => (dispatch: (action: any) => any, getState: () => any) => Output,
) => (
dispatch: (action: any) => any,
getState: () => any,
extraArguments: any,
) => Output,
): (
...params: Input
) => (
dispatch: (action: any) => any,
getState: () => any,
extraArguments: any,
) => Output & { type: string };

0 comments on commit 0651517

Please sign in to comment.