text: !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self)["async-multiple"]={})}(this,function(t){"use strict";function a(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var e=function(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t};function n(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function o(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)}var u,s,h=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},c=(e(l,[{key:"whatType",value:function(t){return Object.prototype.toString.call(t)}},{key:"shuffle",value:function(t){return t.map(function(t){return{v:t,r:Math.random()}}).sort(function(t,e){return t.r-e.r}).map(function(t){return t.v})}},{key:"assign",value:function(){return Object.assign.apply(Object,arguments)}},{key:"isType",value:function(){for(var e=this,t=arguments.length,r=Array(t),n=0;n<t;n++)r[n]=arguments[n];return 0===r.map(function(t){return e.whatType(t)===e.whatType(r[0])}).filter(function(t){return!t}).length}},{key:"isPromise",value:function(t){return"[object Promise]"===this.whatType(t)||!(!t.then||!this.isType(t,t.then,function(){}))}},{key:"makePromise",value:(u=regeneratorRuntime.mark(function t(e){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(this.isPromise(e))return t.next=3,e;t.next=7;break;case 3:if(void 0!==t.sent)return t.abrupt("return",e);t.next=6;break;case 6:return t.abrupt("return",new Promise(function(){}));case 7:if(e instanceof Error)return t.abrupt("return",Promise.reject(e));t.next=9;break;case 9:return t.abrupt("return",Promise.resolve(e));case 10:case"end":return t.stop()}},t,this)}),s=function(){var s=u.apply(this,arguments);return new Promise(function(i,o){return function e(t,r){try{var n=s[t](r),a=n.value}catch(t){return void o(t)}if(!n.done)return Promise.resolve(a).then(function(t){e("next",t)},function(t){e("throw",t)});i(a)}("next")})},function(t){return s.apply(this,arguments)})},{key:"arrayToObject",value:function(t){if(!this.isType(t,[]))throw Error("[arrayToObject error]: expect a array!");var r={};return t.map(function(t,e){r[e]=t}),r}},{key:"objectToArray",value:function(t){if(!this.isType(t,{}))throw Error("[objectToArray error]: expect a object!");var e=[];for(var r in t)e.push(t[r]);return t}},{key:"sleep",value:function(r){return new Promise(function(t,e){setTimeout(function(){return t("[Sleep]: "+r+"ms")},r)})}},{key:"removeUndefined",value:function(t){if(!this.isType(t,{}))throw Error("[removeUndefined message]: obj expect a "+this.whatType({}));for(var e in t)this.isType(t[e],void 0)&&delete t[e];return t}},{key:"randomString",value:function(t){for(var e,r=0<arguments.length&&void 0!==t?t:8,n="";n.length<r;)n+=(e=Math.floor(62*Math.random()))<10?e:String.fromCharCode(e<36?55+e:61+e);return n}},{key:"randomNumber",value:function(t,e,r){var n=arguments.length<=2||void 0===r||r;if(!this.isType(t,e,1))throw Error("[randomNumber message]: obj expect a "+this.whatType(1));if(e<t)throw Error("[randomNumber message]: start number big than end!");var a=Math.random()*(e-t)+t;return n?parseInt(a):a}}]),l);function l(){a(this,l)}var f=(r(p,c),e(p,[{key:"on",value:function(t,e,r){this.isType(t,"")||this._errorManage("name expected a string!"),this.isType(e,function(){})||this._errorManage("callback expected a function!"),this.isType(r,void 0)||this.isType(r,1)||0<r||this._errorManage("times expected a number(min 1)!"),this._debug("Add listener for "+t),this._callbacks[t]=this._callbacks[t]||[],this._callbacks[t].push({fn:e,times:r})}},{key:"emit",value:function(a){for(var i=this,t=arguments.length,o=Array(1<t?t-1:0),e=1;e<t;e++)o[e-1]=arguments[e];this.isType(a,"")||this._errorManage("name expected a string!"),this._callbacks[a]&&this._callbacks[a].map(function(t,e){if(i.isType(t,{})){var r=t.fn,n=t.times;r.apply(void 0,o),i.isType(n,void 0)||i._callbacks[a].splice(e,1,1<n?{fn:r,times:n-1}:void 0)}})}},{key:"remove",value:function(t){this.isType(t,"")||this._errorManage("name expected a string!"),this._callbacks[t]||this._errorManage("action named "+t+" wasn't bind or removed!"),delete this._callbacks[t]}},{key:"once",value:function(t,e){return this.on(t,1<arguments.length?e:void 0,1)}},{key:"_debug",value:function(t){this._isDebug&&t&&(this.isType(t,"")||(t=""+t),console.log("[event MESSAGE]: "+t))}},{key:"_errorManage",value:function(t){var e=0<arguments.length&&void 0!==t?t:"unexpected error!";throw this.isType(e,"")||(e=""+e),Error("[event ERROR]: "+e)}}]),p);function p(){var t=(0<arguments.length&&void 0!==arguments[0]?arguments[0]:{}).debug,e=void 0!==t&&t;a(this,p);var r=i(this,(p.__proto__||Object.getPrototypeOf(p)).call(this));return r._callbacks={},r._isDebug=e,r}var y,v,d=(r(m,c),e(m,[{key:"_errorManage",value:function(t){var e=0<arguments.length&&void 0!==t?t:"unexpected error!";return this.isType(e,"")||(e=""+e),Error("[async-multiple ERROR]: "+e)}},{key:"_debug",value:function(t){t&&(this.isType(t,"")||(t=""+t),console.log("[async-multiple MESSAGE]: "+t))}}]),m);function m(t){a(this,m);var e=i(this,(m.__proto__||Object.getPrototypeOf(m)).call(this,t));return e.event=new f,e}v={get:function(t,e){var r,n,a,i;for(i=t,r=0,a=(n=e.split(".")).length;r<a;r++)i=i[n[r]];return i},set:function(t,e,r){var n,a,i,o,s,u,c;for(n=(o=e.split(".")).slice(0,-1),s=o[o.length-1],c=t,a=0,u=n.length;a<u;a++)c=null!=c[i=n[a]]?c[i]:c[i]={};return c[s]=r}},y=function(e,t){var r;return r="function"==typeof e?function(t){return e(t)}:function(t){return v.get(t,e)},t?function(t,e){return r(t)>r(e)?-1:r(t)<r(e)?1:0}:function(t,e){return r(t)<r(e)?-1:r(t)>r(e)?1:0}};var g="PENDING",k="UNCALL",_="CALLING",b="CALLED",T=(r(w,d),e(w,[{key:"start",value:function(){var a=this;return this.taskStatus=_,this._beginTask().then(function(t){var e=function(t,u){return t.sort(function(t,e){var r,n,a,i,o,s;for(n=0,a=u.length;n<a;n++)if("object"==typeof(r=u[n])?(i=r.predicate,o=r.reverse):i=r,0!==(s=y(i,o)(t,e)))return s})}(t,["order"]),r={},n=e.map(function(t){return r["map"===a._formatType?t.input:t.order]=t.output,t.output});return Promise.resolve({response:t,allObject:r,allArray:e,outputArray:n,toObject:function(){return r},toArray:function(){return e}})})}},{key:"cancelTask",value:function(){var r=this;return this._debug("Calling the cancelTask method, task will stop at next trick!"),this._stopAtNextTrick=!0,new Promise(function(t,e){r.event.on("TASKSTOPED",t())})}},{key:"_assign",value:function(a){for(var i=this,t=arguments.length,e=Array(1<t?t-1:0),r=1;r<t;r++)e[r-1]=arguments[r];return e=e.map(function(t){if(i.isType(t,{})){var e={};for(var r in t){var n=t[r];i.isType(n,{})?a.hasOwnProperty(r)||(e[r]=i.isType(n.default,function(){})?n.default():n.default):e[r]=n}return e}}),this.assign.apply(this,[a].concat(o(e)))}},{key:"_checkParams",value:function(t){var n=this;if(this._assign(this,this._defaultParams),this.isType(t[0],{}))this.assign(this,t[0]);else if(this.isType(t[0],[])&&(this.task=t[0],this.rejectOnError=t[1]||!1,t[2])){if(!this.isType(t[2],{}))throw this._errorManage("option expect to a object, please check!");this.assign(this,t[2])}function e(e){var t=n._defaultParams[e];if(n.isType(t,void 0))return"continue";if(n.isType(t,{})){if(!t.hasOwnProperty("type")||n.isType(n[e],void 0))return"continue";if(t.type=n.isType(t.type,[])?t.type:[t.type],0===t.type.filter(function(t){return t===n[e].constructor}).length){var r=t.type.map(function(t){return t.name});throw n._errorManage(e+" expect to a "+r+" but get a "+n.whatType(n[e])+", please check!")}}else if(!n.isType(t,n[e]))throw n._errorManage(e+" expect to a "+n.whatType(t)+" but get a "+n.whatType(n[e])+", please check!")}for(var r in this._defaultParams)e(r)}},{key:"_initTask",value:function(){var t=0,e=[];for(var r in this.task)e.push({order:t,stepKey:r,handle:this.task[r],status:k}),t++;this.task=this.randomStep?this.shuffle(e):e}},{key:"_initHooks",value:function(){var a=this;if(this.showProgress){var n=this.handleEnd;this.handleEnd=function(){for(var t=arguments.length,e=Array(t),r=0;r<t;r++)e[r]=arguments[r];n.apply(a,e),function(t){var e=t.all,r=t.progress,n=a.task.filter(function(t){return t.status===b}).length;console.log("[ "+a.alias+" ] all: "+e+" complete: "+(n+1)+" progress: "+r)}.apply(a,e)}}}},{key:"_getStepBettwen",value:function(){return this.isType(this.stepBettwen,[])?this.randomNumber.apply(this,o(this.stepBettwen.slice(0,2))):this.stepBettwen}},{key:"_errorManage",value:function(t){var e=0<arguments.length&&void 0!==t?t:"unexpected error!";return this.isType(e,"")||(e=""+e),Error("[async-multiple ERROR]: "+e)}},{key:"_makeError",value:function(t){return t instanceof Error?t:Error(t instanceof String?t:""+t)}},{key:"_createQueue",value:function(){var r=this,t=this.task.filter(function(t){return t.status===k}),e=this.task.filter(function(t){return t.status===_}),n=this.task.map(function(t){return t.order}),a=Math.min(this.maxCall-e.length,t.length);a<1||t.slice(0,a).map(function(t){var e=n.indexOf(t.order);r.task[e].status=_,r.sleep(r._getStepBettwen()).then(function(){r._callTaskHandle(e)})})}},{key:"_beginTask",value:function(){var o=this,s=0,u=[];return 0===this.task.length?Promise.resolve([]):(this._createQueue(),new Promise(function(a,i){o.event.on(o._actionName,function(t){var e=t.error,r=t.output,n=t.step;if(o.taskStatus!==b){if(o._stopAtNextTrick)return o.taskStatus=b,a(u),void o.event.emit("TASKSTOPED");if(o.maxSuccessCount&&u.filter(function(t){return!t.error}).length>=o.maxSuccessCount)return o._debug("Enough success result, task will stop!"),o.taskStatus=b,void a(u);if(e){if(s<o.errorRetry)return s+=1,o.task[n].status=k,void o._createQueue();if(e=o._makeError(e),o.rejectOnError)return i(e)}s=0,o.task[n].status=b,u.push(o.removeUndefined(h({},o.task[n],{output:r,error:e,handle:void 0,status:void 0,stepKey:void 0}))),o.task.filter(function(t){return t.status===b}).length!==o.task.length?o._createQueue():(o.taskStatus=b,a(u))}})}))}},{key:"_debug",value:function(){if(this.debug){for(var t,e=arguments.length,r=Array(e),n=0;n<e;n++)r[n]=arguments[n];(t=function t(e,r,n){null===e&&(e=Function.prototype);var a=Object.getOwnPropertyDescriptor(e,r);if(void 0===a){var i=Object.getPrototypeOf(e);return null===i?void 0:t(i,r,n)}if("value"in a)return a.value;var o=a.get;return void 0!==o?o.call(n):void 0}(w.prototype.__proto__||Object.getPrototypeOf(w.prototype),"_debug",this)).call.apply(t,[this].concat(r))}}},{key:"_callTaskHandle",value:function(t,e){var r=this,n=0<arguments.length&&void 0!==t?t:0,a=1<arguments.length&&void 0!==e&&e;if(this.task.length-1<n||n<0)return Promise.reject(this._errorManage("step overflow!"));function i(t){return(100*t/u).toFixed(2)+"%"}var o=g,s=null,u=this.task.length,c=this.task.filter(function(t){return t.status===b}).length;function l(t){if(r.taskStatus!==b&&o===g){o="REJECT",s&&clearInterval(s);var e=h({},r.task[n],{all:u,progress:i(c+1),output:r._getTaskOutput,error:t,step:n,isRetry:a});r.handleEnd&&r.handleEnd(e),r.event.emit(r._actionName,e)}}this.handleStart&&this.handleStart(h({step:n,all:u,progress:i(c)},this.task[n],{isRetry:a,cancelTask:this.cancelTask.bind(this)})),this.isType(this.stepTimeout,void 0)||(s=setTimeout(function(){l(r._makeError("timeout!"))},this.stepTimeout));var f=this.task[n].handle(this.task[n].input,n,this.cancelTask.bind(this),this.task[n].stepKey);void 0!==f&&this.makePromise(f).then(function(t){if(r.taskStatus!==b&&o===g){o="RESOLVE",s&&clearInterval(s);var e=h({},r.task[n],{all:u,progress:i(c+1),output:t,error:null,step:n,isRetry:a,cancelTask:r.cancelTask.bind(r)});r.handleEnd&&r.handleEnd(e),r.event.emit(r._actionName,e)}},l)}},{key:"_getTaskOutput",get:function(){return this.isType(this.errorReplace,void 0)?null:this.errorReplace}}]),w);function w(){a(this,w);var t=i(this,(w.__proto__||Object.getPrototypeOf(w)).call(this));t._defaultParams={task:{type:[Array,Object],default:[]},rejectOnError:{type:Boolean,default:!1},handleStart:function(){},handleEnd:function(){},randomStep:{type:Boolean,default:!1},stepBettwen:{type:[Array,Number],default:0,min:0},stepTimeout:{type:Number,default:void 0,min:0},errorReplace:void 0,errorRetry:{type:Number,default:0,min:0},maxCall:{type:Number,default:1,min:0},maxSuccessCount:{type:Number,default:void 0},showProgress:{type:Boolean,default:!1},alias:{type:String,default:"TASK_"+t.randomString(8)},debug:{type:Boolean,default:!1}},t._formatType="each",t.taskStatus=k,t._stopAtNextTrick=!1,t._actionName=t.randomString(8);for(var e=arguments.length,r=Array(e),n=0;n<e;n++)r[n]=arguments[n];return t._checkParams(r),t._initTask(),t._initHooks(),t}var O=(r(E,T),e(E,[{key:"_initTask",value:function(){var t=this,e=0,r=[];for(var n in this.task)r.push({order:e,input:this.task[n],stepKey:n,handle:function(){return t.handle.apply(t,arguments)},status:k}),e++;this.task=this.randomStep?this.shuffle(r):r}}]),E);function E(t){a(this,E);var e=i(this,(E.__proto__||Object.getPrototypeOf(E)).call(this,t));return e._formatType="map",e}t.Each=T,t.Event=f,t.Map=O,t.each=function(){for(var t=arguments.length,e=Array(t),r=0;r<t;r++)e[r]=arguments[r];return(new(Function.prototype.bind.apply(T,[null].concat(e)))).start()},t.map=function(){for(var t=arguments.length,e=Array(t),r=0;r<t;r++)e[r]=arguments[r];return(new(Function.prototype.bind.apply(O,[null].concat(e)))).start()},Object.defineProperty(t,"__esModule",{value:!0})});
  