Created
December 24, 2019 19:33
-
-
Save dummerbd/e181313d20fe2d307fe70c3fe175c542 to your computer and use it in GitHub Desktop.
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 logObjectAccess(target, name, depth = 1) { | |
const isProxied = new WeakSet(); | |
return new Proxy(target, { | |
get(target, key, receiver) { | |
const value = Reflect.get(...arguments); | |
console.warn(`Get access to ${name}[${key}]`, value); | |
if ( | |
typeof value === "object" && | |
value !== null && | |
!isProxied.has(value) && | |
depth > 0 | |
) { | |
const proxiedValue = logObjectAccess( | |
value, | |
`${name}.${key}`, | |
depth - 1 | |
); | |
isProxied.add(proxiedValue); | |
return proxiedValue; | |
} | |
return value; | |
}, | |
set(target, key, value, receiver) { | |
console.warn(`Set access to ${name}[${key}]`, value); | |
return Reflect.set(...arguments); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment