Skip to content

Instantly share code, notes, and snippets.

@dounan
Last active September 20, 2017 23:28
Show Gist options
  • Save dounan/ab5c5870b2fa7e8189790301b09b83ca to your computer and use it in GitHub Desktop.
Save dounan/ab5c5870b2fa7e8189790301b09b83ca to your computer and use it in GitHub Desktop.
Mutation detection via proxy example
function wrap(obj) {
return new Proxy(obj, {
set: (target, property, value, receiver) => {
if (target[property] !== value) {
console.warn("MUTATION DETECTED!");
}
// Perform the normal set()
target[property] = value;
return true;
},
});
}
const obj = {};
const wrappedObj = wrap(obj);
wrappedObj.value = 'oops'; // console should log "MUTATION DETECTED!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment