Code coverage report for app/utils/action_sequence.js

Statements: 100% (25 / 25)      Branches: 91.67% (11 / 12)      Functions: 100% (6 / 6)      Lines: 100% (25 / 25)      Ignored: none     

All files » app/utils/ » action_sequence.js
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 138    1                                                                                 1                                                                               6 2   6           3 3               6 2   6                 46 46 46   46 46   46 30 27 27 3 3 3   3       16   16      
'use strict';
 
;require.register("utils/action_sequence", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  /**
   * Utility to define sequence of actions (sync or async either) and execute them in exact order
   * methods executed in order that described in sequence
   * each action obtain result of previous action <code>previousResponse</code>
   * For example:
   * loadMap: {
   *  '0': {
   *   type: 'async',
   *   callback: function (previousResponse) {
   *     //method1
   *   }
   *  },
   *  '1': {
   *    type: 'sync',
   *    callback: function (previousResponse) {
   *      //method2
   *    }
   *  }
   * }
   * However method1 is async, method2 will be executed only after method1 is completed
   * @type {*}
   */
  App.actionSequence = Em.Object.extend({
    /**
     * List of actions which will be executed
     * format:
     * [
     *   {
     *     type: 'async',
     *     callback: function(previousResponse) {
     *       //async method should return Deferred object to continue sequence
     *     }
     *   },
     *   {
     *     type: 'sync',
     *     callback: function(previousResponse) {
     *       //code
     *     }
     *   }
     * ]
     * @type {Object[]}
     */
    sequence: [],
    /**
     * specific context for methods executed in actions
     */
    context: this,
    /**
     * Function called after sequence is completed
     * @type {callback}
     */
    finishedCallback: Em.K,
    /**
     * counter of actions
     */
    actionCounter: 0,
    /**
     * set sequence
     * @param sequence
     * @return {object}
     */
    setSequence: function setSequence(sequence) {
      if (Array.isArray(sequence)) {
        this.set('sequence', sequence);
      }
      return this;
    },
    /**
     * start executing sequence of actions
     */
    start: function start() {
      this.set('actionCounter', this.get('sequence.length'));
      this.runNextAction(0, null);
    },
    /**
     * set <code>finishedCallback</code>
     * @param callback
     * @return {object}
     */
    onFinish: function onFinish(callback) {
      if (typeof callback === 'function') {
        this.set('finishedCallback', callback);
      }
      return this;
    },
    /**
     * run next action in sequence
     * @param index
     * @param previousResponse
     * @return {Boolean}
     */
    runNextAction: function runNextAction(index, previousResponse) {
      var action = this.get('sequence')[index];
      var context = this.get('context');
      var self = this;
 
      index++;
      this.decrementProperty('actionCounter');
 
      if (this.get('actionCounter') >= 0 && !Em.isNone(action)) {
        if (action.type === 'sync') {
          this.runNextAction(index, action.callback.call(context, previousResponse));
          return true;
        } else Eif (action.type === 'async') {
          action.callback.call(context, previousResponse).done(function (response) {
            self.runNextAction(index, response);
          });
          return true;
        }
      } else {
        //if no more actions left then finish sequence
        this.finishedCallback();
      }
      return false;
    }
  });
});