2016-05-23 00:23:16 +02:00
|
|
|
module.exports = ensnare
|
|
|
|
|
|
|
|
// creates a proxy object that calls cb everytime the obj's properties/fns are accessed
|
2016-06-21 22:18:32 +02:00
|
|
|
function ensnare (obj, cb) {
|
2016-05-23 00:23:16 +02:00
|
|
|
var proxy = {}
|
2016-06-21 22:18:32 +02:00
|
|
|
Object.keys(obj).forEach(function (key) {
|
2016-05-23 00:23:16 +02:00
|
|
|
var val = obj[key]
|
|
|
|
switch (typeof val) {
|
|
|
|
case 'function':
|
2016-06-21 22:18:32 +02:00
|
|
|
proxy[key] = function () {
|
2016-05-23 00:23:16 +02:00
|
|
|
cb()
|
|
|
|
val.apply(obj, arguments)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
Object.defineProperty(proxy, key, {
|
2016-06-21 22:18:32 +02:00
|
|
|
get: function () { cb(); return obj[key] },
|
|
|
|
set: function (val) { cb(); obj[key] = val; return val },
|
2016-05-23 00:23:16 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return proxy
|
|
|
|
}
|