/*! asynquence-contrib
    v0.1.11-a (c) Kyle Simpson
    MIT License: http://getify.mit-license.org
*/
!function(n,t){"undefined"!=typeof module&&module.exports?module.exports=t(require(n)):"function"==typeof define&&define.amd?define([n],t):t(n)}(this.ASQ||"asynquence",function(n){"use strict";var t=Array.prototype.slice,e=Object.create(null);return n.extend("all",function(n){return n.gate}),n.extend("any",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments);return r.then(function(r){function a(){var n;o===u.length&&(n=[],l?(u.forEach(function(t,e){n.push(c["s"+e])}),r.apply(e,n)):(u.forEach(function(t,e){n.push(s["s"+e])}),r.fail.apply(e,n)))}var l=!1,o=0,c={},s={},i=n.apply(e,t.call(arguments,1));u=u.map(function(u,i){return function(f){var p=t.call(arguments);p[0]=function(){l=!0,o++,c["s"+i]=arguments.length>1?n.messages.apply(e,arguments):arguments[0],a()},p[0].fail=function(){o++,s["s"+i]=arguments.length>1?n.messages.apply(e,arguments):arguments[0],a()},p[0].abort=function(){l||(f.abort(),r.abort())},u.apply(e,p)}}),i.gate.apply(e,u)}),r}}),n.extend("errfcb",function(n){return function(){var r={then:function(n){return r.then_cb=n,r},or:function(n){return r.or_cb=n,r},__ASQ__:!0,next:!0};return n.seq(r),function(n){n?r.or_cb(n):r.then_cb.apply(e,t.call(arguments,1))}}}),n.extend("first",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments);return r.then(function(r){var a=0,l={},o=!1,c=n.apply(e,t.call(arguments,1));u=u.map(function(s,i){return function(f){var p=t.call(arguments);p[0]=function(){o||(o=!0,a++,r(arguments.length>1?n.messages.apply(e,arguments):arguments[0]),c.abort())},p[0].fail=function(){var t=[];a++,l["s"+i]=arguments.length>1?n.messages.apply(e,arguments):arguments[0],o||a!==u.length||(u.forEach(function(n,e){t.push(l["s"+e])}),r.fail.apply(e,t))},p[0].abort=function(){o||(f.abort(),r.abort())},s.apply(e,p)}}),c.gate.apply(e,u)}),r}}),function(){var r;n.iterable=function(){function a(n){return"undefined"!=typeof setImmediate?setImmediate(n):setTimeout(n,0)}function u(){var n;if(h=null,q.length>0)for(;_.length>0;){n=_.shift();try{n.apply(e,q)}catch(t){checkBranding(t)?q=q.concat(t):(q.push(t),t.stack&&q.push(t.stack)),0===_.length&&console.error.apply(console,q)}}}function l(){return v||d||0===arguments.length?m:(b.push.apply(b,arguments),m)}function o(){return d||0===arguments.length?m:(_.push.apply(_,arguments),h||(h=a(u)),m)}function c(){return d||0===arguments.length?m:(t.call(arguments).forEach(function(n){l(n).or(n.fail)}),m)}function s(){if(v||d||0===b.length)return b.length>0&&i("Sequence cannot be iterated"),{done:!0};try{return{value:b.shift().apply(e,arguments)}}catch(t){return n.isMessageWrapper(t)?i.apply(e,t):t.stack?i(t,t.stack):i(t),{}}}function i(){return v||d?m:(q.push.apply(q,arguments),v=!0,h||(h=a(u)),m)}function f(){v||d||(d=!0,clearTimeout(h),h=null,b.length=0,_.length=0,q.length=0)}function p(){var t;return r={val_queue:b.slice(0),or_queue:_.slice(0)},t=n.iterable(),r=null,t}function g(n){return Object.defineProperty(n,y,{enumerable:!1,value:!0}),n}var m,h,y="__ASQ__",v=!1,d=!1,b=[],_=[],q=[];return m=g({val:l,then:l,or:o,pipe:c,next:s,"throw":i,abort:f,duplicate:p}),m["object"==typeof Symbol&&null!=Symbol&&Symbol.iterator||"@@iterator"]=function(){return m},r&&(b=r.val_queue.slice(0),_=r.or_queue.slice(0)),m.val.apply(e,arguments),m}}(),n.extend("last",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments);return r.then(function(r){function a(){var n;o===u.length&&(n=[],l?r(c):(u.forEach(function(t,e){n.push(s["s"+e])}),r.fail.apply(e,n)))}var l=!1,o=0,c={},s={},i=n.apply(e,t.call(arguments,1));u=u.map(function(u,i){return function(f){var p=t.call(arguments);p[0]=function(){l=!0,o++,c=arguments.length>1?n.messages.apply(e,arguments):arguments[0],a()},p[0].fail=function(){o++,s["s"+i]=arguments.length>1?n.messages.apply(e,arguments):arguments[0],a()},p[0].abort=function(){l||(f.abort(),r.abort())},u.apply(e,p)}}),i.gate.apply(e,u)}),r}}),n.extend("map",function(r,a){return function(u,l){return a("seq_error")||a("seq_aborted")?r:(r.seq(function(){var r,a=t.call(arguments);return l||(l=a.shift()),u||(u=a.shift()),"function"==typeof u&&Array.isArray(l)&&(r=u,u=l,l=r),n.apply(e,a).gate.apply(e,u.map(function(n){return function(){l.apply(e,[n].concat(t.call(arguments)))}}))}).val(function(){return t.call(arguments)}),r)}}),n.extend("none",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments);return r.then(function(r){function a(){var n;o===u.length&&(n=[],l?(u.forEach(function(t,e){n.push(c["s"+e])}),r.fail.apply(e,n)):(u.forEach(function(t,e){n.push(s["s"+e])}),r.apply(e,n)))}var l=!1,o=0,c={},s={},i=n.apply(e,t.call(arguments,1));u=u.map(function(u,i){return function(f){var p=t.call(arguments);p[0]=function(){l=!0,o++,c["s"+i]=arguments.length>1?n.messages.apply(e,arguments):arguments[0],a()},p[0].fail=function(){o++,s["s"+i]=arguments.length>1?n.messages.apply(e,arguments):arguments[0],a()},p[0].abort=function(){l||(f.abort(),r.abort())},u.apply(e,p)}}),i.gate.apply(e,u)}),r}}),n.react=function(t){function r(){var n=a.duplicate();return n.unpause.apply(e,arguments),n}var a;return n(function(){t(r)}),a=n().duplicate()},n.extend("runner",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments);return r.then(function(r){var a,l,o,c=t.call(arguments,1);u=u.map(function(t){var u=t;return"function"==typeof t&&t.constructor&&"GeneratorFunction"===t.constructor.name?(u=t.apply(e,c),c=[]):n.isSequence(t)&&"next"in t||(a=c.slice(),c=[],u=n.iterable().val(function(){return t.apply(e,a)})),n.isSequence(u)&&u.or(function(){r.fail.apply(e,arguments)}),u}),function s(){l=u.shift();try{o=l.next.apply(l,c)}catch(a){return r.fail(a)}n.isSequence(o.value)||(o.value=null!==o.value&&("object"==typeof o.value||"function"==typeof o.value)&&"then"in o.value?n().promise(o.value):n.apply(e,"undefined"!=typeof o.value?[o.value]:[])),o.value.val(function(){arguments.length>0&&(c=t.call(arguments)),o.done||u.push(l),u.length>0?s():r.apply(e,c)}).or(function(){l["throw"].apply(e,arguments)})}()}),r}}),n.extend("toPromise",function(r){return function(){return new Promise(function(a,u){r.val(function(){var r=t.call(arguments);return a.call(e,r.length>1?r:r[0]),n.messages.apply(e,r)}).or(function(){var n=t.call(arguments);u.call(e,n.length>1?n:n[0])})})}}),n.extend("try",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments).map(function(r){return function(a){var u=t.call(arguments),l=n.apply(e,u.slice(1));l.then(function(){r.apply(e,arguments)}).val(function(){a.apply(e,arguments)}).or(function(){var t=n.messages.apply(e,arguments);a({"catch":t.length>1?t:t[0]})})}});return r.then.apply(e,u),r}}),n.extend("until",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=t.call(arguments).map(function(r){return function a(u){var l=t.call(arguments),o=n.apply(e,l.slice(1));o.then(function(){var n=t.call(arguments);n[0]["break"]=function(){u.fail.apply(e,arguments),o.abort()},r.apply(e,n)}).val(function(){u.apply(e,arguments)}).or(function(){a.apply(e,l)})}});return r.then.apply(e,u),r}}),n.extend("waterfall",function(r,a){return function(){if(a("seq_error")||a("seq_aborted")||0===arguments.length)return r;var u=n.messages();return t.call(arguments).forEach(function(t){r.then(t).val(function(){var t=n.messages.apply(e,arguments);return u.push(t.length>1?t:t[0]),u})}),r}}),{}});
