-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghostrap.min.js
6 lines (6 loc) · 4.52 KB
/
ghostrap.min.js
1
2
3
4
5
6
/*!
* ghostrap v1.0.2 - Observe the object property getter, setter or method calls and add custom behavior.
* Copyright (c) 2015 polygon planet <polygon.planet.aqua@gmail.com>
* @license MIT
*/
!function(a,b,c){"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports?module.exports=c():exports[a]=c():"function"==typeof define&&define.amd?define(c):b[a]=c()}("ghostrap",this,function(){"use strict";function a(a){this.init(a)}function b(b){return null!=this&&this instanceof a?this:new a(b)}function c(a,b){var c=Object.getOwnPropertyDescriptor(a.target,b);if(!c)return!1;var d=a.traps[b]||(a.traps[b]={});if(d.trapped)return!0;var e=a.handlers[b]||(a.handlers[b]={});d.value=d.prevValue=d.originalValue=a.target[b],d.originalDesc=c,d.restore=function(){delete a.target[b],m(d.originalValue)?a.target[b]=d.originalValue:(Object.defineProperty(a.target,b,d.originalDesc),m(d.value)||d.value===d.originalValue||(a.target[b]=d.value))};var f=i("get",c,function(){return d.value}),j=i("set",c,function(a){return d.value=a}),k=h(d.value),l=function(){Object.defineProperty(a.target,b,{get:d.get,set:d.set})},n=function(c,d,f,g){if(p(e,d))for(var h=e[d],i=0,j=h.length;j>i;i++)f=h[i].call(c,a.target,b,f,g);return f},o=function(c,e){delete a.target[b],a.target[b]=d.value=f();try{return e.call(c)}catch(g){if(g!==q)throw g}finally{l()}},q={};return g(d,{get:function(){return o(this,function(){return n(this,"beforeget",d.value),d.value=f(),d.value=n(this,"get",d.value),d.value})},set:function(c){return o(this,function(){if(n(this,"beforeset",c),j(c),c=f(),c=n(this,"set",c),d.prevValue!==c&&n(this,"change",c),d.value=d.prevValue=c,m(d.value))throw k=h(d.value),a.target[b]=d.value=d.applyFn,q;return d.value})},applyFn:function(){var a,b=arguments;return n(this,"beforeapply",a,b),a=k.apply(this,b),n(this,"apply",a,b)}}),m(d.value)?a.target[b]=d.applyFn:l(),d.trapped=!0}function d(a,b){b&&(b=[].concat(b),e(a.handlers,b))}function e(a,b,c,d){if(a)if(l(a))f(a,b),c&&d&&c[d]===a&&0===a.length&&delete c[d];else for(var g=Object.keys(a),h=0,i=g.length;i>h;h++){var j=g[h];e(a[j],b,a,j)}}function f(a,b){for(var c=b.length,d=0;d<a.length;d++)for(var e=0;c>e;e++)b[e]===a[d]&&a.splice(d,1)}function g(a){return o(arguments,1).forEach(function(b){for(var c,d=Object.keys(b),e=0,f=d.length;f>e;e++)c=d[e],a[c]=b[c]}),a}function h(a){return m(a)?function(){return a.apply(this,arguments)}:function(){return a}}function i(a,b,c){return b[a]&&h(b[a])||m(b.value)&&h(b.value)||c}function j(a,b){var c,d=!1;return function(){if(d)return c;try{return c=a.apply(this,arguments)}finally{d=!0,a=null,b&&b.apply(this,arguments)}}}function k(a){for(var b=Object.keys(a),c=0,d=b.length;d>c;c++)delete a[b[c]];return a}function l(a){return Array.isArray(a)}function m(a){return"function"==typeof a}var n,o=Array.prototype.slice.call.bind(Array.prototype.slice),p=Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);a.prototype={events:null,init:function(a){if(!a)throw new Error("ghostrap requires more than 1 arguments");return n&&n.has(a)?this.events=n.get(a):(this.events={traps:{},target:a,handlers:{}},n||(n=q()),n.set(a,this.events)),this},on:function(a,b){this._once&&(b=j(b,function(){this.off(a,b)}.bind(this)),delete this._once);var d=this.events;if(!d.target)throw new Error("Target is not defined");var e=a.split(":"),f=e[0].toLowerCase(),g=e[1];if(!p(d.traps,g)&&(d.traps[g]={},d.handlers[g]={},!c(d,g)))throw new Error("Failed to set handler");var h=d.handlers[g];return h[f]||(h[f]=[]),h[f].push(b),this},once:function(a,b){return this._once=!0,this.on(a,b)},off:function(a,b){var c=this.events;if(0===arguments.length){var e=c.target;return this.clear(),this.init(e),this}if(null==b&&(m(a)||l(a)&&m(a[0])))return this.off("*",a);if("*"===a)return d(c,b),this;var f=a.split(":"),g=f[0].toLowerCase(),h=f[1],i=c.handlers[h];if(i){var j=i[g];j&&j.length>0&&(null==b?j.length=0:i[g]=j.filter(function(a){return a!==b})),j&&0===j.length&&delete i[g]}return this},clear:function(){var a=this.events;return Object.keys(a.traps).forEach(function(b){var c=a.traps[b];c&&c.restore&&c.restore()}),a.trapper&&k(a.trapper),k(a.traps),k(a.handlers),n["delete"](a.target),a.target=null,this}};var q;return q="undefined"!=typeof WeakMap?function(){return new WeakMap}:function(){function a(){this._id=["","WeakMap",Date.now(),Math.random()].join(".")}return a.prototype={set:function(a,b){Object.defineProperty(a,this._id,{configurable:!0,value:b})},get:function(a){return a[this._id]},has:function(a){return p(a,this._id)},"delete":function(a){return delete a[this._id]}},function(){return new a}}(),b});