wizardDetectorQueue.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. OCA = OCA || {};
  6. (function() {
  7. /**
  8. * @classdesc only run detector is allowed to run at a time. Basically
  9. * because we cannot have parallel LDAP connections per session. This
  10. * queue is takes care of running all the detectors one after the other.
  11. *
  12. * @constructor
  13. */
  14. var WizardDetectorQueue = OCA.LDAP.Wizard.WizardObject.subClass({
  15. /**
  16. * initializes the instance. Always call it after creating the instance.
  17. */
  18. init: function() {
  19. this.queue = [];
  20. this.isRunning = false;
  21. },
  22. /**
  23. * empties the queue and cancels a possibly running request
  24. */
  25. reset: function() {
  26. this.queue = [];
  27. if(!_.isUndefined(this.runningRequest)) {
  28. this.runningRequest.abort();
  29. delete this.runningRequest;
  30. }
  31. this.isRunning = false;
  32. },
  33. /**
  34. * a parameter-free callback that eventually executes the run method of
  35. * the detector.
  36. *
  37. * @callback detectorCallBack
  38. * @see OCA.LDAP.Wizard.ConfigModel._processSetResult
  39. */
  40. /**
  41. * adds a detector to the queue and attempts to trigger to run the
  42. * next job, because it might be the first.
  43. *
  44. * @param {detectorCallBack} callback
  45. */
  46. add: function(callback) {
  47. this.queue.push(callback);
  48. this.next();
  49. },
  50. /**
  51. * Executes the next detector if none is running. This method is also
  52. * automatically invoked after a detector finished.
  53. */
  54. next: function() {
  55. if(this.isRunning === true || this.queue.length === 0) {
  56. return;
  57. }
  58. this.isRunning = true;
  59. var callback = this.queue.shift();
  60. var request = callback();
  61. // we receive either false or a jqXHR object
  62. // false in case the detector decided against executing
  63. if(request === false) {
  64. this.isRunning = false;
  65. this.next();
  66. return;
  67. }
  68. this.runningRequest = request;
  69. var detectorQueue = this;
  70. $.when(request).then(function() {
  71. detectorQueue.isRunning = false;
  72. detectorQueue.next();
  73. });
  74. }
  75. });
  76. OCA.LDAP.Wizard.WizardDetectorQueue = WizardDetectorQueue;
  77. })();