Skip to content

Instantly share code, notes, and snippets.

@dansalias
Created April 10, 2022 12:11
Show Gist options
  • Save dansalias/76a876e243504f7c9acfcb97a95e65bf to your computer and use it in GitHub Desktop.
Save dansalias/76a876e243504f7c9acfcb97a95e65bf to your computer and use it in GitHub Desktop.
Deeply observe object changes
const reporter = (path: string[] = []) => ({
get(target, property) {
if (typeof target[property] === 'object') {
return new Proxy(target[property], reporter(path.concat(property)))
} else {
return Reflect.get(target, property)
}
},
set(target, property, value) {
console.log('setting ', path.concat(property).join('.'))
return Reflect.set(target, property, value)
},
})
const object: Object = new Proxy({
someProp: {
someDeepProp: {
someValue: true,
},
},
}, reporter())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment