Redux w/middleware

by Rich Costello

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"></script>

JavaScript

const reducer = (initialState=0, action) => {
	if (action.type === "INC") {
  	return initialState + 1;
    } else if (action.type === "DEC") {
    return initialState - 1;
    } else if (action.type === "E") {
    	throw new Error("AAAAHH SHIT!!!!!");
      }
    return initialState;
}

const logger = (store) => (next) => (action) => {
	console.log("action fired", action);
  next(action);
}

const error = (store) => (next) => (action) => {
	try {
      next(action);
      } catch(e) {
        console.log("AHHHH!!", e);
      }
}

const middleware = Redux.applyMiddleware(logger, error);

const store = Redux.createStore(reducer, 1, middleware);
store.subscribe(() => {
	console.log("store changed", store.getState())
})


store.dispatch({type: "DEC"})
store.dispatch({type: "INC"})
store.dispatch({type: "DEC"})
store.dispatch({type: "INC"})
store.dispatch({type: "INC"})
store.dispatch({type: "INC"})
store.dispatch({type: "E"})