Last active
September 20, 2017 23:28
-
-
Save dounan/ab5c5870b2fa7e8189790301b09b83ca to your computer and use it in GitHub Desktop.
Mutation detection via proxy example
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
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