action_sequence.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * Utility to define sequence of actions (sync or async either) and execute them in exact order
  20. * methods executed in order that described in sequence
  21. * each action obtain result of previous action <code>previousResponse</code>
  22. * For example:
  23. * loadMap: {
  24. * '0': {
  25. * type: 'async',
  26. * callback: function (previousResponse) {
  27. * //method1
  28. * }
  29. * },
  30. * '1': {
  31. * type: 'sync',
  32. * callback: function (previousResponse) {
  33. * //method2
  34. * }
  35. * }
  36. * }
  37. * However method1 is async, method2 will be executed only after method1 is completed
  38. * @type {*}
  39. */
  40. App.actionSequence = Em.Object.extend({
  41. /**
  42. * List of actions which will be executed
  43. * format:
  44. * [
  45. * {
  46. * type: 'async',
  47. * callback: function(previousResponse) {
  48. * //async method should return Deferred object to continue sequence
  49. * }
  50. * },
  51. * {
  52. * type: 'sync',
  53. * callback: function(previousResponse) {
  54. * //code
  55. * }
  56. * }
  57. * ]
  58. * @type {Object[]}
  59. */
  60. sequence: [],
  61. /**
  62. * specific context for methods executed in actions
  63. */
  64. context: this,
  65. /**
  66. * Function called after sequence is completed
  67. * @type {callback}
  68. */
  69. finishedCallback: Em.K,
  70. /**
  71. * counter of actions
  72. */
  73. actionCounter: 0,
  74. /**
  75. * set sequence
  76. * @param sequence
  77. * @return {object}
  78. */
  79. setSequence: function (sequence) {
  80. if (Array.isArray(sequence)) {
  81. this.set('sequence', sequence);
  82. } else {
  83. console.log('ERROR: passed sequence has incorrect type');
  84. }
  85. return this;
  86. },
  87. /**
  88. * start executing sequence of actions
  89. */
  90. start: function () {
  91. this.set('actionCounter', this.get('sequence.length'));
  92. this.runNextAction(0, null);
  93. },
  94. /**
  95. * set <code>finishedCallback</code>
  96. * @param callback
  97. * @return {object}
  98. */
  99. onFinish: function (callback) {
  100. if (typeof(callback) === 'function') {
  101. this.set('finishedCallback', callback);
  102. }
  103. return this;
  104. },
  105. /**
  106. * run next action in sequence
  107. * @param index
  108. * @param previousResponse
  109. * @return {Boolean}
  110. */
  111. runNextAction: function (index, previousResponse) {
  112. var action = this.get('sequence')[index];
  113. var context = this.get('context');
  114. var self = this;
  115. index++;
  116. this.decrementProperty('actionCounter');
  117. if (this.get('actionCounter') >= 0 && !Em.isNone(action)) {
  118. if (action.type === 'sync') {
  119. this.runNextAction(index, action.callback.call(context, previousResponse));
  120. return true;
  121. } else if (action.type === 'async') {
  122. action.callback.call(context, previousResponse).done(function (response) {
  123. self.runNextAction(index, response);
  124. });
  125. return true;
  126. } else {
  127. console.log('WARNING: action has incorrect type, action skipped');
  128. }
  129. } else {
  130. //if no more actions left then finish sequence
  131. this.finishedCallback();
  132. }
  133. return false;
  134. }
  135. });