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

1 line
1.6 KiB
JavaScript

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