Last active
December 22, 2019 15:24
-
-
Save whitelizard/64cfb1233c549cc3ef32f9d7c7a8ca3c to your computer and use it in GitHub Desktop.
Functional Style with Immutable Store in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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