Files
galaxy/static/scripts/packed/utils/ajax-queue.js
T

1 line
1.5 KiB
JavaScript

define([],function(){function k(m){var l=this;l.deferred=jQuery.Deferred();l.queue=[];l.responses=[];l.numToProcess=0;l.running=false;l.init(m||[]);l.start();return l}k.prototype.init=function i(m){var l=this;m.forEach(function(n){l.add(n)})};k.prototype.add=function j(n){var l=this,m=this.queue.length;this.numToProcess+=1;this.queue.push(function(){var o=n();o.done(function(p){l.deferred.notify({curr:m,total:l.numToProcess,response:p})});o.always(function(p){l.responses.push(p);if(l.queue.length){l.queue.shift()()}else{l.stop()}})})};k.prototype.start=function b(){if(this.queue.length){this.running=true;this.queue.shift()()}};k.prototype.stop=function h(l,m){this.running=false;this.queue=[];if(l){this.deferred.reject(m)}else{this.deferred.resolve(this.responses)}return this};k.prototype.done=function f(l){return this.deferred.done(l)};k.prototype.fail=function d(l){return this.deferred.fail(l)};k.prototype.always=function e(l){return this.deferred.always(l)};k.prototype.progress=function a(l){return this.deferred.progress(l)};k.create=function g(l){return new AjaxQeueue(l).deferred};function c(m){var l=this;l.names={};k.call(this,m);return l}c.prototype=new k();c.prototype.constructor=c;c.prototype.add=function j(l){if(!(l.hasOwnProperty("name")&&l.hasOwnProperty("fn"))){throw new Error('NamedAjaxQueue.add requires an object with both "name" and "fn": '+JSON.stringify(l))}if(this.names.hasOwnProperty(l.name)){return}this.names[l.name]=true;k.prototype.add.call(this,l.fn)};c.create=function g(l){return new c(l).deferred};return{AjaxQueue:k,NamedAjaxQueue:c}});