aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/ensnare.js
blob: b70330a5a8d89fe8172a10d25cc8850ac17350a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module.exports = ensnare

// creates a proxy object that calls cb everytime the obj's properties/fns are accessed
function ensnare(obj, cb){
  var proxy = {}
  Object.keys(obj).forEach(function(key){
    var val = obj[key]
    switch (typeof val) {
      case 'function':
        proxy[key] = function(){
          cb()
          val.apply(obj, arguments)
        }
        return
      default:
        Object.defineProperty(proxy, key, {
          get: function(){ cb(); return obj[key] },
          set: function(val){ cb(); return obj[key] = val },
        })
        return
    }
  })
  return proxy
}