forked from happypoulp/redux-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_dispatch-async-action-1.js
97 lines (76 loc) · 3.45 KB
/
08_dispatch-async-action-1.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Tutorial 8 - dispatch-async-action-1.js
// We saw previously how we could dispatch actions and how those actions will modify
// the state of our application thanks to reducers.
// But so far we've only considered synchronous actions, or more exactly action creators
// that produce an action synchronously: when called, an action is returned immediately.
// Let's now imagine a simple asynchronous use-case:
// 1) user clicks on button "Say Hi in 2 second"
// 2) When button "A" is clicked, we'd like to show message "Hi" after 2 seconds have elapsed
// 3) 2 seconds later, our view is updated with the message "Hi"
// Of course this message is part of our application state so we have to save it
// in Redux store. But what we want is to have our store save the message
// only 2 seconds after the action creator is called (because if we were to update our state
// immediately, any subscriber to state's modifications - like our view - would be notified right away
// and would then react to this update 2 seconds too soon).
// If we were to call an action creator like we did until now...
import { createStore, combineReducers } from 'redux'
var reducer = combineReducers({
speaker: function (state = {}, action) {
console.log('speaker was called with state', state, 'and action', action)
switch (action.type) {
case 'SAY':
return {
...state,
message: action.message
}
default:
return state;
}
}
})
var store_0 = createStore(reducer)
var sayActionCreator = function (message) {
return {
type: 'SAY',
message
}
}
console.log("\n", 'Running our normal action creator:', "\n")
console.log(new Date());
store_0.dispatch(sayActionCreator('Hi'))
console.log(new Date());
console.log('store_0 state after action SET_NAME:', store_0.getState())
// Output (skipping initialization output):
// Sun Aug 02 2015 01:03:05 GMT+0200 (CEST)
// speaker was called with state {} and action { type: 'SAY', message: 'Hi' }
// Sun Aug 02 2015 01:03:05 GMT+0200 (CEST)
// store_0 state after action SET_NAME: { speaker: { message: 'Hi' } }
// ... then we see that our store is updated immediately.
// What we'd like instead is an action creator that looks a bit like this:
var asyncSayActionCreator_0 = function (message) {
setTimeout(function () {
return {
type: 'SAY',
message
}
}, 2000)
}
// But then our action creator would not return an action, it would return "undefined". So this is not
// quite the solution we're looking for.
// Here's the trick: instead of returning an action, we'll return a function. And this function will be the
// one to dispatch the action when it seems appropriate to do so. But if we want our function to be able to
// dispatch the action it should be given the dispatch function. Then, this should look like this:
var asyncSayActionCreator_1 = function (message) {
return function (dispatch) {
setTimeout(function () {
dispatch({
type: 'SAY',
message
})
}, 2000)
}
}
// Again you'll notice that our action creator is not returning an action, it is returning a function.
// So there are high chances that our reducers won't know what to do with it. But we never know, so let's
// try it out and find out what would happen...
// Go to next tutorial: dispatch_action_creator-2.js