UNPKG

sas

Version:

The Callback Hell Terminator

7 lines 2.27 kB
/*! *Version: 3.0.4 *Author: Du Wei *Repository: https://github.com/hezedu/sas *Released: MIT */ !function(){function t(t,e,i){var r;if("object"!=typeof e){switch(arguments.length){case 2:i=e;break;case 3:r=e}e={}}else r=e.iterator;new s(t,r,i,e)}function s(t,s,e,i){this.tasks=t,this.result=i.context||{},this.tasksCount=0,this.tasksCbCount=0,this.error=null,this.end=e,this.iterator=s,this.process=i.process,this.processInterval=i.processInterval||1e3,this.init()}function e(t,s,e,i,r){this.index=s,this._count=e,this._root=r,this._parents=arguments}var i=Object.prototype.toString;s.prototype.init=function(){var t=[1,0];this.dis(this.tasks,t[1],t),this._process()},s.prototype.dis=function(t,s,e,r){if(!this.error){var n;switch(i.call(t)){case"[object Function]":this.forFn(t,s,e,r);break;case"[object Object]":var o=Object.keys(t),a=o.length;n=[a,0];for(var c=0;c<a;c++){var h=o[c];this.dis(t[h],h,n,arguments)}break;case"[object Array]":n=[t.length,0],this.dis(t[0],n[1],n,arguments);break;default:this.iterator?this.forFn(this.iterator(t),s,e,r):(e[1]++,this.nextTick(t,s,e,r))}}},s.prototype.forFn=function(t,s,i,r){function n(e,n){if(a.tasksCbCount++,!a.error&&i[0]!==i[1]){if(e)switch(e){case"$reload":return n=n||t,a.dis(n,s,i,r);case"$up":i[1]=i[0];break;default:return a.error=e,void a._end()}else i[1]++;"$"===t.name[0]&&(a.result[t.name.substr(1)]=n),a.nextTick(t,s,i,r)}}this.tasksCount++;var o=null,a=this;t.length>1&&(o=new e(t,s,i,r,this.tasks)),t.call(this.result,n,o)},s.prototype.nextTick=function(t,s,e,i){e[0]===e[1]?i?(i[2][1]++,this.nextTick.apply(this,i)):this._end():"number"==typeof s&&this.dis(i[0][e[1]],e[1],e,i)},s.prototype._process=function(){if(this.process){var t=this;t._t=setInterval(function(){t.process(t.tasksCount,t.tasksCbCount)},t.processInterval)}},s.prototype._end=function(){this.process&&(clearInterval(this._t),this.process(this.tasksCount,this.tasksCbCount)),this.end&&this.end(this.error,this.result)},e.prototype.indexs=function(){for(var t=this._parents,s=[];t[3];)s.splice(0,0,t[1]),t=t[3];return s},e.prototype.upperIndex=function(t){t=t||1;for(var s=this._parents;t>0&&s[0]!==this._root;)s=s[3],t--;if(0===t)return s[1]},"function"==typeof define&&define.amd?define(function(){return t}):this.sas=t}();