Created
April 21, 2018 18:27
-
-
Save pirogoeth/6cb5e07a926183b1c72f0642306996b9 to your computer and use it in GitHub Desktop.
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 initialState = { | |
debug: false, | |
item: null, | |
}; | |
function makeProxyContext(currentState, updateState) { | |
let cur = Object.assign({}, currentState); | |
let proxyState = new Proxy(updateState, { | |
get(_, prop, receiver) { | |
return cur[prop]; | |
}, | |
set(target, prop, value) { | |
console.log('updating updateState', prop, value); | |
target[prop] = value; | |
}, | |
}); | |
return proxyState; | |
} | |
function action() { | |
let previousItem = this.item; | |
if ( !previousItem ) { | |
let nextItem = 'hello'; | |
this.item = nextItem; | |
} | |
} | |
let previousState = Object.assign({}, initialState); | |
let updateState = Object.create(null); | |
let updateContext = makeProxyContext(previousState, updateState); | |
action.apply(updateContext); | |
console.log(updateState); | |
console.log(previousState); | |
let nextState = Object.assign({}, previousState, updateState); | |
console.log(nextState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment