aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_external/js/rollingqueue.js
blob: df3797ada89b19f975e8f7f1e49605274fa69380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * 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;

})();