Created
April 10, 2022 12:11
-
-
Save dansalias/76a876e243504f7c9acfcb97a95e65bf to your computer and use it in GitHub Desktop.
Deeply observe object changes
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 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