Created
January 16, 2021 22:45
-
-
Save jeiea/e788dc99a0ece17aa0dfff715eadaabd to your computer and use it in GitHub Desktop.
tried monitoring object
This file contains hidden or 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 collapsedTrace = (message) => { | |
console.groupCollapsed(message); | |
console.trace(); | |
console.groupEnd(); | |
}; | |
const isPrimitive = (x) => x !== Object(x); | |
const proxy = (target, path) => { | |
if (isPrimitive(target)) { | |
return target; | |
} | |
const proxies = {}; | |
return new Proxy(target, { | |
get: (target, property, receiver) => { | |
const child = `${path || 'obj'}.${property}`; | |
collapsedTrace(`get ${child}`); | |
if (!proxies[property]) { | |
const original = Reflect.get(target, property, receiver); | |
proxies[property] = proxy(original, child); | |
} | |
return proxies[property]; | |
}, | |
set: (target, property, value) => { | |
collapsedTrace(`set ${path || 'obj'}.${property}:`, value); | |
delete proxies[property]; | |
return Reflect.set(target, property, value); | |
}, | |
}); | |
}; | |
const hook = (target, property) => { | |
let proxied; | |
const set = (value) => { | |
proxied = proxy(typeof value === 'function' ? value.bind(target) : value, property); | |
}; | |
set(target[property]); | |
return Object.defineProperty(target, property, { | |
get: () => { | |
collapsedTrace(`get ${property}`); | |
return proxied; | |
}, | |
set: (value) => { | |
collapsedTrace(`set ${property}`); | |
set(value); | |
}, | |
}); | |
}; | |
console.log('VM'); | |
hook(this, '_actions'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment