Skip to content

Instantly share code, notes, and snippets.

@whitelizard
Last active December 22, 2019 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitelizard/64cfb1233c549cc3ef32f9d7c7a8ca3c to your computer and use it in GitHub Desktop.
Save whitelizard/64cfb1233c549cc3ef32f9d7c7a8ca3c to your computer and use it in GitHub Desktop.
Functional Style with Immutable Store in JavaScript
const deepFreeze = item => {
const complex = Object(item) === item;
if (Object.isFrozen(item) || !complex) return item;
Object.values(item).forEach(deepFreeze);
return Object.freeze(item);
};
const createStore = (initState = {}, reducer) => {
let state = deepFreeze(initState);
return {
getState: key => (key ? state[key] : state),
dispatch: action => (state = deepFreeze(reducer(state, action))),
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment