UNPKG

watchjs

Version:

A utility for watching object changes.

1 lines 3.26 kB
"use strict";(function(e){if(typeof exports==="object"){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{window.WatchJS=e();window.watch=window.WatchJS.watch;window.unwatch=window.WatchJS.unwatch;window.callWatchers=window.WatchJS.callWatchers}})(function(){var e={noMore:false},t,n,r;var i=function(e){var t={};return e&&t.toString.call(e)=="[object Function]"};var s=function(e){return e%1===0};var o=function(e){return Object.prototype.toString.call(e)==="[object Array]"};var u=function(){return Object.defineProperty||Object.prototype.__defineGetter__};var a=function(e,t,n,r){try{Object.defineProperty(e,t,{get:n,set:r,enumerable:true,configurable:true})}catch(i){try{Object.prototype.__defineGetter__.call(e,t,n);Object.prototype.__defineSetter__.call(e,t,r)}catch(s){throw"watchJS error: browser not supported :/"}}};var f=function(e,t,n){try{Object.defineProperty(e,t,{enumerable:false,configurable:true,writable:false,value:n})}catch(r){e[t]=n}};var l=function(){if(i(arguments[1])){c.apply(this,arguments)}else if(o(arguments[1])){h.apply(this,arguments)}else{p.apply(this,arguments)}};var c=function(e,t,n){if(e instanceof String||!(e instanceof Object)&&!o(e)){return}var r=[];if(o(e)){for(var i=0;i<e.length;i++){r.push(i)}}else{for(var s in e){r.push(s)}}h(e,r,t,n)};var h=function(e,t,n,r){for(var i in t){p(e,t[i],n,r)}};var p=function(e,n,r,s){if(i(e[n])){return}if(e[n]!=null&&(s===undefined||s>0)){if(s!==undefined){s--}c(e[n],r,s)}t(e,n,r)};var d=function(){if(i(arguments[1])){v.apply(this,arguments)}else if(o(arguments[1])){m.apply(this,arguments)}else{n.apply(this,arguments)}};var v=function(e,t){if(e instanceof String||!(e instanceof Object)&&!o(e)){return}var n=[];if(o(e)){for(var r=0;r<e.length;r++){n.push(r)}}else{for(var i in e){n.push(i)}}m(e,n,t)};var m=function(e,t,r){for(var i in t){n(e,t[i],r)}};if(u()){t=function(t,n,i){var s=t[n];b(t,n);if(!t.watchers){f(t,"watchers",{})}if(!t.watchers[n]){t.watchers[n]=[]}t.watchers[n].push(i);var o=function(){return s};var u=function(o){var u=s;s=o;if(t[n]){c(t[n],i)}b(t,n);if(!e.noMore){if(JSON.stringify(u)!==JSON.stringify(o)){r(t,n,"set",o,u);e.noMore=false}}};a(t,n,o,u)};r=function(e,t,n,r,i){for(var o in e.watchers[t]){if(s(o)){e.watchers[t][o].call(e,t,n,r,i)}}};var g=["pop","push","reverse","shift","sort","slice","unshift"];var y=function(e,t,n,i){f(e[t],i,function(){var s=n.apply(e[t],arguments);p(e,e[t]);if(i!=="slice"){r(e,t,i,arguments)}return s})};var b=function(e,t){if(!e[t]||e[t]instanceof String||!o(e[t])){return}for(var n=g.length,r;n--;){r=g[n];y(e,t,e[t][r],r)}};n=function(e,t,n){for(var r in e.watchers[t]){var i=e.watchers[t][r];if(i==n){e.watchers[t].splice(r,1)}}}}else{var w=[];t=function(e,t,n){w.push({obj:e,prop:t,serialized:JSON.stringify(e[t]),watcher:n})};n=function(e,t,n){for(var r in w){var i=w[r];if(i.obj==e&&i.prop==t&&i.watcher==n){w.splice(r,1)}}};r=function(e,t,n,r){for(var i in w){var s=w[i];if(s.obj==e&&s.prop==t){s.watcher.call(e,t,n,r)}}};var E=function(){for(var e in w){var t=w[e];var n=JSON.stringify(t.obj[t.prop]);if(n!=t.serialized){t.watcher.call(t.obj,t.prop,t.obj[t.prop],JSON.parse(t.serialized));t.serialized=n}}};setInterval(E,50)}e.watch=l;e.unwatch=d;e.callWatchers=r;return e})