Skip to content

Instantly share code, notes, and snippets.

@pirogoeth
Created April 21, 2018 18:27
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 pirogoeth/6cb5e07a926183b1c72f0642306996b9 to your computer and use it in GitHub Desktop.
Save pirogoeth/6cb5e07a926183b1c72f0642306996b9 to your computer and use it in GitHub Desktop.
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