/ contract / src / shared / state / debugging.js
debugging.js
 1  import { compose } from '../utils/general';
 2  
 3  const applyMiddleware = (...middlewares) => {
 4    return createStoreFn => (reducer, preloadedState, enhancer) => {
 5      const store = createStoreFn(reducer, preloadedState, enhancer);
 6      let dispatch = store.dispatch;
 7      let chain = [];
 8  
 9      const middlewareAPI = {
10        getState: store.getState,
11        dispatch: action => dispatch(action)
12      };
13      chain = middlewares.map(middleware => middleware(middlewareAPI));
14      dispatch = compose(...chain)(store.dispatch);
15  
16      return {
17        ...store,
18        dispatch
19      };
20    };
21  };
22  const loggerMiddleware =
23    ({ logActions = true, logState = true } = {}) =>
24    ({ getState }) =>
25    next =>
26    action => {
27      if (logActions) {
28        console.log(`Dispatching ${JSON.stringify(action, null, '\t')}`);
29      }
30      const result = next(action);
31      if (logState) {
32        console.log(`Next state ${JSON.stringify(getState(), null, '\t')}`);
33      }
34      return result;
35    };
36  
37  export { applyMiddleware, loggerMiddleware };