Skip to content

Instantly share code, notes, and snippets.

@esamattis
Last active October 7, 2016 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esamattis/14bbe3c803092fa234efade31de6e134 to your computer and use it in GitHub Desktop.
Save esamattis/14bbe3c803092fa234efade31de6e134 to your computer and use it in GitHub Desktop.
import deepFreeze from "deep-freeze";
import isPlainObject from "lodash/fp/isPlainObject";
export function wrapSanityChecks(reducer) {
if (process.env.NODE_ENV === "production") return reducer;
return (state, action) => {
if (!isPlainObject(action)) {
const msg = `Bad action object of type ${typeof action}`;
console.log(msg, action);
throw new Error(msg);
}
// Actions should be serializable so that they can be replayed using
// Redux Devtools. Serializable actions also would allow sending them
// over network etc.
try {
JSON.stringify(action);
} catch (error) {
const msg = "Invalid action! Actions must be serializable";
console.log(msg, action);
throw new Error(msg);
}
// Ensure immutable state and actions. Reducers should never mutate
// those.
state = reducer(deepFreeze(state), deepFreeze(action));
if (!isPlainObject(state)) {
const msg = `State screwed by action ${action.type}. Store state is now '${typeof state}'`;
console.log(msg, action);
throw new Error(msg);
}
return state;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment