1
0

rollingqueue.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * ownCloud
  3. *
  4. * @author Juan Pablo Villafañez Ramos <jvillafanez@owncloud.com>
  5. * @author Jesus Macias Portela <jesus@owncloud.com>
  6. * @copyright (C) 2014 ownCloud, Inc.
  7. *
  8. * This file is licensed under the Affero General Public License version 3
  9. * or later.
  10. *
  11. * See the COPYING-README file.
  12. *
  13. */
  14. (function(){
  15. /**
  16. * Launch several functions at thee same time. The number of functions
  17. * running at the same time is controlled by the queueWindow param
  18. *
  19. * The function list come in the following format:
  20. *
  21. * var flist = [
  22. * {
  23. * funcName: function () {
  24. * var d = $.Deferred();
  25. * setTimeout(function(){d.resolve();}, 1000);
  26. * return d;
  27. * }
  28. * },
  29. * {
  30. * funcName: $.get,
  31. * funcArgs: [
  32. * OC.filePath('files_external', 'ajax', 'connectivityCheck.php'),
  33. * {},
  34. * function () {
  35. * console.log('titoooo');
  36. * }
  37. * ]
  38. * },
  39. * {
  40. * funcName: $.get,
  41. * funcArgs: [
  42. * OC.filePath('files_external', 'ajax', 'connectivityCheck.php')
  43. * ],
  44. * done: function () {
  45. * console.log('yuupi');
  46. * },
  47. * always: function () {
  48. * console.log('always done');
  49. * }
  50. * }
  51. *];
  52. *
  53. * functions MUST implement the deferred interface
  54. *
  55. * @param functionList list of functions that the queue will run
  56. * (check example above for the expected format)
  57. * @param queueWindow specify the number of functions that will
  58. * be executed at the same time
  59. */
  60. var RollingQueue = function (functionList, queueWindow, callback) {
  61. this.queueWindow = queueWindow || 1;
  62. this.functionList = functionList;
  63. this.callback = callback;
  64. this.counter = 0;
  65. this.runQueue = function() {
  66. this.callbackCalled = false;
  67. this.deferredsList = [];
  68. if (!$.isArray(this.functionList)) {
  69. throw "functionList must be an array";
  70. }
  71. for (var i = 0; i < this.queueWindow; i++) {
  72. this.launchNext();
  73. }
  74. };
  75. this.hasNext = function() {
  76. return (this.counter in this.functionList);
  77. };
  78. this.launchNext = function() {
  79. var currentCounter = this.counter++;
  80. if (currentCounter in this.functionList) {
  81. var funcData = this.functionList[currentCounter];
  82. if ($.isFunction(funcData.funcName)) {
  83. var defObj = funcData.funcName.apply(funcData.funcName, funcData.funcArgs);
  84. this.deferredsList.push(defObj);
  85. if ($.isFunction(funcData.done)) {
  86. defObj.done(funcData.done);
  87. }
  88. if ($.isFunction(funcData.fail)) {
  89. defObj.fail(funcData.fail);
  90. }
  91. if ($.isFunction(funcData.always)) {
  92. defObj.always(funcData.always);
  93. }
  94. if (this.hasNext()) {
  95. var self = this;
  96. defObj.always(function(){
  97. _.defer($.proxy(function(){
  98. self.launchNext();
  99. }, self));
  100. });
  101. } else {
  102. if (!this.callbackCalled) {
  103. this.callbackCalled = true;
  104. if ($.isFunction(this.callback)) {
  105. $.when.apply($, this.deferredsList)
  106. .always($.proxy(function(){
  107. this.callback();
  108. }, this)
  109. );
  110. }
  111. }
  112. }
  113. return defObj;
  114. }
  115. }
  116. return false;
  117. };
  118. };
  119. if (!OCA.Files_External) {
  120. OCA.Files_External = {};
  121. }
  122. if (!OCA.Files_External.StatusManager) {
  123. OCA.Files_External.StatusManager = {};
  124. }
  125. OCA.Files_External.StatusManager.RollingQueue = RollingQueue;
  126. })();