-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobservable.min.js
7 lines (7 loc) · 3.44 KB
/
observable.min.js
1
2
3
4
5
6
7
(function(f,q){function n(a){return"function"===typeof a}function r(a,b,c){if(c.recording||!a)return c.value;var d=[];c.recording=!0;h.push(d);var e=f.computedContext;f.computedContext={isInitial:function(){return!c.initialised},getDependenciesCount:function(){return d.length}};try{return a.apply(b)}finally{c.initialised=!0;f.computedContext=e;h.pop();c.recording=!1;a=[];if(!c._d)for(b=0;b<d.length;b++)a.push(d[b].subscribe(function(){c.peek()}));c.setDependent(a)}}function p(a,b){h.push([]);try{a.call(b)}finally{h.pop()}}
function g(){if(!(this instanceof g))return new g;this.subscribers=function(a){var b=this._s||(this._s={});a&&(b=b[a]||(b[a]=[]));return b};this.notifySubscribers=function(a,b){var c=this.subscribers(b||"change");try{this.getter&&(a=this.getter())}finally{for(var d=c.length,e=0;e<d;e++)c[e]._p||c[e].cb.call(c[e].context,a);for(e=0;e<d;e++)c[e]._p&&(c.splice(e,1),e--,d--)}};this.subscribe=function(a,b,c){this.deferEvaluation&&this.peek&&this.peek();c=c||"change";if(!n(a))throw Error("Function expected.");
var d={cb:a,context:b};this.subscribers(c).push(d);return{dispose:function(){d._p=!0}}};this.copyProperties=function(a){for(var b in this)a[b]=this[b];a._clonedFrom=this;return a};this.extend=function(a){for(var b in a)this[b]=a[b];return this};this.getSubscriptionsCount=function(){var a=0,b=this.subscribers(),c;for(c in b)for(var d in b[c])b[c][d]._p||a++;return a}}function k(){function a(){return 0<arguments.length?(a.setter(arguments[0],!0,arguments),this):a.getter()}a.valueHasMutated=function(){a.notifySubscribers(a.value)};
a.valueWillMutate=function(){a.notifySubscribers(a.value,"beforeChange")};a.setter=function(b,c,d){var e=a.equalityComparer;if("always"===a.notify||!e||!e(b,a.value))try{p(a.valueWillMutate,a);if(c&&!a.write)throw Error("Observable not settable.");a.value=b;c&&a.write.apply(a.getContext(),d)}finally{p(a.valueHasMutated,a)}};a.equalityComparer=function(a,c){return"[object Object]"!==Object.prototype.toString.call(a)&&a===c};a.getter=function(){a:{var b=h.length,b=0<b?h[b-1]:[],c;for(c in b)if(b[c]===
a)break a;b.push(a)}a.deferEvaluation&&a.peek();return a.value};a.peek=function(){a.deferEvaluation=!1;a.disposeWhen&&a.disposeWhen()&&a.dispose();if(!a._d){var b=r(a.read,a.getContext(),a);a.setter(b,!1,[]);return b}};a.getContext=function(){return void 0===a.owner?q:a.owner};a.setDependent=function(b){var c=a._subs;if(c)for(var d=0;d<c.length;d++)c[d].dispose();a._subs=b};a.getDependenciesCount=function(){return(a._subs||[]).length};a.isActive=function(){return 0<a.getDependenciesCount()};a.dispose=
function(){a._d=!0;a.setDependent([])};this.copyProperties(a);a.original=this;return a}function l(a,b,c){if(!(this instanceof l))return new l(a,b,c);if(!n(a)&&(c=a||{},a=c.read,!n(a)))throw Error("Function expected.");var d=k.call(this);d.owner=b;d.read=a;c&&d.extend(c);d.deferEvaluation||d.peek();return d}function m(a,b){return function(c){return!c||b&&!b(c)?!1:(c._clonedFrom||c)instanceof a}}if(f){var h=[];f.subscribable=g;f.computed=f.dependentObservable=l;f.observable=function(a){var b=new k;
b.value=a;b.write=function(){};return b};g.fn=g.prototype={};f.observable.fn=k.prototype=new g;l.fn=l.prototype=(new k).original;f.isSubscribable=m(g);f.isComputed=m(l);f.isObservable=m(k);f.isWriteableObservable=m(k,function(a){return!!a.write});f.computedContext={isInitial:function(){},getDependenciesCount:function(){}}}})("undefined"!==typeof exports?exports:window.ko?null:window.ko={},"undefined"!==typeof exports?process:window);