-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapply-middleware-example.js
More file actions
25 lines (25 loc) · 750 Bytes
/
apply-middleware-example.js
File metadata and controls
25 lines (25 loc) · 750 Bytes
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
import Dispatcher from "./Dispatcher";
import applyMiddleware from "./apply-middleware";
import timestamp from "./timestamp";
import createLogger from "./logger";
const dispatcher = new Dispatcher();
dispatcher.subscribe(action => {
console.log(action);
/*
{ timeStamp: 1463742440479, type: 'FOO' }
*/
});
// Redux compatible middleware API
const state = {};
const middlewareAPI = {
getState(){
// shallow-copy state
return Object.assign({}, state);
},
dispatch(action){
dispatcher.dispatch(action);
}
};
// create `dispatch` function that wrapped with middleware
const dispatchWithMiddleware = applyMiddleware(createLogger(), timestamp)(middlewareAPI);
dispatchWithMiddleware({type: "FOO"});