-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
139 lines (104 loc) · 2.59 KB
/
demo.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
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);
const simpleMiddleware1 = store => next => action => {
console.log('MIDDLEWARE 1 '.blue, 'received'.grey, action);
// next(action);
};
const simpleMiddleware2 = store => next => action => {
console.log('MIDDLEWARE 2 '.magenta, 'received'.grey, action);
next(action);
const {
dispatch,
} = store;
const {
type,
} = action;
dispatch({ type: ACTION_TYPES.ANOTHER_ACTION });
}
const simpleMiddleware3 = store => next => action => {
// import {
// isLoggedSelector,
// } from './selectors';
console.log('MIDDLEWARE 3 '.yellow, 'received'.grey, action);
next(action);
const {
getState,
} = store;
const state = 'what';
console.log('MIDDLEWARE 3 '.yellow, 'state'.grey, isLogged);
};
const simpleMiddleware4 = store => next => action => {
console.log('MIDDLEWARE 4 '.cyan, 'received'.grey, action);
const transformedAction = action;
next(transformedAction);
};
const httpMiddleware = store => next => action => {
// import * as service from './service';
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';
// And if I want to start websocket *after* login success ?
const websocketAfterLoginMiddleware = store => {
const {
dispatch,
} = store;
websocket.onNewMessage((message) => {
dispatch({
type: ACTION_TYPES.NOTIFICATION_RECEIVED,
message,
});
});
return next => action => {
next(action);
};
};
const thunkMiddleware = store => next => action => {
console.log('THUNK '.blue, 'received'.grey, action);
const {
dispatch,
getState,
} = store;
if (typeof action === 'function') {
action(dispatch, getState);
} else {
next(action);
}
};
const store = createStore(
reducer,
applyMiddleware(
// simpleMiddleware1,
// simpleMiddleware2,
// simpleMiddleware3,
// simpleMiddleware4,
// thunkMiddleware,
// httpMiddleware,
// websocketAfterLoginMiddleware,
),
);
store.dispatch({ type: ACTION_TYPES.LOGIN });