UNPKG

operations

Version:

A library for managing complex chains of asynchronous operations in Javascript.

1 lines 26.2 kB
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){(function(){var a=this,d=a._,e=Array.prototype,f=Object.prototype,g=Function.prototype,h=e.push,i=e.slice,j=e.concat,k=f.toString,l=f.hasOwnProperty,m=Array.isArray,n=Object.keys,o=g.bind,p=function(a){return a instanceof p?a:this instanceof p?void(this._wrapped=a):new p(a)};"undefined"!=typeof c?("undefined"!=typeof b&&b.exports&&(c=b.exports=p),c._=p):a._=p,p.VERSION="1.7.0";var q=function(a,b,c){if(void 0===b)return a;switch(null==c?3:c){case 1:return function(c){return a.call(b,c)};case 2:return function(c,d){return a.call(b,c,d)};case 3:return function(c,d,e){return a.call(b,c,d,e)};case 4:return function(c,d,e,f){return a.call(b,c,d,e,f)}}return function(){return a.apply(b,arguments)}};p.iteratee=function(a,b,c){return null==a?p.identity:p.isFunction(a)?q(a,b,c):p.isObject(a)?p.matches(a):p.property(a)},p.each=p.forEach=function(a,b,c){if(null==a)return a;b=q(b,c);var d,e=a.length;if(e===+e)for(d=0;e>d;d++)b(a[d],d,a);else{var f=p.keys(a);for(d=0,e=f.length;e>d;d++)b(a[f[d]],f[d],a)}return a},p.map=p.collect=function(a,b,c){if(null==a)return[];b=p.iteratee(b,c);for(var d,e=a.length!==+a.length&&p.keys(a),f=(e||a).length,g=Array(f),h=0;f>h;h++)d=e?e[h]:h,g[h]=b(a[d],d,a);return g};var r="Reduce of empty array with no initial value";p.reduce=p.foldl=p.inject=function(a,b,c,d){null==a&&(a=[]),b=q(b,d,4);var e,f=a.length!==+a.length&&p.keys(a),g=(f||a).length,h=0;if(arguments.length<3){if(!g)throw new TypeError(r);c=a[f?f[h++]:h++]}for(;g>h;h++)e=f?f[h]:h,c=b(c,a[e],e,a);return c},p.reduceRight=p.foldr=function(a,b,c,d){null==a&&(a=[]),b=q(b,d,4);var e,f=a.length!==+a.length&&p.keys(a),g=(f||a).length;if(arguments.length<3){if(!g)throw new TypeError(r);c=a[f?f[--g]:--g]}for(;g--;)e=f?f[g]:g,c=b(c,a[e],e,a);return c},p.find=p.detect=function(a,b,c){var d;return b=p.iteratee(b,c),p.some(a,function(a,c,e){return b(a,c,e)?(d=a,!0):void 0}),d},p.filter=p.select=function(a,b,c){var d=[];return null==a?d:(b=p.iteratee(b,c),p.each(a,function(a,c,e){b(a,c,e)&&d.push(a)}),d)},p.reject=function(a,b,c){return p.filter(a,p.negate(p.iteratee(b)),c)},p.every=p.all=function(a,b,c){if(null==a)return!0;b=p.iteratee(b,c);var d,e,f=a.length!==+a.length&&p.keys(a),g=(f||a).length;for(d=0;g>d;d++)if(e=f?f[d]:d,!b(a[e],e,a))return!1;return!0},p.some=p.any=function(a,b,c){if(null==a)return!1;b=p.iteratee(b,c);var d,e,f=a.length!==+a.length&&p.keys(a),g=(f||a).length;for(d=0;g>d;d++)if(e=f?f[d]:d,b(a[e],e,a))return!0;return!1},p.contains=p.include=function(a,b){return null==a?!1:(a.length!==+a.length&&(a=p.values(a)),p.indexOf(a,b)>=0)},p.invoke=function(a,b){var c=i.call(arguments,2),d=p.isFunction(b);return p.map(a,function(a){return(d?b:a[b]).apply(a,c)})},p.pluck=function(a,b){return p.map(a,p.property(b))},p.where=function(a,b){return p.filter(a,p.matches(b))},p.findWhere=function(a,b){return p.find(a,p.matches(b))},p.max=function(a,b,c){var d,e,f=-1/0,g=-1/0;if(null==b&&null!=a){a=a.length===+a.length?a:p.values(a);for(var h=0,i=a.length;i>h;h++)d=a[h],d>f&&(f=d)}else b=p.iteratee(b,c),p.each(a,function(a,c,d){e=b(a,c,d),(e>g||e===-1/0&&f===-1/0)&&(f=a,g=e)});return f},p.min=function(a,b,c){var d,e,f=1/0,g=1/0;if(null==b&&null!=a){a=a.length===+a.length?a:p.values(a);for(var h=0,i=a.length;i>h;h++)d=a[h],f>d&&(f=d)}else b=p.iteratee(b,c),p.each(a,function(a,c,d){e=b(a,c,d),(g>e||1/0===e&&1/0===f)&&(f=a,g=e)});return f},p.shuffle=function(a){for(var b,c=a&&a.length===+a.length?a:p.values(a),d=c.length,e=Array(d),f=0;d>f;f++)b=p.random(0,f),b!==f&&(e[f]=e[b]),e[b]=c[f];return e},p.sample=function(a,b,c){return null==b||c?(a.length!==+a.length&&(a=p.values(a)),a[p.random(a.length-1)]):p.shuffle(a).slice(0,Math.max(0,b))},p.sortBy=function(a,b,c){return b=p.iteratee(b,c),p.pluck(p.map(a,function(a,c,d){return{value:a,index:c,criteria:b(a,c,d)}}).sort(function(a,b){var c=a.criteria,d=b.criteria;if(c!==d){if(c>d||void 0===c)return 1;if(d>c||void 0===d)return-1}return a.index-b.index}),"value")};var s=function(a){return function(b,c,d){var e={};return c=p.iteratee(c,d),p.each(b,function(d,f){var g=c(d,f,b);a(e,d,g)}),e}};p.groupBy=s(function(a,b,c){p.has(a,c)?a[c].push(b):a[c]=[b]}),p.indexBy=s(function(a,b,c){a[c]=b}),p.countBy=s(function(a,b,c){p.has(a,c)?a[c]++:a[c]=1}),p.sortedIndex=function(a,b,c,d){c=p.iteratee(c,d,1);for(var e=c(b),f=0,g=a.length;g>f;){var h=f+g>>>1;c(a[h])<e?f=h+1:g=h}return f},p.toArray=function(a){return a?p.isArray(a)?i.call(a):a.length===+a.length?p.map(a,p.identity):p.values(a):[]},p.size=function(a){return null==a?0:a.length===+a.length?a.length:p.keys(a).length},p.partition=function(a,b,c){b=p.iteratee(b,c);var d=[],e=[];return p.each(a,function(a,c,f){(b(a,c,f)?d:e).push(a)}),[d,e]},p.first=p.head=p.take=function(a,b,c){return null==a?void 0:null==b||c?a[0]:0>b?[]:i.call(a,0,b)},p.initial=function(a,b,c){return i.call(a,0,Math.max(0,a.length-(null==b||c?1:b)))},p.last=function(a,b,c){return null==a?void 0:null==b||c?a[a.length-1]:i.call(a,Math.max(a.length-b,0))},p.rest=p.tail=p.drop=function(a,b,c){return i.call(a,null==b||c?1:b)},p.compact=function(a){return p.filter(a,p.identity)};var t=function(a,b,c,d){if(b&&p.every(a,p.isArray))return j.apply(d,a);for(var e=0,f=a.length;f>e;e++){var g=a[e];p.isArray(g)||p.isArguments(g)?b?h.apply(d,g):t(g,b,c,d):c||d.push(g)}return d};p.flatten=function(a,b){return t(a,b,!1,[])},p.without=function(a){return p.difference(a,i.call(arguments,1))},p.uniq=p.unique=function(a,b,c,d){if(null==a)return[];p.isBoolean(b)||(d=c,c=b,b=!1),null!=c&&(c=p.iteratee(c,d));for(var e=[],f=[],g=0,h=a.length;h>g;g++){var i=a[g];if(b)g&&f===i||e.push(i),f=i;else if(c){var j=c(i,g,a);p.indexOf(f,j)<0&&(f.push(j),e.push(i))}else p.indexOf(e,i)<0&&e.push(i)}return e},p.union=function(){return p.uniq(t(arguments,!0,!0,[]))},p.intersection=function(a){if(null==a)return[];for(var b=[],c=arguments.length,d=0,e=a.length;e>d;d++){var f=a[d];if(!p.contains(b,f)){for(var g=1;c>g&&p.contains(arguments[g],f);g++);g===c&&b.push(f)}}return b},p.difference=function(a){var b=t(i.call(arguments,1),!0,!0,[]);return p.filter(a,function(a){return!p.contains(b,a)})},p.zip=function(a){if(null==a)return[];for(var b=p.max(arguments,"length").length,c=Array(b),d=0;b>d;d++)c[d]=p.pluck(arguments,d);return c},p.object=function(a,b){if(null==a)return{};for(var c={},d=0,e=a.length;e>d;d++)b?c[a[d]]=b[d]:c[a[d][0]]=a[d][1];return c},p.indexOf=function(a,b,c){if(null==a)return-1;var d=0,e=a.length;if(c){if("number"!=typeof c)return d=p.sortedIndex(a,b),a[d]===b?d:-1;d=0>c?Math.max(0,e+c):c}for(;e>d;d++)if(a[d]===b)return d;return-1},p.lastIndexOf=function(a,b,c){if(null==a)return-1;var d=a.length;for("number"==typeof c&&(d=0>c?d+c+1:Math.min(d,c+1));--d>=0;)if(a[d]===b)return d;return-1},p.range=function(a,b,c){arguments.length<=1&&(b=a||0,a=0),c=c||1;for(var d=Math.max(Math.ceil((b-a)/c),0),e=Array(d),f=0;d>f;f++,a+=c)e[f]=a;return e};var u=function(){};p.bind=function(a,b){var c,d;if(o&&a.bind===o)return o.apply(a,i.call(arguments,1));if(!p.isFunction(a))throw new TypeError("Bind must be called on a function");return c=i.call(arguments,2),d=function(){if(!(this instanceof d))return a.apply(b,c.concat(i.call(arguments)));u.prototype=a.prototype;var e=new u;u.prototype=null;var f=a.apply(e,c.concat(i.call(arguments)));return p.isObject(f)?f:e}},p.partial=function(a){var b=i.call(arguments,1);return function(){for(var c=0,d=b.slice(),e=0,f=d.length;f>e;e++)d[e]===p&&(d[e]=arguments[c++]);for(;c<arguments.length;)d.push(arguments[c++]);return a.apply(this,d)}},p.bindAll=function(a){var b,c,d=arguments.length;if(1>=d)throw new Error("bindAll must be passed function names");for(b=1;d>b;b++)c=arguments[b],a[c]=p.bind(a[c],a);return a},p.memoize=function(a,b){var c=function(d){var e=c.cache,f=b?b.apply(this,arguments):d;return p.has(e,f)||(e[f]=a.apply(this,arguments)),e[f]};return c.cache={},c},p.delay=function(a,b){var c=i.call(arguments,2);return setTimeout(function(){return a.apply(null,c)},b)},p.defer=function(a){return p.delay.apply(p,[a,1].concat(i.call(arguments,1)))},p.throttle=function(a,b,c){var d,e,f,g=null,h=0;c||(c={});var i=function(){h=c.leading===!1?0:p.now(),g=null,f=a.apply(d,e),g||(d=e=null)};return function(){var j=p.now();h||c.leading!==!1||(h=j);var k=b-(j-h);return d=this,e=arguments,0>=k||k>b?(clearTimeout(g),g=null,h=j,f=a.apply(d,e),g||(d=e=null)):g||c.trailing===!1||(g=setTimeout(i,k)),f}},p.debounce=function(a,b,c){var d,e,f,g,h,i=function(){var j=p.now()-g;b>j&&j>0?d=setTimeout(i,b-j):(d=null,c||(h=a.apply(f,e),d||(f=e=null)))};return function(){f=this,e=arguments,g=p.now();var j=c&&!d;return d||(d=setTimeout(i,b)),j&&(h=a.apply(f,e),f=e=null),h}},p.wrap=function(a,b){return p.partial(b,a)},p.negate=function(a){return function(){return!a.apply(this,arguments)}},p.compose=function(){var a=arguments,b=a.length-1;return function(){for(var c=b,d=a[b].apply(this,arguments);c--;)d=a[c].call(this,d);return d}},p.after=function(a,b){return function(){return--a<1?b.apply(this,arguments):void 0}},p.before=function(a,b){var c;return function(){return--a>0?c=b.apply(this,arguments):b=null,c}},p.once=p.partial(p.before,2),p.keys=function(a){if(!p.isObject(a))return[];if(n)return n(a);var b=[];for(var c in a)p.has(a,c)&&b.push(c);return b},p.values=function(a){for(var b=p.keys(a),c=b.length,d=Array(c),e=0;c>e;e++)d[e]=a[b[e]];return d},p.pairs=function(a){for(var b=p.keys(a),c=b.length,d=Array(c),e=0;c>e;e++)d[e]=[b[e],a[b[e]]];return d},p.invert=function(a){for(var b={},c=p.keys(a),d=0,e=c.length;e>d;d++)b[a[c[d]]]=c[d];return b},p.functions=p.methods=function(a){var b=[];for(var c in a)p.isFunction(a[c])&&b.push(c);return b.sort()},p.extend=function(a){if(!p.isObject(a))return a;for(var b,c,d=1,e=arguments.length;e>d;d++){b=arguments[d];for(c in b)l.call(b,c)&&(a[c]=b[c])}return a},p.pick=function(a,b,c){var d,e={};if(null==a)return e;if(p.isFunction(b)){b=q(b,c);for(d in a){var f=a[d];b(f,d,a)&&(e[d]=f)}}else{var g=j.apply([],i.call(arguments,1));a=new Object(a);for(var h=0,k=g.length;k>h;h++)d=g[h],d in a&&(e[d]=a[d])}return e},p.omit=function(a,b,c){if(p.isFunction(b))b=p.negate(b);else{var d=p.map(j.apply([],i.call(arguments,1)),String);b=function(a,b){return!p.contains(d,b)}}return p.pick(a,b,c)},p.defaults=function(a){if(!p.isObject(a))return a;for(var b=1,c=arguments.length;c>b;b++){var d=arguments[b];for(var e in d)void 0===a[e]&&(a[e]=d[e])}return a},p.clone=function(a){return p.isObject(a)?p.isArray(a)?a.slice():p.extend({},a):a},p.tap=function(a,b){return b(a),a};var v=function(a,b,c,d){if(a===b)return 0!==a||1/a===1/b;if(null==a||null==b)return a===b;a instanceof p&&(a=a._wrapped),b instanceof p&&(b=b._wrapped);var e=k.call(a);if(e!==k.call(b))return!1;switch(e){case"[object RegExp]":case"[object String]":return""+a==""+b;case"[object Number]":return+a!==+a?+b!==+b:0===+a?1/+a===1/b:+a===+b;case"[object Date]":case"[object Boolean]":return+a===+b}if("object"!=typeof a||"object"!=typeof b)return!1;for(var f=c.length;f--;)if(c[f]===a)return d[f]===b;var g=a.constructor,h=b.constructor;if(g!==h&&"constructor"in a&&"constructor"in b&&!(p.isFunction(g)&&g instanceof g&&p.isFunction(h)&&h instanceof h))return!1;c.push(a),d.push(b);var i,j;if("[object Array]"===e){if(i=a.length,j=i===b.length)for(;i--&&(j=v(a[i],b[i],c,d)););}else{var l,m=p.keys(a);if(i=m.length,j=p.keys(b).length===i)for(;i--&&(l=m[i],j=p.has(b,l)&&v(a[l],b[l],c,d)););}return c.pop(),d.pop(),j};p.isEqual=function(a,b){return v(a,b,[],[])},p.isEmpty=function(a){if(null==a)return!0;if(p.isArray(a)||p.isString(a)||p.isArguments(a))return 0===a.length;for(var b in a)if(p.has(a,b))return!1;return!0},p.isElement=function(a){return!(!a||1!==a.nodeType)},p.isArray=m||function(a){return"[object Array]"===k.call(a)},p.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a},p.each(["Arguments","Function","String","Number","Date","RegExp"],function(a){p["is"+a]=function(b){return k.call(b)==="[object "+a+"]"}}),p.isArguments(arguments)||(p.isArguments=function(a){return p.has(a,"callee")}),"function"!=typeof/./&&(p.isFunction=function(a){return"function"==typeof a||!1}),p.isFinite=function(a){return isFinite(a)&&!isNaN(parseFloat(a))},p.isNaN=function(a){return p.isNumber(a)&&a!==+a},p.isBoolean=function(a){return a===!0||a===!1||"[object Boolean]"===k.call(a)},p.isNull=function(a){return null===a},p.isUndefined=function(a){return void 0===a},p.has=function(a,b){return null!=a&&l.call(a,b)},p.noConflict=function(){return a._=d,this},p.identity=function(a){return a},p.constant=function(a){return function(){return a}},p.noop=function(){},p.property=function(a){return function(b){return b[a]}},p.matches=function(a){var b=p.pairs(a),c=b.length;return function(a){if(null==a)return!c;a=new Object(a);for(var d=0;c>d;d++){var e=b[d],f=e[0];if(e[1]!==a[f]||!(f in a))return!1}return!0}},p.times=function(a,b,c){var d=Array(Math.max(0,a));b=q(b,c,1);for(var e=0;a>e;e++)d[e]=b(e);return d},p.random=function(a,b){return null==b&&(b=a,a=0),a+Math.floor(Math.random()*(b-a+1))},p.now=Date.now||function(){return(new Date).getTime()};var w={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"},x=p.invert(w),y=function(a){var b=function(b){return a[b]},c="(?:"+p.keys(a).join("|")+")",d=RegExp(c),e=RegExp(c,"g");return function(a){return a=null==a?"":""+a,d.test(a)?a.replace(e,b):a}};p.escape=y(w),p.unescape=y(x),p.result=function(a,b){if(null==a)return void 0;var c=a[b];return p.isFunction(c)?a[b]():c};var z=0;p.uniqueId=function(a){var b=++z+"";return a?a+b:b},p.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var A=/(.)^/,B={"'":"'","\\":"\\","\r":"r","\n":"n","\u2028":"u2028","\u2029":"u2029"},C=/\\|'|\r|\n|\u2028|\u2029/g,D=function(a){return"\\"+B[a]};p.template=function(a,b,c){!b&&c&&(b=c),b=p.defaults({},b,p.templateSettings);var d=RegExp([(b.escape||A).source,(b.interpolate||A).source,(b.evaluate||A).source].join("|")+"|$","g"),e=0,f="__p+='";a.replace(d,function(b,c,d,g,h){return f+=a.slice(e,h).replace(C,D),e=h+b.length,c?f+="'+\n((__t=("+c+"))==null?'':_.escape(__t))+\n'":d?f+="'+\n((__t=("+d+"))==null?'':__t)+\n'":g&&(f+="';\n"+g+"\n__p+='"),b}),f+="';\n",b.variable||(f="with(obj||{}){\n"+f+"}\n"),f="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+f+"return __p;\n";try{var g=new Function(b.variable||"obj","_",f)}catch(h){throw h.source=f,h}var i=function(a){return g.call(this,a,p)},j=b.variable||"obj";return i.source="function("+j+"){\n"+f+"}",i},p.chain=function(a){var b=p(a);return b._chain=!0,b};var E=function(a){return this._chain?p(a).chain():a};p.mixin=function(a){p.each(p.functions(a),function(b){var c=p[b]=a[b];p.prototype[b]=function(){var a=[this._wrapped];return h.apply(a,arguments),E.call(this,c.apply(p,a))}})},p.mixin(p),p.each(["pop","push","reverse","shift","sort","splice","unshift"],function(a){var b=e[a];p.prototype[a]=function(){var c=this._wrapped;return b.apply(c,arguments),"shift"!==a&&"splice"!==a||0!==c.length||delete c[0],E.call(this,c)}}),p.each(["concat","join","slice"],function(a){var b=e[a];p.prototype[a]=function(){return E.call(this,b.apply(this._wrapped,arguments))}}),p.prototype.value=function(){return this._wrapped},"function"==typeof define&&define.amd&&define("underscore",[],function(){return p})}).call(this)},{}],2:[function(a,b,c){{var d=a("./operation").Operation,e=a("./queue").OperationQueue,f=a("./log");a("underscore")}"object"==typeof window?op={Operation:d,OperationQueue:e,Logger:f}:(c.Operation=d,c.OperationQueue=e,c.Logger=f)},{"./log":3,"./operation":4,"./queue":5,underscore:1}],3:[function(a,b){function c(a){return this?(this.name=a,this.trace=d(this,e.bind(console.debug?console.debug:console.log,console),c.Level.trace),this.debug=d(this,e.bind(console.debug?console.debug:console.log,console),c.Level.debug),this.info=d(this,e.bind(console.info?console.info:console.log,console),c.Level.info),this.log=d(this,e.bind(console.log?console.log:console.log,console),c.Level.info),this.warn=d(this,e.bind(console.warn?console.warn:console.log,console),c.Level.warning),this.error=d(this,e.bind(console.error?console.error:console.log,console),c.Level.error),void(this.fatal=d(this,e.bind(console.error?console.error:console.log,console),c.Level.fatal))):new c(a)}function d(a,b,c){var d=function(d){a.performLog(b,c,d,arguments)};return Object.defineProperty(d,"isEnabled",{get:function(){var b=a.currentLevel();return c>=b},enumerable:!0,configurable:!0}),d.f=b,d.logger=a,d.level=c,d}var e=a("underscore"),f={};c.Level={trace:0,debug:1,info:2,warning:3,warn:3,error:4,fatal:5},c.LevelText={},c.LevelText[c.Level.trace]="TRACE",c.LevelText[c.Level.debug]="DEBUG",c.LevelText[c.Level.info]="INFO ",c.LevelText[c.Level.warning]="WARN ",c.LevelText[c.Level.error]="ERROR",c.levelAsText=function(a){return this.LevelText[a]},c.loggerWithName=function(a){return new c(a)},c.prototype.currentLevel=function(){var a=f[this.name];return a?a:c.Level.trace},c.prototype.setLevel=function(a){f[this.name]=a},c.prototype.override=function(a,b,d){var e=c.levelAsText(a),f=this[e.trim().toLowerCase()],g=f.f,h=Array.prototype.slice.call(arguments,3,arguments.length);this.performLog(g,a,d,h,b)},c.prototype.performLog=function(a,b,d,f,g){var h=this,i=void 0!==g?g:this.currentLevel();if(b>=i){a=e.partial(a,c.levelAsText(b)+" ["+h.name+"]: "+d);for(var j=[],k=0;k<f.length;k++)j[k]=f[k];j.splice(0,1),a.apply(a,j)}},b.exports=c},{underscore:1}],4:[function(a,b){function c(){if(!this)return new(Function.prototype.bind.apply(c,arguments));var a=this;arguments.length&&("string"==typeof arguments[0]?(this.name=arguments[0],this.work=arguments[1],this.completion=arguments[2]):("function"==typeof arguments[0]||"[object Array]"===Object.prototype.toString.call(arguments[0])||arguments[0]instanceof c)&&(this.work=arguments[0],this.completion=arguments[1])),this.error=null,this.completed=!1,this.result=null,this.running=!1,this.cancelled=!1,this.dependencies=[],this._mustSucceed=[],this._onCompletion=[],this.logLevel=null,Object.defineProperty(this,"failed",{get:function(){return!!a.error||a.failedDueToDependency},enumerable:!0,configurable:!0}),Object.defineProperty(this,"composite",{get:function(){return a.work instanceof c||"[object Array]"===Object.prototype.toString.call(a.work)},enumerable:!0,configurable:!0}),Object.defineProperty(this,"numOperationsRemaining",{get:function(){return a.work instanceof c?a.work.completed?0:1:"[object Array]"===Object.prototype.toString.call(a.work)?d.reduce(a.work,function(a,b){return b.completed?a:a+1},0):null},enumerable:!0,configurable:!0}),Object.defineProperty(this,"canRun",{get:function(){return a.dependencies.length?d.reduce(a.dependencies,function(b,c){var d=a._mustSucceed.indexOf(c)>-1,e=b&&c.completed;return d&&e&&(e=e&&!(c.failed||c.cancelled)),e},!0):!0},enumerable:!0,configurable:!0}),Object.defineProperty(this,"failedDueToDependency",{get:function(){if(a.dependencies.length){var b=d.reduce(a.dependencies,function(b,c){var d=a._mustSucceed.indexOf(c)>-1,e=(c.failed||c.cancelled)&&d;return e&&b.push(c),b},[]);return b.length?b:!1}return!1},enumerable:!0,configurable:!0}),Object.defineProperty(this,"failedDueToCancellationOfDependency",{get:function(){if(a.dependencies.length){var b=d.reduce(a.dependencies,function(b,c){var d=a._mustSucceed.indexOf(c)>-1;return d&&c.cancelled&&b.push(c),b},[]);return b.length?b:!1}return!1},enumerable:!0,configurable:!0}),Object.defineProperty(this,"loggingOveridden",{get:function(){return a.logLevel?a.logLevel<=e.Level.info:!1},enumerable:!0,configurable:!0})}var d=a("underscore"),e=a("./log"),f=e.loggerWithName("Operation");c.running=[],c.prototype._startSingle=function(){var a=this;this.work(function(b,c){a.result=c,a.error=b,a.completed=!0,a.running=!1,a._complete()})},c.prototype._startComposite=function(){var a=this,b=a.work instanceof c?[a.work]:a.work;d.each(b,function(c){c.completion=function(){var c=a.numOperationsRemaining;if(!c){var e=d.pluck(b,"error"),f=d.pluck(b,"result");a.result=d.some(f)?f:null,a.error=d.some(e)?e:null,a.completed=!0,a.running=!1,a._complete()}},c.start()})},c.prototype._logCompletion=function(){var a=this._getLogFunc();if(f.info.isEnabled||this.loggingOveridden){var b=this.name||"Unnamed",c=this.failedDueToDependency;if(c)a('"'+b+'" failed due to failure/cancellation of dependencies: '+d.pluck(c,"name").join(", "));else if(this.failed){var e=this.error;e="[object Array]"===Object.prototype.toString.call(e)?d.filter(e,function(a){return a}):[this.error],a('"'+b+'" failed due to errors:',e)}else a(this.cancelled?'"'+b+'" has been cancelled.':'"'+b+'" has succeeded.')}},c.prototype._getLogFunc=function(){return this.logLevel?d.bind(f.override,f,e.Level.info,this.logLevel):f.info},c.prototype._logStart=function(){if(f.info.isEnabled||this.loggingOveridden){var a=this.name||"Unnamed",b=this._getLogFunc();b('"'+a+'" has started.')}},c.prototype._complete=function(){var a=this;this.completed=!0;var b=c.running.indexOf(this);c.running.splice(b,1),this.completion&&d.bind(this.completion,this)(),this._logCompletion(),d.each(this._onCompletion,function(b){d.bind(b,a)()})},c.prototype.__start=function(){this._logStart(),this.work?(this.composite?this._startComposite():this._startSingle(),c.running.push(this)):(this.result=null,this.error=null,this.running=!1,this._complete())},c.prototype.start=function(){var a=this,b=!this.running&&!this.completed,c=b&&this.failed;c?this._complete():b&&(this.running=!0,this.canRun?this.__start():d.each(this.dependencies,function(b){b.onCompletion(function(){a.canRun&&a.__start()})}))},c.prototype.addDependency=function(){var a=this;if(1==arguments.length)this.dependencies.push(arguments[0]);else if(arguments.length){var b=arguments,c=b[b.length-1],e=!1;"boolean"==typeof c&&(b=Array.prototype.slice.call(b,0,b.length-1),e=c),d.each(b,function(b){a.dependencies.push(b)}),e&&d.each(b,function(b){a._mustSucceed.push(b)})}},c.prototype.onCompletion=function(a){this._onCompletion.push(a)},c.prototype.cancel=function(a){this.cancelled||(this.cancelled=!0,f.debug("Cancelling "+this.name,this),this.composite&&d.each(this.work,function(a){a.cancel()}),this.onCompletion(function(){this.running=!1,a&&a()}))},Object.defineProperty(c,"logLevel",{get:function(){return f.currentLevel()},set:function(a){f.setLevel(a)},configurable:!0,enumerable:!0}),b.exports.Operation=c},{"./log":3,underscore:1}],5:[function(a,b){function c(){if(!this)return new(Function.prototype.bind.apply(c,arguments));var a=this;arguments.length&&("number"==typeof arguments[0]?this.maxConcurrentOperations=arguments[0]:(this.name=arguments[0],this.maxConcurrentOperations=arguments[1])),this._queuedOperations=[],this._runningOperations=[],this._running=!1,this._onStart=[],this._onStop=[],this.logLevel=null,Object.defineProperty(this,"numRunningOperations",{get:function(){return a._runningOperations.length},configurable:!0,enumerable:!0}),Object.defineProperty(this,"loggingOveridden",{get:function(){return a.logLevel?a.logLevel<=e.Level.info:!1},enumerable:!0,configurable:!0})}var d=a("underscore"),e=a("./log"),f=e.loggerWithName("OperationQueue");c.prototype._nextOperations=function(){for(var a=this;a._runningOperations.length<a.maxConcurrentOperations&&a._queuedOperations.length;){var b=a._queuedOperations[0];a._queuedOperations.splice(0,1),a._runOperation(b)}},c.prototype._runOperation=function(a){for(var b=this,c=0;c<this._queuedOperations.length;c++)if(this._queuedOperations[c]==a){this._queuedOperations.splice(c,1);break}this._runningOperations.push(a),a.onCompletion(function(){var c=b._runningOperations.indexOf(a);b._runningOperations.splice(c,1),b._running&&b._nextOperations(),b._logStatus()}),a.start(),this._logStatus()},c.prototype._logStatus=function(){var a=this._getLogFunc();if(f.info.isEnabled||this.loggingOveridden){var b=this.numRunningOperations,c=this._queuedOperations.length,d=this.name||"Unnamed Queue";a(b&&c?'"'+d+'" now has '+b.toString()+" operations running and "+c.toString()+" operations queued":b?'"'+d+'" now has '+b.toString()+" operations running":c?'"'+d+'" now has '+c.toString()+" operations queued":'"'+d+'" has no operations running or queued')}},c.prototype._logStart=function(){var a=this._getLogFunc();if(f.info.isEnabled||this.loggingOveridden){var b=this.name||"Unnamed Queue";a('"'+b+'" is now running')}},c.prototype._getLogFunc=function(){return this.logLevel?d.bind(f.override,f,e.Level.info,this.logLevel):f.info},c.prototype._logStop=function(){var a=this._getLogFunc();if(f.info.isEnabled||this.loggingOveridden){var b=this.name||"Unnamed Queue";a('"'+b+'" is no longer running')}},c.prototype._addOperation=function(a){this.numRunningOperations<this.maxConcurrentOperations&&this._running?this._runOperation(a):this._queuedOperations.push(a),this._logStatus()},c.prototype.addOperation=function(a){var b=this;"[object Array]"===Object.prototype.toString.call(a)?d.each(a,function(a){b._addOperation(a)}):this._addOperation(a)},c.prototype.start=function(){var a=this,b=this._running;this._running=!0,b||(d.each(a._onStart,function(b){d.bind(b,a)()}),a._nextOperations(),a._logStart())},c.prototype.stop=function(a){var b=this,c=this._running;if(this._running=!1,c){if(a){var e=this._runningOperations.slice(0);d.each(e,function(a){a.cancel()})}b._logStop(),d.each(b._onStop,function(a){d.bind(a,b)()})}},c.prototype.onStart=function(a){this._onStart.push(a)},c.prototype.onStop=function(a){this._onStop.push(a)},Object.defineProperty(c,"logLevel",{get:function(){return f.currentLevel()},set:function(a){f.setLevel(a)},configurable:!0,enumerable:!0}),b.exports.OperationQueue=c},{"./log":3,underscore:1}]},{},[2]);