From 00e9f3c6ae2d4092f0c9270d113d7e6dd47ddf0b Mon Sep 17 00:00:00 2001 From: kumavis Date: Sun, 22 May 2016 15:23:16 -0700 Subject: inpage - refactor for modularity --- app/scripts/lib/ensnare.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 app/scripts/lib/ensnare.js (limited to 'app/scripts/lib/ensnare.js') diff --git a/app/scripts/lib/ensnare.js b/app/scripts/lib/ensnare.js new file mode 100644 index 000000000..b70330a5a --- /dev/null +++ b/app/scripts/lib/ensnare.js @@ -0,0 +1,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 +} -- cgit