// https://github.com/mennovanslooten/underscore-observe/tree/master
'use strict';!function(e){'object'==typeof module&&'object'==typeof module.exports?module.exports=function(t){e(t);}:e(_);}((function(e){var t,n=[],r=[],i=!!(Object.keys&&Object.create&&Object.getPrototypeOf&&Object.getOwnPropertyNames&&Object.isSealed&&Object.isFrozen&&Object.isExtensible&&Object.getOwnPropertyDescriptor&&Object.defineProperty&&Object.defineProperties&&Object.seal&&Object.freeze&&Object.preventExtensions);function o(t){var n=!0,r=[],o={generic:[],create:[],update:[],delete:[]};function c(){e.each(o.generic,(function(e){e(t,r);})),r=JSON.parse(JSON.stringify(t));}function u(t,n){e.each(o.create,(function(e){e(t,n);}));}function s(t,n,r){e.each(o.update,(function(e){e(t,n,r);}));}function a(t,n){e.each(o.delete,(function(e){e(t,n);}));}function f(){var n=r.length,i=t.length;if(n!==i||JSON.stringify(r)!==JSON.stringify(t)){for(var o=Math.max(i,n)-1;o>=0;o--){var f=r[o],h=t[o];o>n-1?u(h,o):o>i-1?a(f,o):e.isEqual(h,f)||s(h,f,o);}c();}}function h(e,n){i?Object.defineProperty(t,e,{value:n}):t[e]=n;}return h('pop',(function(){f();var e=Array.prototype.pop.apply(this,arguments);return a(e,this.length),c(),e;})),h('push',(function(){f();var e=arguments[0],t=Array.prototype.push.apply(this,arguments);return u(e,t-1),c(),t;})),h('reverse',(function(){f();for(var t=this.length-1;t>=0;t--)a(this[t],t);var n=Array.prototype.reverse.apply(this,arguments);return e.each(this,u),c(),n;})),h('shift',(function(){f();var e=Array.prototype.shift.apply(this,arguments);return a(e,0),c(),e;})),h('sort',(function(){f();for(var t=this.length-1;t>=0;t--)a(this[t],t);var n=Array.prototype.sort.apply(this,arguments);return e.each(this,u),c(),n;})),h('splice',(function(t){f();for(var n=Array.prototype.slice.call(arguments,2),r=Array.prototype.splice.apply(this,arguments),i=r.length-1;i>=0;i--)a(r[i],t+i);return e.each(n,(function(e,n){u(e,t+n);})),c(),r;})),h('unshift',(function(){f();var t=Array.prototype.unshift.apply(this,arguments);return e.each(arguments,(function(e,t){u(e,t);})),c(),t;})),{detectChanges:f,unbind:function(t,n){e.isUndefined(t)&&e.isUndefined(n)?e.each(o,(function(e){e.length=0;})):e.isString(t)&&e.isUndefined(n)?o[t].length=0:e.isFunction(t)&&e.isUndefined(n)?(n=t,o[t='generic']=e.without(o[t],n)):e.isString(t)&&e.isFunction(n)&&(o[t]=e.without(o[t],n));},bind:function(i,c){o[i].push(c),'generic'===i?c(t,r):'create'===i&&e.each(t,(function(e,t){c(e,t);})),n&&(r=JSON.parse(JSON.stringify(t)),n=!1);}};}function c(){r.length?(e.each(r,(function(e){e.detectChanges();})),u()):t=null;}function u(){t&&clearTimeout(t),t=setTimeout(c,250);}e.mixin({observe:function(t,i,c){if(!e.isArray(t))throw'subject should be a array';e.isFunction(i)&&(c=i,i='generic');var s=e.indexOf(n,t);if(-1===s){s=n.length,n.push(t);var a=new o(t);r.push(a);}return r[s].bind(i,c),u(),t;},unobserve:function(t,i,o){if(!arguments.length)return e.each(r,(function(e){e.unbind();})),n.length=0,void(r.length=0);if(!e.isArray(t))throw'subject should be a array';var c=e.indexOf(n,t);return-1!==c?(r[c].unbind(i,o),t):void 0;}});}));