-
Notifications
You must be signed in to change notification settings - Fork 1
/
thunk.js
45 lines (35 loc) · 880 Bytes
/
thunk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
var redux = require('./redux.js');
function thunk (store) {
return function (dispatch) {
return function (action) {
if (typeof action === 'function') {
return action(store.dispatch, store.getState);
}
return dispatch(action);
}
}
}
var createStoreSubstitute = redux.applyMiddleware(
thunk
)(redux.createStore);
function reducer (state, action) {
if (action.type === 'one') return 'one';
return 'none';
}
function actionCreator () { return { type:'two' }; }
var store = createStoreSubstitute(reducer, 'start');
function asyncStuff (item) {
return function (dispatch,getState) {
process.nextTick(function () {
dispatch(actionCreator);
});
};
}
store.dispatch(asyncStuff());
console.log(store.getState());
// -> 'start'
process.nextTick(() => {
console.log(store.getState());
// -> 'none'
});