123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /**
- * ownCloud
- *
- * @author Juan Pablo Villafañez Ramos <jvillafanez@owncloud.com>
- * @author Jesus Macias Portela <jesus@owncloud.com>
- * @copyright (C) 2014 ownCloud, Inc.
- *
- * This file is licensed under the Affero General Public License version 3
- * or later.
- *
- * See the COPYING-README file.
- *
- */
- (function(){
- /**
- * Launch several functions at thee same time. The number of functions
- * running at the same time is controlled by the queueWindow param
- *
- * The function list come in the following format:
- *
- * var flist = [
- * {
- * funcName: function () {
- * var d = $.Deferred();
- * setTimeout(function(){d.resolve();}, 1000);
- * return d;
- * }
- * },
- * {
- * funcName: $.get,
- * funcArgs: [
- * OC.filePath('files_external', 'ajax', 'connectivityCheck.php'),
- * {},
- * function () {
- * console.log('titoooo');
- * }
- * ]
- * },
- * {
- * funcName: $.get,
- * funcArgs: [
- * OC.filePath('files_external', 'ajax', 'connectivityCheck.php')
- * ],
- * done: function () {
- * console.log('yuupi');
- * },
- * always: function () {
- * console.log('always done');
- * }
- * }
- *];
- *
- * functions MUST implement the deferred interface
- *
- * @param functionList list of functions that the queue will run
- * (check example above for the expected format)
- * @param queueWindow specify the number of functions that will
- * be executed at the same time
- */
- var RollingQueue = function (functionList, queueWindow, callback) {
- this.queueWindow = queueWindow || 1;
- this.functionList = functionList;
- this.callback = callback;
- this.counter = 0;
- this.runQueue = function() {
- this.callbackCalled = false;
- this.deferredsList = [];
- if (!$.isArray(this.functionList)) {
- throw "functionList must be an array";
- }
- for (var i = 0; i < this.queueWindow; i++) {
- this.launchNext();
- }
- };
- this.hasNext = function() {
- return (this.counter in this.functionList);
- };
- this.launchNext = function() {
- var currentCounter = this.counter++;
- if (currentCounter in this.functionList) {
- var funcData = this.functionList[currentCounter];
- if ($.isFunction(funcData.funcName)) {
- var defObj = funcData.funcName.apply(funcData.funcName, funcData.funcArgs);
- this.deferredsList.push(defObj);
- if ($.isFunction(funcData.done)) {
- defObj.done(funcData.done);
- }
- if ($.isFunction(funcData.fail)) {
- defObj.fail(funcData.fail);
- }
- if ($.isFunction(funcData.always)) {
- defObj.always(funcData.always);
- }
- if (this.hasNext()) {
- var self = this;
- defObj.always(function(){
- _.defer($.proxy(function(){
- self.launchNext();
- }, self));
- });
- } else {
- if (!this.callbackCalled) {
- this.callbackCalled = true;
- if ($.isFunction(this.callback)) {
- $.when.apply($, this.deferredsList)
- .always($.proxy(function(){
- this.callback();
- }, this)
- );
- }
- }
- }
- return defObj;
- }
- }
- return false;
- };
- };
- if (!OCA.Files_External) {
- OCA.Files_External = {};
- }
- if (!OCA.Files_External.StatusManager) {
- OCA.Files_External.StatusManager = {};
- }
- OCA.Files_External.StatusManager.RollingQueue = RollingQueue;
- })();
|