server/apps/files_external/js/rollingqueue.js

138 lines
3.3 KiB
JavaScript
Raw Normal View History

/**
* ownCloud
*
* @author Juan Pablo Villafañez Ramos <jvillafanez@owncloud.com>
* @author Jesus Macias Portela <jesus@owncloud.com>
* @copyright (C) 2014 ownCloud, Inc.
*
2015-11-18 17:34:26 +00:00
* This file is licensed under the Affero General Public License version 3
* or later.
*
2015-11-18 17:34:26 +00:00
* 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) {
2015-11-24 07:16:14 +00:00
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";
}
2016-07-05 21:18:06 +00:00
for (var i = 0; i < this.queueWindow; i++) {
2015-11-24 07:16:14 +00:00
this.launchNext();
}
};
2015-11-24 07:16:14 +00:00
this.hasNext = function() {
return (this.counter in this.functionList);
};
2015-11-24 07:16:14 +00:00
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);
}
2015-11-24 07:16:14 +00:00
if ($.isFunction(funcData.fail)) {
defObj.fail(funcData.fail);
}
2015-11-24 07:16:14 +00:00
if ($.isFunction(funcData.always)) {
defObj.always(funcData.always);
}
2015-11-24 07:16:14 +00:00
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;
2016-07-05 21:18:06 +00:00
})();