Skip to content

Commit aec2f26

Browse files
committed
Make setState return the state that was set
1 parent 022e6b0 commit aec2f26

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/Redux.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default class Redux {
3737
setState(nextState) {
3838
this.state = nextState;
3939
this.listeners.forEach(listener => listener());
40+
return nextState;
4041
}
4142

4243
subscribe(listener) {

src/createDispatcher.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import compose from './utils/composeMiddleware';
22

33
export default function createDispatcher(store, middlewares = []) {
44
return function dispatcher(initialState, setState) {
5-
let state = store(initialState, {});
6-
setState(state);
5+
let state = setState(store(initialState, {}));
76

87
function dispatch(action) {
9-
state = store(state, action);
10-
setState(state);
8+
state = setState(store(state, action));
119
return action;
1210
}
1311

test/createDispatcher.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ const { ADD_TODO } = constants;
1010
describe('createDispatcher', () => {
1111

1212
it('should handle sync and async dispatches', done => {
13-
const spy = expect.createSpy(() => {});
13+
const spy = expect.createSpy(
14+
nextState => nextState
15+
).andCallThrough();
16+
1417
const dispatcher = createDispatcher(
1518
composeStores({ todoStore }),
1619
// we need this middleware to handle async actions
17-
getState => [thunkMiddleware(getState)]);
20+
getState => [thunkMiddleware(getState)]
21+
);
1822

1923
expect(dispatcher).toBeA('function');
2024

0 commit comments

Comments
 (0)