wizardDetectorQueue.js 2.1 KB

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