-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-with-middlewares.js
199 lines (156 loc) · 4.03 KB
/
app-with-middlewares.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import 'colors';
import {
createStore,
applyMiddleware,
} from 'redux'
import reducer from './reducer';
import * as ACTION_TYPES from './actionTypes';
console.clear();
console.log(`-- Start ${Date.now()} !`.grey);
// most basic middleware
// test removing next(action) to see if the action reach the reducer or not
// test calling next multiple times
// texst calling next after a delay
const simpleMiddleware1 = store => next => action => {
console.log('MIDDLEWARE 1 '.blue, 'received'.grey, action);
next(action);
};
// testing dispatch
// try to call dispatch without the switch on action.type
// try to call dispatch after a delay
// test the difference between calling next before / after dispatch
// try to call next only in the switch
const simpleMiddleware2 = store => next => action => {
console.log('MIDDLEWARE 2 '.magenta, 'received'.grey, action);
next(action);
const {
dispatch,
} = store;
const {
type,
} = action;
switch (type) {
case ACTION_TYPES.LOGIN: {
dispatch({ type: ACTION_TYPES.ANOTHER_ACTION });
}
}
}
import {
isLoggedSelector,
} from './selectors';
// testing getState
const simpleMiddleware3 = store => next => action => {
console.log('MIDDLEWARE 3 '.yellow, 'received'.grey, action);
next(action);
const {
getState,
} = store;
const state = getState();
const isLogged = isLoggedSelector(state)
console.log('MIDDLEWARE 3 '.yellow, 'state'.grey, isLogged);
};
// testing transforming / extending the action
// test putting this middleware first in applyMiddleware
const simpleMiddleware4 = store => next => action => {
console.log('MIDDLEWARE 4 '.cyan, 'received'.grey, action);
const transformedAction = {
type: `${action.type}-transformed`,
date: new Date().toISOString(),
}
next(transformedAction);
};
import * as service from './service';
// testing output side effect after LOGIN action
// try service.loginThatFail
const httpMiddleware = store => next => action => {
console.log('MIDDLEWARE HTTP '.cyan, 'received'.grey, action);
next(action);
const {
dispatch,
} = store;
const {
type,
} = action;
if (type === ACTION_TYPES.LOGIN) {
dispatch({ type: ACTION_TYPES.LOGIN_REQUESTED });
service
.login()
.then(() => {
dispatch({ type: ACTION_TYPES.LOGIN_SUCCEEDED });
})
.catch(error => {
dispatch({ type: ACTION_TYPES.LOGIN_FAILED });
});
}
};
import * as websocket from './websocket';
// testing input side effect
// try not returning next => action => {}
const websocketMiddleware = store => {
const {
dispatch,
} = store;
websocket.onNewMessage((message) => {
dispatch({
type: ACTION_TYPES.NOTIFICATION_RECEIVED,
message,
});
});
return next => action => {
next(action);
};
};
// And if I want to start websocket *after* login success ?
const websocketAfterLoginMiddleware = store => {
const {
dispatch,
} = store;
let isWebsocketAlreadyRunning = false;
return next => action => {
next(action);
const {
type,
} = action;
switch (type) {
case ACTION_TYPES.LOGIN_SUCCEEDED: {
// be sure to not listen twice to websocket if we receive multiple
// login sucess
if (isWebsocketAlreadyRunning) {
return;
}
websocket.onNewMessage((message) => {
dispatch({
type: ACTION_TYPES.NOTIFICATION_RECEIVED,
message,
});
});
isWebsocketAlreadyRunning = true;
}
}
};
};
const thunkMiddleware = store => next => action => {
const {
dispatch,
getState,
} = store;
if (typeof action === 'function') {
action(dispatch, getState);
} else {
next(action);
}
};
const store = createStore(
reducer,
applyMiddleware(
// thunkMiddleware,
// simpleMiddleware1,
// simpleMiddleware2,
// simpleMiddleware3,
// simpleMiddleware4,
// httpMiddleware,
// websocketMiddleware,
// websocketAfterLoginMiddleware,
),
);
store.dispatch({ type: ACTION_TYPES.LOGIN });