-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskq.min.js
1 lines (1 loc) · 9.36 KB
/
taskq.min.js
1
!function(t,e){if("object"!=typeof t.window&&"object"!=typeof t.document){var n=function(){},s={};t.window={requestAnimationFrame:n,addEventListener:n,document:{getElementById:n,querySelector:n,querySelectorAll:n,elementFromPoint:n,head:s,body:s}}}"function"==typeof define&&define.amd?define(e):"object"==typeof exports?module.exports=e(t.window,t.window.document):e(t,t.document)}(this,function(t,e){var n=Math.random(),s=e.querySelector("script[src*='taskq.js']"),r=s&&s.getAttribute("global-name")||"taskq",i=s&&+s.getAttribute("data-min-pause")||0,a=s&&+s.getAttribute("data-backsteps")||3,o=s&&+s.getAttribute("data-step-limit")||100,u=t[r]=new function(){var e=[],s={},r=[],a=[],o=Math.random(),u=!1,c=!1;Object.defineProperties(this,{paused:{configurable:!1,enumerable:!1,get:function(){return u},set:function(t){return"object"!=typeof t||t.hash!==o?(console.log("You cannot set this manually"),!1):"boolean"==typeof t.value&&(u=t.value,!0)}},pause:{configurable:!1,enumerable:!1,get:function(){var t=!u;return this.paused={hash:o,value:!0},t}},resume:{configurable:!1,enumerable:!1,get:function(){var t=u;return this.paused={hash:o,value:!1},t}},running:{configurable:!1,enumerable:!1,get:function(){return c},set:function(t){return"object"!=typeof t||t.hash!==n?(console.log("You cannot set this manually"),!1):"boolean"==typeof t.value&&(c=t.value,!0)}}}),this.scriptLoading=!1,this.scriptLoaded=!1,this.scriptComplete=!0,this.flush=function(t){return"main"===t?(e=[],s={}):"script"===t&&(r=[]),this},this.export=function(t,e){return s[e=e||"default"]=t,this},this.push=function(t){return this.scriptLoading||this.scriptLoaded?r.push(t):e.push(t),this},this.perform=function(){return this.execute(this.sortTasks(e,!0),s,{origin:"main"}),this},this.promise=function(){return this.promise.promise?this.promise.promise:t.Promise&&Promise.constructor===Function?this.promise.promise=Promise.resolve():this.promise.promise=new this.__promise},this.minPause=i,this.thenable=function(e){var i=[],o=this,c=void 0,d=!1,h=!1,p=function(){};this.errored=!1,this.rejected=!1,this.catch=function(t){return"function"==typeof t&&(p=t),this},this.target=e,this.status={complete:!1},this.counter=0,this.next=void 0,this.resolver=Object.defineProperties(function(t){return!d&&((c=!!t)?(o.counter--,o.next=!0):o.rejected=!0,d=!0)},{init:{configurable:!1,enumerable:!1,get:function(){return h=!0}},value:{configurable:!1,enumerable:!1,get:function(){return c}}}),this.then=function(t){return this.counter++,i.push(function(){e.promise().then(function(){t(o.resolver),h||(o.counter--,o.next=!0)})}),this},this.execute=function(){return e.execute(r.length&&e.sortTasks(r),s,{origin:"script",status:o.status,flush:!0}),this},this.impender=function(){if(e.running||(e.running={hash:n,value:!0}),!u&&(o.errored||o.rejected||!o.counter&&o.status.complete))return e.running={hash:n,value:!1},e.scriptLoaded=!1,e.scriptComplete=!0,o.rejected&&p(),void(a.length&&a.shift()());u||!o.status.complete||void 0!==o.next&&!o.next||(h=!1,d=!1,c=void 0,o.next=!1,i.shift()()),t.requestAnimationFrame(o.impender)},this.impender()},this.queuePacker=function(t,e){var n=[],s=this;return a.push(function(){n.forEach(function(t,e){this.then(t)},s.load(t,e))}),new function(){this.then=function(t){return n.push(t),this}}}},c=u.constructor.prototype;return c.emptyArr=[],c.version=function(){return"2.3.7"},c.sortTasksUnstable=function(t,e,n,s,r){return t.map(function(t,e){return[t,t._taskqId,t._taskqWaitFor]}).sort(function(t,i){var a=t[1],o=i[1],u=t[2]||c.emptyArr,d=i[2]||c.emptyArr,h=t[0],p=i[0],f=void 0===h._taskqKSC?h._taskqKSC=!~n.indexOf(a):h._taskqKSC,l=void 0===h._taskqKEC?h._taskqKEC=!!~s.indexOf(a):h._taskqKEC,m=void 0===p._taskqKSC?p._taskqKSC=!~n.indexOf(o):p._taskqKSC,q=void 0===p._taskqKEC?p._taskqKEC=!!~s.indexOf(o):p._taskqKEC,k=f&&(l||u.some(function(t,e){return t===o||r[t]&&r[t]._taskqWaitFor&&~r[t]._taskqWaitFor.indexOf(o)})),_=m&&(q||d.some(function(t,e){return t===a||r[t]&&r[t]._taskqWaitFor&&~r[t]._taskqWaitFor.indexOf(a)}));return k*e+u.length-_*e-d.length}).map(function(t,e){return t[0]})},c.sortTasks=function(t,e){var n=Date.now(),s=1,r=t.length,i=Math.max.apply(null,t.map(function(t,e){return(t._taskqWaitFor||c.emptyArr).length}))+1,u=["start","init","begin","loadstart","loadStart"],d=["end","defer","finish","loadend","loadEnd"],h=t.reduce(function(t,e,n,s){return e._taskqId&&(t[e._taskqId]=e),t},{});t:for(var p,f,l=0,m=(t=this.sortTasksUnstable(t,i,u,d,h))[l],q=m._taskqId,k=m._taskqWaitFor||c.emptyArr;l<r;++l,m=t[l],q=m&&m._taskqId,k=m&&(m._taskqWaitFor||c.emptyArr)){if(s>o){console.log("Max step limit of "+o+" step(s) has been reached. Terminating sort.");break t}p=void 0===m._taskqKSC?m._taskqKSC=!~u.indexOf(q):m._taskqKSC,f=void 0===m._taskqKEC?m._taskqKEC=!!~d.indexOf(q):m._taskqKEC;e:for(var _=l+1;_<r;++_){var g=t[_],v=g._taskqId,b=g._taskqWaitFor||c.emptyArr,y=void 0===g._taskqKSC?g._taskqKSC=!~u.indexOf(v):g._taskqKSC,C=void 0===g._taskqKEC?g._taskqKEC=!!~d.indexOf(v):g._taskqKEC,x=p&&(f||k.some(function(t,e){return t===v||h[t]&&h[t]._taskqWaitFor&&~h[t]._taskqWaitFor.indexOf(v)})),O=y&&(C||b.some(function(t,e){return t===q||h[t]&&h[t]._taskqWaitFor&&~h[t]._taskqWaitFor.indexOf(q)}));if(!this.areCircular(m,g,h)&&x*i+this.dependencyIsSubset(m,g,h)*k.length-O*i-b.length>0){t[l]=g,t[_]=m;var S=Math.max(0,_-8),D=Math.min(r,Math.max(0,_-8)+9);t=t.slice(0,S).concat(this.sortTasksUnstable(t.slice(S,D),i,u,d,h)).concat(t.slice(D)),++s,l=Math.max(0,l-=a);break e}}}return e&&console.log("Semi stable sorting done in: "+s+" steps, ~"+(Date.now()-n)+"ms"),this.clearDependencyLedger(t),t},c.__promise=function(){this.then=function(e){return t.requestAnimationFrame(e),this}},c.execute=function(e,s,r){if(this.running||(this.running={hash:n,value:!0}),!e.length)return(void 0===r.flush||r.flush)&&this.flush(r.origin),r.status&&(r.status.complete=!0),void(this.running={hash:n,value:!1});var i=this,a=this.promise(),o=Date.now();a=a.then(function(n){t.requestAnimationFrame(function(){var n=e.shift(),a=0;if("function"==typeof n){var u=/function\s*\w*\s*\(((?:\s*\w+\s*\,?\s*)*)\)\s*\{/.exec(n.toString());n.apply(s[n._taskqScope]||t,u?u[1].replace(/\s+/g,"").split(",").map(function(t,e){return s[t]}):void 0)}else console.log("not a function ref");!i.paused&&(a=Date.now()-o)>=i.minPause&&("main"===r.origin&&i.scriptComplete||"script"===r.origin)?i.execute(e,s,r):i.wait(i.minPause-a,e,s,r)})})},c.wait=function(e,n,s,r){var i=this,a=0,o=function(u){a=a||u,!i.paused&&e+a-u<=0&&("main"===r.origin&&i.scriptComplete||"script"===r.origin)?i.execute(n,s,r):t.requestAnimationFrame(o)};t.requestAnimationFrame(o)},c.load=function(t,n){if(!this.scriptComplete)return this.queuePacker(t,n);n=n||e.head;var s=this,r=n.querySelector("script[src*='"+t.replace(/\?.*$/gi,"")+"']"),i=e.createElement("script"),a=new this.thenable(s);return this.scriptLoading=!0,this.scriptComplete=!1,i.async=!0,i.onload=function(){s.scriptLoading=!1,s.scriptLoaded=!0,a.execute()},i.onerror=function(){a.errored=!0},i.src=t,r?n.replaceChild(i,r):n.appendChild(i),a},c.dependencyIsSubset=function(t,e,n,s){if(t._taskqDependencyIsSubset=t._taskqDependencyIsSubset||[],t._taskqDependencyIsNotSubset=t._taskqDependencyIsNotSubset||[],~t._taskqDependencyIsSubset.indexOf(e))return!0;if(~t._taskqDependencyIsNotSubset.indexOf(e))return!1;s=s||[];var r=t._taskqWaitFor||c.emptyArr,i=e._taskqWaitFor||c.emptyArr;if(r.length){if(i.length){if(~s.indexOf(e))return;if(r.map(function(t,e){return i.indexOf(t)}).some(function(t,e){return!~t})){var a=i.map(function(r,i){return n[r]&&!this.areCircular(e,n[r],n)&&this.dependencyIsSubset(t,n[r],n,s)},this).some(function(t,e){return!0===t});return a?t._taskqDependencyIsSubset.push(e):t._taskqDependencyIsNotSubset.push(e),a}return t._taskqDependencyIsSubset.push(e),s.push(e),!0}return t._taskqDependencyIsNotSubset.push(e),s.push(e),!1}return t._taskqDependencyIsNotSubset.push(e),!1},c.dependsOn=function(t,e,n,s){if(t._taskqDependsOn=t._taskqDependsOn||[],t._taskqNotDependsOn=t._taskqNotDependsOn||[],~t._taskqDependsOn.indexOf(e))return!0;if(~t._taskqNotDependsOn.indexOf(e))return!1;var r=t._taskqWaitFor||c.emptyArr;if(void 0===s&&(s=[])&&!r.length)return t._taskqNotDependsOn.push(e),!1;if(!~s.indexOf(e)){if(~r.map(function(t,e){return n[t]}).indexOf(e))return t._taskqDependsOn.push(e),s.push(e),!0;var i=r.map(function(r,i){return n[r]&&!this.areCircular(t,n[r],n)&&this.dependsOn(n[r],e,n,s)},this).some(function(t,e){return!0===t});return i?t._taskqDependsOn.push(e):t._taskqNotDependsOn.push(e),i}},c.areCircular=function(t,e,n){if(t._taskqCircular=t._taskqCircular||[],t._taskqNotCircular=t._taskqNotCircular||[],e._taskqCircular=e._taskqCircular||[],e._taskqNotCircular=e._taskqNotCircular||[],~t._taskqCircular.indexOf(e))return!0;if(~t._taskqNotCircular.indexOf(e))return!1;var s=this.dependsOn(t,e,n)&&this.dependsOn(e,t,n);return s?(t._taskqCircular.push(e),e._taskqCircular.push(t),console.log("Circular dependency detected, consider revising "+(t._taskqId||t.toString().slice(0,30))+" and "+(e._taskqId||e.toString().slice(0,30)))):(t._taskqNotCircular.push(e),e._taskqNotCircular.push(t)),s},c.clearDependencyLedger=function(t){var e=this;return t.forEach(function(t,n){e.deleteEnum(t,this)},["_taskqDependencyIsSubset","_taskqDependencyIsNotSubset","_taskqDependsOn","_taskqNotDependsOn","_taskqCircular","_taskqNotCircular"]),this},c.deleteEnum=function(t,e){return t&&"object"==typeof t&&e instanceof Array&&e.forEach(function(e,n){delete t[e]}),this},t.addEventListener("load",function(){u.perform()},!1),u});