This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
/
Statesman.min.js
executable file
·1 lines (1 loc) · 12.5 KB
/
Statesman.min.js
1
(function(t){"use strict";var e,n,s,i,o,r,a,h,u,c,f,p,l,d,g,v,y,b,m,O,w,x="",j={},P={},k=Object.prototype.hasOwnProperty,D={};try{Object.defineProperty({},"test",{value:0}),Object.defineProperties({},{test:{value:0}}),O=Object.defineProperty,w=Object.defineProperties}catch(C){O=function(t,e,n){t[e]=n.value},w=function(t,e){var n;for(n in e)k.call(e,n)&&O(t,n,e[n])}}(function(){var t,n,s,o,r,a,h,u,c,f;c=["data","computed"],f=c,i=function(t){var n,s=this;return t||(t={}),n=function(t){u(this,n,t||{})},n.prototype=o(s.prototype),s!==e&&r(n,s),h(n,t),n.extend=i,n},r=function(t,e){c.forEach(function(s){e[s]&&(t[s]=n(e[s]))})},a=function(t,e){return/_super/.test(t)?function(){var n,s=this._super;return this._super=e,n=t.apply(this,arguments),this._super=s,n}:t},h=function(t,e){var n,i;c.forEach(function(n){var i=e[n];i&&(t[n]?s(t[n],i):t[n]=i)});for(n in e)k.call(e,n)&&!k.call(t.prototype,n)&&-1===f.indexOf(n)&&(i=e[n],t.prototype[n]="function"==typeof i&&"function"==typeof t.prototype[n]?a(i,t.prototype[n]):i)},u=function(t,i,o){i.data&&(o=s(n(i.data),o)),e.call(t,o),i.computed&&t.compute(i.computed),t.init&&t.init.call(t,o)},t=function(t,e){var n;for(n in e)k.call(e,n)&&!k.call(t,n)&&(t[n]=e[n])},n=function(t){var e,n={};for(e in t)k.call(t,e)&&(n[e]=t[e]);return n},s=function(t,e){var n;for(n in e)k.call(e,n)&&(t[n]=e[n]);return t};try{Object.create(null),o=Object.create}catch(p){o=function(){var t=function(){};return function(e,n){var s;return t.prototype=e,s=new t,n&&Object.defineProperties(s,n),s}}()}})(),e=function(t){w(this,{data:{value:t||{},writable:!0},subs:{value:{},writable:!0},cache:{value:{}},cacheMap:{value:{}},deps:{value:{}},depsMap:{value:{}},refs:{value:{}},refsMap:{value:{}},computed:{value:{}},subsets:{value:{}},deferred:{value:[]},changes:{value:null,writable:!0},upstreamChanges:{value:null,writable:!0},changeHash:{value:null,writable:!0}})},j.add=function(t,e){var n=this.get(t);void 0===e&&(e=1),h(n)&&h(e)&&this.set(t,+n+(void 0===e?1:+e))},function(t){var e,n,s,i,r;t.compute=function(t,n){var s,i,o;if("object"==typeof t){s={};for(i in t)t.hasOwnProperty(i)&&(o=new e(this,i,t[i]),s[i]=o.value);return s}return o=new e(this,t,n),o.value},e=function(t,e,i){var r;if(t.computed[e]&&t.computed[e].teardown(),this.statesman=t,this.keypath=e,t.computed[e]=this,"string"==typeof i?i=o(i,t):("function"==typeof i&&(i=i()),s(e,i,t.debug)),this.signature=i,this.cache=i.cache,this.async=i.async,this.context=i.context||t,this.refs=[],r=i.dependsOn.length,this.cache)for(1===r&&(this.selfUpdating=!0);r--;)this.refs[r]=new n(this,i.dependsOn[r]);this.setting=!0,t.set(this.keypath,this.value=this.getter()),this.setting=!1},e.prototype={bubble:function(){this.selfUpdating?this.update():this.deferred||(this.statesman.deferred.push(this),this.deferred=!0)},update:function(){var t;return t=this.getter(),a(t,this.value)||(this.setting=!0,f(this.statesman,this.keypath,t),this.setting=!1,this.value=t),this},getter:function(){var t,e,n,s,i,o,r,a,h=this;s=this.statesman;try{if(this.signature.compiled)n=this.signature.compiled();else{if(e=[],this.async&&(o=this.context.async,this.context.async=function(){return function(t){a?(h.setting=!0,s.set(h.keypath,t),h.setting=!1):(i=!0,r=t)}}),this.cache){for(t=this.refs.length;t--;)e[t]=this.refs[t].value;n=this.signature.get.apply(this.context,e)}else{for(t=this.signature.dependsOn.length;t--;)e[t]=s.get(this.signature.dependsOn[t]);n=this.signature.get.apply(this.context,e)}a=!0,this.async&&(this.context.async=o,i?n=r:void 0===n&&(n=this.value))}}catch(u){if(s.debug)throw u;n=void 0}return this.override=!1,n},setter:function(t){if(this.signature.set)try{this.signature.set.call(this.context,t)}catch(e){if(this.statesman.debug)throw e}else if(this.signature.readonly){if(this.statesman.debug)throw Error('You cannot overwrite a computed value ("'+this.keypath+'"), unless its readonly flag is set true')}else this.override=!0,this.setting=!0,this.statesman.set(this.keypath,t),this.setting=!1},teardown:function(){for(;this.refs.length;)this.refs.pop().teardown(),this.statesman.computed[this.keypath]=null}},n=function(t,e){this.computed=t,this.statesman=t.statesman,this.keypath=e,this.value=this.statesman.get(e),b(this,!0)},n.prototype={update:function(){var t;t=this.statesman.get(this.keypath),a(t,this.value)||(this.value=t,this.computed.bubble())},teardown:function(){m(this,!0)}},i=[],r=/async/,s=function(t,e,n){if(!e.compiled){if(!e.get&&!e.set)throw Error("Computed values must have either a get() or a set() method, or both");if(e.set||e.readonly===!1||(e.readonly=!0),e.dependsOn?"string"==typeof e.dependsOn&&(e.dependsOn=[e.dependsOn]):e.dependsOn=i,!e.dependsOn.length){if(e.cache&&n)throw Error("Computed values with no dependencies must be uncached");e.cache=!1}e.cache!==!1&&(e.cache=!0),e.get&&r.test(e.get)&&(e.async=!0)}if(-1!==e.dependsOn.indexOf(t))throw Error('A computed value ("'+t+'") cannot depend on itself');return e}}(j),j.get=j.toJSON=function(t){return p(this,t&&u(t))},p=function(t,e,n,s){var i,o,r,a,h;return e?((i=t.computed[e])&&(s||i.cache||i.override||(t.cache[e]=i.getter())),t.cache.hasOwnProperty(e)?t.cache[e]:(n=n||e.split("."),o=n.pop(),r=n.join("."),a=p(t,r,n),"object"==typeof a&&a.hasOwnProperty(o)&&(h=a[o],t.cache[e]=h,t.cacheMap[r]||(t.cacheMap[r]=[]),t.cacheMap[r].push(e)),h)):t.data},function(t){var e;t.observe=function(t,n,s){var i,o,r,a,h;if("function"==typeof t&&(s=n,n=t,t=""),h=!s||s.init!==!1,"string"==typeof t)return i=new e(this,t,n,s),h?i.update():i.value=this.get(t),{cancel:function(){i.teardown()}};if("object"!=typeof t)throw Error("Bad arguments to Statesman.prototype.observe()");s=n,o=[];for(r in t)t.hasOwnProperty(r)&&(o[o.length]=new e(this,r,t[r],s));if(a=o.length,h)for(;a--;)o[a].update();else for(;a--;)o[a].value=this.get(i.keypath);return{cancel:function(){for(a=o.length;a--;)o[a].teardown()}}},t.unobserve=function(t){var n,s;if(t=void 0===t?"":u(t),n=this.deps[t])for(s=n.length;s--;)n[s]instanceof e&&n[s].teardown()},t.unobserveAll=function(){var t;for(t in this.deps)this.unobserve(t)},e=function(t,e,n,s){this.statesman=t,this.keypath=u(e),this.callback=n,this.context=s&&s.context?s.context:t,b(this)},e.prototype={update:function(){var t;if(t=p(this.statesman,this.keypath),!a(t,this.value)){try{this.callback.call(this.context,t,this.value)}catch(e){if(this.statesman.debug)throw e}this.value=t}},teardown:function(){m(this)}}}(j),j.removeComputedValue=function(t){return this.computed[t]&&this.computed[t].teardown(),this},j.reset=function(t){return this.data={},this.set(t,{silent:!0}),this.fire("reset"),d(this,""),this},function(t){var e,n,s=/^\s*[0-9]+\s*$/;t.set=function(t,e,s){var i,o,r,a;if(this.changes=[],this.upstreamChanges=[],this.changeHash={},"object"==typeof t){s=e;for(r in t)t.hasOwnProperty(r)&&(a=u(r),e=t[r],f(this,a,e))}else a=u(t),f(this,a,e);for(i=[],o=[];this.changes.length;)n(i,this.changes),n(o,this.upstreamChanges),v(this);return s&&s.silent?this:(g(this,o,!0),i.length&&g(this,i),i.length&&this.fire("change",this.changeHash),this)},f=function(t,n,s){var i,o,r;if((r=t.computed[n])&&!r.setting)return r.setter(s),void 0;if(i=p(t,n,null,!0),i!==s)e(t.data,n,s);else if("object"!=typeof s)return;for(l(t,n),t.changes[t.changes.length]=n,t.changeHash[n]=s,o=n.split(".");o.length&&(o.pop(),n=o.join("."),!t.upstreamChanges[n]);)t.upstreamChanges[n]=!0,t.upstreamChanges.push(n)},e=function(t,e,n){for(var i,o=e.split(".");o.length>1;)i=o.shift(),t[i]||(t[i]=s.test(o[0])?[]:{}),t=t[i];t[o[0]]=n},n=function(t,e){for(var n,s=e.length;s--;)n=e[s],t["_"+n]||(t["_"+n]=!0,t[t.length]=n)}}(j),j.subset=function(t){if(!t)throw"No subset path specified";return this.subsets[t]||(this.subsets[t]=new n(t,this)),this.subsets[t]},j.subtract=function(t,e){var n=this.get(t);void 0===e&&(e=1),h(n)&&h(e)&&this.set(t,+n-(void 0===e?1:+e))},j.toggle=function(t){this.set(t,!this.get(t))},l=function(t,e){var n=t.cacheMap[e];if(delete t.cache[e],n)for(;n.length;)l(t,n.pop())},h=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},d=function(t,e,n){var s,i,o;if(s=t.deps[e])for(i=s.length;i--;)s[i].update();if(!n&&(o=t.depsMap[e]))for(i=o.length;i--;)d(t,o[i])},g=function(t,e,n){var s;for(s=e.length;s--;)d(t,e[s],n)},v=function(t){var e,n,s,i,o;for(n=t.changes,s=t.upstreamChanges,t.changes=[],t.upstreamChanges=[],e=s.length;e--;)i=s[e],y(t,i,!0);for(e=n.length;e--;)i=n[e],y(t,i);for(;t.deferred.length;)o=t.deferred.pop(),o.update(),o.deferred=!1},y=function(t,e,n){var s,i,o;if(s=t.refs[e])for(o=s.length;o--;)s[o].update();if(!n&&(i=t.refsMap[e]))for(o=i.length;o--;)y(t,i[o])},b=function(t,e){var n,s,i,o,r,a,h,u;for(n=t.statesman,s=t.keypath,e?(h=n.refs,u=n.refsMap):(h=n.deps,u=n.depsMap),i=h[s]||(h[s]=[]),i[i.length]=t,o=s.split(".");o.length;)o.pop(),r=o.join("."),a=u[r]||(u[r]=[]),void 0===a[s]&&(a[s]=0,a[a.length]=s),a[s]+=1,s=r},m=function(t,e){var n,s,i,o,r,a,h,u;for(n=t.statesman,s=t.keypath,e?(h=n.refs,u=n.refsMap):(h=n.deps,u=n.depsMap),i=h[s],i.splice(i.indexOf(t),1),o=s.split(".");o.length;)o.pop(),r=o.join("."),a=u[r],a[s]-=1,a[s]||(a.splice(a.indexOf(s),1),a[s]=void 0),s=r},r={total:function(t){return t.reduce(function(t,e){return t+e})}},n=function(t,e){var n,s,i=this;this.path=t,this.pathDot=t+".",this.root=e,this.subs={},n=RegExp("^"+this.pathDot.replace(".","\\.")),s=this.pathDot.length,this.root.on("change",function(t){var e,o,r,a;r={};for(o in t)t.hasOwnProperty(o)&&n.test(o)&&(e=o.substring(s),r[e]=t[o],a=!0);a&&i.fire("change",r)})},P.add=function(t,e){this.root.add(this.pathDot+t,e)},function(t){var e;t.compute=function(t,n){var s,i;if("object"==typeof t){s={};for(i in t)t.hasOwnProperty(i)&&(s[i]=e(this,i,t));return s}return e(this,t,n)},e=function(t,e,n){var s,i=t.pathDot;if("string"==typeof n)return n=o(n,t.root,i),t.root.compute(i+e,n);if("function"==typeof n&&(n=n()),n.dependsOn)for("string"==typeof n.dependsOn&&(n.dependsOn=[n.dependsOn]),s=n.dependsOn.length;s--;)n.dependsOn=i+n.dependsOn;return n.context||(n.context=t),t.root.compute(i+e,n)}}(P),P.get=function(t){return t?this.root.get(this.pathDot+t):this.root.get(this.path)},P.observe=function(t,e,n){var s,i;if("object"==typeof t){n=e,i={};for(s in t)t.hasOwnProperty(s)&&(i[this.pathDot+s]=t[s]);return n?n.context=n.context||this:n={context:this},this.root.observe(i,n)}return"function"==typeof t?(n=e,e=t,t=this.path):t=""===t?this.path:this.pathDot+t,n?n.context=n.context||this:n={context:this},this.root.observe(t,e,n)},P.removeComputedValue=function(t){return this.root.removeComputedValue(this.pathDot+t),this},P.reset=function(t){return this.root.set(this.path,t),this},P.set=function(t,e,n){var s,i;if("object"==typeof t){n=e,i={};for(s in t)t.hasOwnProperty(s)&&(i[this.pathDot+s]=t[s]);return this.root.set(i,n),this}return this.root.set(this.pathDot+t,e,n),this},P.subset=function(t){return this.root.subset(this.pathDot+t)},P.subtract=function(t,e){this.root.subtract(this.pathDot+t,e)},P.toggle=function(t){this.root.toggle(this.pathDot+t)},s={},s.on=function(t,e){var n,s,i,o=this;if("object"==typeof t){i=[];for(s in t)t.hasOwnProperty(s)&&(i[i.length]=this.on(s,t[s]));return{cancel:function(){for(;i.length;)i.pop().cancel()}}}return this.subs[t]||(this.subs[t]=[]),n=this.subs[t],n[n.length]=e,{cancel:function(){o.off(t,e)}}},s.once=function(t,e){var n,s,i,o,r=this;if("object"==typeof t){i=[];for(s in t)t.hasOwnProperty(s)&&(i[i.length]=this.once(s,t[s]));return{cancel:function(){for(;i.length;)i.pop().cancel()}}}return this.subs[t]||(this.subs[t]=[]),n=this.subs[t],o=function(){e.apply(r,arguments),r.off(t,o)},n[n.length]=o,{cancel:function(){r.off(t,o)}}},s.off=function(t,e){var n,s;return t?e?(n=this.subs[t],n&&(s=n.indexOf(e),-1!==s&&n.splice(s,1),n.length||delete this.subs[t]),this):(delete this.subs[t],this):(this.subs={},this)},s.fire=function(t){var e,n,s,i;if(e=this.subs[t],!e)return this;for(s=e.length,n=Array.prototype.slice.call(arguments,1),i=0;s>i;i+=1)e[i].apply(this,n)},function(){var t=/\$\{\s*([a-zA-Z0-9_$\[\]\.]+)\s*\}/g;o=function(n,s,i){var o,r,a,h;return i=i||"",r=[],o=n.replace(t,function(t,e){return-1===r.indexOf(e)&&(r[r.length]=i+e),'m.get("'+i+e+'")'}),a=Function("utils","var m=this;return "+o),h=a.bind?a.bind(s,e.utils):function(){return a.call(s,e.utils)},{compiled:h,dependsOn:r,cache:!!r.length}}}(),a=function(t,e){return null===t&&null===e?!0:"object"==typeof t||"object"==typeof e?!1:t===e},u=function(t){return D[t]||(D[t]=t.replace(/\[\s*([0-9]+)\s*\]/g,".$1"))},c=function(t,e){var n;for(n in e)e.hasOwnProperty(n)&&(t[n]=e[n])},c(j,s),c(P,s),e.prototype=j,n.prototype=P,e.utils=r,e.extend=i,e.VERSION=x,"undefined"!=typeof module&&module.exports?module.exports=e:"function"==typeof define&&define.amd?define(function(){return e}):t.Statesman=e})(this);