-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathknockout-deferred-updates.min.js
25 lines (25 loc) · 9.2 KB
/
knockout-deferred-updates.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Deferred Updates plugin for Knockout http://knockoutjs.com/
(c) Michael Best, Steven Sanderson
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Version 3.3.1
*/
(function(b){"function"===typeof require&&"object"===typeof exports&&"object"===typeof module?module.exports=b(require("knockout")):"function"===typeof define&&define.amd?define(["knockout"],b):b(ko)})(function(b){function h(a,A){for(var c in a)if(a.hasOwnProperty(c)&&a[c]&&0<=a[c].toString().indexOf(A))return c}function m(a,A){for(var c in a)if(a.hasOwnProperty(c)&&a[c]===A)return c}function ja(a,A,c,b){var e=a.subscribe(A,null,"dirty",!1,b),f=a.subscribe(c,null,"change",!1,b);return{dispose:function(){e.dispose();
f.dispose()},_target:a}}var F="object"===typeof global&&global?global:window;if(!b)throw Error("Deferred Updates requires Knockout");if("3.4.0"<=b.version)throw Error("This version of Deferred Updates supports Knockout version 3.3 and lower.");b.tasks=function(){function a(a){var d=0,c=0;l=l._next={_mark:!0};try{for(var b=a;b=b._next;)if(h=b,b._mark){if(b._next){if(5E3<=++c)throw Error("'Too much recursion' after processing "+d+" tasks.");l=l._next={_mark:!0}}}else b._done||(b._done=!0,b._func.apply(b.object,
b.args||[]),++d)}finally{if(a!==B)a._next=null,l=a;else{g=[];B._next=null;p=l=B;if(f)F[e](f);f=void 0}h=void 0}return d}function A(){if(!h)return a(B)}var c,e;F.setImmediate?(c="setImmediate",e="clearImmediate"):(c="setTimeout",e="clearTimeout");var f,B={},l=B,g=[],h,p=B,m={processImmediate:function(c,d,b){g.push(p);p=l;try{return c.apply(d,b||[])}finally{try{p._next&&a(p)}finally{p=g.pop()||B}}},processDelayed:function(a,d,b){2==arguments.length&&"object"==typeof d&&(b=d,d=b.distinct);var e;if(e=
d||void 0===d)a:{e=h||p;for(var k;k=e._next;e=k)if(k._func===a&&!k._done){e._next=k._next;e._next||(l=e);e=!0;break a}e=!1}k=b||{};k._func=a;l=l._next=k;g.length||f||(f=F[c](A));return!e},makeProcessedCallback:function(a){return function(){return m.processImmediate(a,this,arguments)}}};b.processDeferredBindingUpdatesForNode=b.processAllDeferredBindingUpdates=function(){for(var a=B;a=a._next;)a.node&&!a._done&&(a._done=!0,a._func.call())};b.processAllDeferredUpdates=A;b.evaluateAsynchronously=function(a,
d){return setTimeout(m.makeProcessedCallback(a),d)};return m}();var x=b.utils,M=x.objectForEach||function(a,b){for(var c in a)a.hasOwnProperty(c)&&b(c,a[c])},$=x.arrayForEach,ka={__proto__:[]}instanceof Array?function(a,b){a.__proto__=b;return a}:x.extend,f=function(a,b){for(var c in a)if(a.hasOwnProperty(c)&&a[c]&&a[c][b])return a[c]}(b,"end"),la=h(f,".apply(")||"ignore",U=h(f,".push"),aa=h(f,"Only subscribable"),ma=f.isInitial?m(f,f.isInitial):"isInitial",t=b.computed,na=function(a,b){var c=[],
e;for(e in a)a.hasOwnProperty(e)&&a[e]===b&&c.push(e);return c}(b,t),N=m(t.fn,t),e=b.computed(function(){}),oa=m(e,e.peek)||"peek",pa=m(e,e.isActive)||"isActive",ba=m(e,e.getDependenciesCount),ca=m(e,!1),qa=m(e,e.dispose),da="disposeWhenNodeIsRemoved",ea="disposeWhen";if("hasWriteFunction"!=ca){var e=t.toString(),C;if(C=e.match(/.\.disposeWhenNodeIsRemoved\s*\|\|\s*.\.([^|\s,]+)/))da=C[1];if(e=e.match(/.\.disposeWhen\s*\|\|\s*.\.([^|\s,]+)/))ea=e[1]}var ra=h(x,"documentElement)")||h(x,"ocument)"),
e=b.subscribable.fn;C=h(e,".bind(");var O=h(e,"notifySubscribers"),g=(new b.subscribable).subscribe(),G=g.constructor.prototype,Q=m(G,g.dispose);g.dispose();var g=null,fa,ga;O&&b.extenders.rateLimit&&(g=(new b.subscribable).extend({rateLimit:1}),g[O]=null,ga=h(g,"=!0")||"_rateLimitedChange",fa=h(g,'"beforeChange")')||"_rateLimitedBeforeChange",g=null);var y=new b.subscribable,g=m(y,1),D,V,ha;g&&(D=h(e,"return this."+g),V=h(e,"++this."+g),ha=h(e,"return this."+D+"()"));var y=null,R,ia;b.pureComputed&&
(y=b.pureComputed(function(){}),y[N]=null,R=h(y,g?'"awake"':"!1,")||"beforeSubscriptionAdd",ia=h(y,g?'"asleep"':"()||")||"afterSubscriptionRemove",y=null);var S=[],s,sa=0;f[U]=function(a){S.push(s);s=a};f.end=function(){s=S.pop()};f[aa]=function(a){if(s){if(!b.isSubscribable(a))throw Error("Only subscribable things can act as dependencies");s.callback(a,a._id||(a._id=++sa))}};b.ignoreDependencies=f[la]=function(a,b,c){try{return f[U](),a.apply(b,c||[])}finally{f.end()}};f[ba]=f.getDependenciesCount=
function(){if(s)return s.computed.getDependenciesCount()};f[ma]=f.isInitial=function(){if(s)return s.isInitial};var ta=e[C];e[C]=e.subscribe=function(a,e,c,f,g){c=c||"change";if(g)"change"==c&&(this.dependents=this.dependents||[],this.dependents.push(g));else{var h=e?a.bind(e):a;a="change"==c&&b.isObservable(this)?function(a){p.deferUpdates&&!1!==l.deferUpdates||l.deferUpdates?b.tasks.processDelayed(h,{args:[a]}):h(a)}:h}var l=ta.call(this,a,null,c);l._target=this;l.event=c;l.dependent=g;l.deferUpdates=
f;return l};var H=e.notifySubscribers,u;e.notifySubscribers=function(a,b){if("change"===b||"dirty"===b||void 0===b)if(u)u.push([this,a,b]);else try{if(u=[],H.call(this,a,b),u.length)for(var c=0,e;e=u[c];c++)Function.prototype.call.apply(H,e)}finally{u=null}else H.call(this,a,b)};e.getDependents=function(){return this.dependents?[].concat(this.dependents):[]};var ua=G[Q];G[Q]=G.dispose=function(){ua.call(this);this.dependent&&"change"==this.event&&x.arrayRemoveItem(this._target.dependents,this.dependent)};
var p=function(a,e,c){function g(a){return a[N]===p?ja(a,y,t,d):a.subscribe(t,null,"change",!1,d)}function h(a,b,e){if(H&&b===d)throw Error("A 'pure' computed must not be called recursively");q[a]=e;e._order=v++;e._version=D&&b[D]()}function m(){if(!D)return!0;var a,b;for(a in q)if(q.hasOwnProperty(a)&&(b=q[a],b._target[ha](b._version)))return!0}function l(){!r&&q&&M(q,function(a,b){b.dispose&&b.dispose()});q=null;v=0;J=!0;r=w=n=!1}function s(a,e){var c="dirty"==e,f=c&&!w&&!n;c?w=!0:n=!0;(c=d.throttleEvaluation)&&
0<=c?(clearTimeout(W),W=b.evaluateAsynchronously(u,c)):d._evalRateLimited?d._evalRateLimited():p.deferUpdates&&!1!==d.deferUpdates||d.deferUpdates?f=b.tasks.processDelayed(u,{node:E}):n&&(u(),f=!1);f&&d.notifySubscribers&&(d.notifySubscribers(k,"dirty"),!w&&c&&clearTimeout(W))}function t(a){w||n?n=!0:s(a,"change")}function y(a){s(a,"dirty")}function u(){I(!0)}function I(a){if(!P)if(!n&&a)w=n,d.equalityComparer&&d.equalityComparer(k,K)||(K=k,d.notifySubscribers(k,void 0));else{var b;a:if(J)b=!0;else{if(X&&
X()){if(!F){L();n=!1;b=!0;break a}}else F=!1;b=void 0}if(!b){P=!0;try{var c=q,l=v;b=H?void 0:!v;f[U]({callback:function(a,b){J||(l&&c[b]?(h(b,a,c[b]),delete c[b],--l):q[b]||h(b,a,r?{_target:a}:g(a)))},computed:d,isInitial:b});q={};v=0;try{var m=e?z.call(e):z()}finally{f.end(),l&&!r&&M(c,function(a,b){b.dispose&&b.dispose()}),w=n=!1}if(!d.equalityComparer||!d.equalityComparer(k,m))if(r||d.notifySubscribers(k,"beforeChange"),k=m,d._latestValue=k,r){if(V)d[V]()}else a&&(K=k,d.notifySubscribers(k,void 0));
b&&(K=k,d.notifySubscribers(k,"awake"))}finally{P=!1}v||L()}}}function d(){if(0<arguments.length){if("function"===typeof Y){var a=d.deferUpdates;d.deferUpdates=!1;try{Y.apply(e,arguments)}finally{d.deferUpdates=a}}else throw Error('Cannot write a value to a ko.computed unless you specify a "write" option. If you wish to read the current value, don\'t pass any parameters.');return this}f[aa](d);(n||w||r&&m())&&I();return k}function C(){(n&&!v||r&&m())&&I();return k}function G(){return n||w||0<v}var k,
K,w=!1,n=!0,P=!1,F=!1,J=!1,z=a,H=!1,r=!1;z&&"object"==typeof z?(c=z,z=c.read):(c=c||{},z||(z=c.read));if("function"!=typeof z)throw Error("Pass a function that returns the value of the ko.computed");var T,Y=c.write,E=c[da]||c.disposeWhenNodeIsRemoved||null,Z=c[ea]||c.disposeWhen,X=Z,L=l,q={},v=0,Q=[],W=null;e||(e=c.owner);b.subscribable.call(d);ka(d,p.fn);d[oa]=d.peek=C;d[ba]=d.getDependenciesCount=function(){return v};d[ca]=d.hasWriteFunction="function"===typeof Y;d[qa]=d.dispose=function(){L()};
d[pa]=d.isActive=G;d.activeWhen=function(a){T||(T=b.computed(function(){P=!a();!P&&n&&s(void 0,"change")}),T.deferUpdates=!1,Q.push(T))};d.getDependencies=function(){var a=[];M(q,function(b,c){a.push(c._target)});return a};if(O){var S=d[O];d[O]=function(a){S.call(d,a);d._evalRateLimited=function(){w=n=!1;d[fa](k);n=!0;d[ga](d)}}}R&&(c.pure?(r=H=!0,d[R]=function(a){if(!J&&r&&"change"==a){r=!1;if(n||m())q=null,v=0,n=!0,I();else{var b=[];M(q,function(a,c){b[c._order]=a});$(b,function(a,b){var c=q[a],
d=g(c._target);d._order=b;d._version=c._version;q[a]=d})}J||(d.notifySubscribers(k,"awake"),K=k)}},d[ia]=function(a){J||a&&"change"!=a||d.getSubscriptionsCount("change")||(M(q,function(a,b){b.dispose&&(q[a]={_target:b._target,_order:b._order,_version:b._version},b.dispose())}),r=!0,d.notifySubscribers(void 0,"asleep"),K=void 0)},D&&(d._originalGetVersion=d[D],d[D]=function(){r&&(n||m())&&I();return d._originalGetVersion()})):c.deferEvaluation&&(d[R]=function(a){"change"!=a&&"beforeChange"!=a||C()}));
E&&(F=!0,E.nodeType&&(X=function(){return!x[ra](E)||Z&&Z()}));r||c.deferEvaluation||I();E&&G()&&E.nodeType&&(L=function(){x.domNodeDisposal.removeDisposeCallback(E,L);l()},x.domNodeDisposal.addDisposeCallback(E,L));return d};p[N]=t[N];p.fn=t.fn;p.fn[N]=p;p.deferUpdates=!0;$(na,function(a){b[a]=p});t=e=null;b.extenders.throttle=function(a,e){if(b.isWriteableObservable(a)){var c=null;return b.computed({read:a,write:function(f){clearTimeout(c);c=b.evaluateAsynchronously(function(){a(f)},e)}})}a.throttleEvaluation=
e;return a};b.extenders.deferred=function(a,b){a.deferUpdates=b};return b});