| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @constructor | 6 * @constructor |
| 7 * @class FunctionSequence to invoke steps in sequence | 7 * @class FunctionSequence to invoke steps in sequence |
| 8 * | 8 * |
| 9 * @param {string} name //TODO(JSDOC). |
| 9 * @param {Array} steps array of functions to invoke in sequence. | 10 * @param {Array} steps array of functions to invoke in sequence. |
| 10 * @param {Object} logger logger. | 11 * @param {Object} logger logger. |
| 11 * @param {Function} callback callback to invoke on success. | 12 * @param {Function} callback callback to invoke on success. |
| 12 * @param {Function} failureCallback callback to invoke on failure. | 13 * @param {Function} failureCallback callback to invoke on failure. |
| 13 */ | 14 */ |
| 14 function FunctionSequence(name, steps, logger, callback, failureCallback) { | 15 function FunctionSequence(name, steps, logger, callback, failureCallback) { |
| 15 // Private variables hidden in closure | 16 // Private variables hidden in closure |
| 16 this.currentStepIdx_ = -1; | 17 this.currentStepIdx_ = -1; |
| 17 this.failed_ = false; | 18 this.failed_ = false; |
| 18 this.steps_ = steps; | 19 this.steps_ = steps; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 this.callback_(); | 76 this.callback_(); |
| 76 } | 77 } |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 /** | 80 /** |
| 80 * Advances to next step. | 81 * Advances to next step. |
| 81 * This method should not be used externally. In external | 82 * This method should not be used externally. In external |
| 82 * cases should be used nextStep function, which is defined in closure and thus | 83 * cases should be used nextStep function, which is defined in closure and thus |
| 83 * has access to internal variables of functionsequence. | 84 * has access to internal variables of functionsequence. |
| 84 * @private | 85 * @private |
| 86 * @param {...} var_args //TODO(JSDOC). |
| 85 */ | 87 */ |
| 86 FunctionSequence.prototype.nextStep_ = function(var_args) { | 88 FunctionSequence.prototype.nextStep_ = function(var_args) { |
| 87 if (this.failed_) { | 89 if (this.failed_) { |
| 88 return; | 90 return; |
| 89 } | 91 } |
| 90 | 92 |
| 91 if (++this.currentStepIdx_ >= this.steps_.length) { | 93 if (++this.currentStepIdx_ >= this.steps_.length) { |
| 92 this.logger.vlog('Sequence ended'); | 94 this.logger.vlog('Sequence ended'); |
| 93 this.callback_.apply(this, arguments); | 95 this.callback_.apply(this, arguments); |
| 94 } else { | 96 } else { |
| 95 this.logger.vlog('Attempting to start step [' + | 97 this.logger.vlog('Attempting to start step [' + |
| 96 this.steps_[this.currentStepIdx_].name + | 98 this.steps_[this.currentStepIdx_].name + |
| 97 ']'); | 99 ']'); |
| 98 try { | 100 try { |
| 99 this.steps_[this.currentStepIdx_].apply(this, arguments); | 101 this.steps_[this.currentStepIdx_].apply(this, arguments); |
| 100 } catch (e) { | 102 } catch (e) { |
| 101 this.onError(e.toString()); | 103 this.onError(e.toString()); |
| 102 } | 104 } |
| 103 } | 105 } |
| 104 }; | 106 }; |
| 105 | 107 |
| 106 /** | 108 /** |
| 107 * This function should be called only once on start, so start sequence pipeline | 109 * This function should be called only once on start, so start sequence pipeline |
| 110 * @param {...} var_args //TODO(JSDOC). |
| 111 */ |
| 108 */ | 112 */ |
| 109 FunctionSequence.prototype.start = function(var_args) { | 113 FunctionSequence.prototype.start = function(var_args) { |
| 110 if (this.started) { | 114 if (this.started) { |
| 111 throw new Error('"Start" method of FunctionSequence was called twice'); | 115 throw new Error('"Start" method of FunctionSequence was called twice'); |
| 112 } | 116 } |
| 113 | 117 |
| 114 this.logger.log('Starting sequence with ' + arguments.length + ' arguments'); | 118 this.logger.log('Starting sequence with ' + arguments.length + ' arguments'); |
| 115 | 119 |
| 116 this.started = true; | 120 this.started = true; |
| 117 this.nextStep.apply(this, arguments); | 121 this.nextStep.apply(this, arguments); |
| 118 }; | 122 }; |
| 119 | 123 |
| 120 /** | 124 /** |
| 121 * Add Function object mimics to FunctionSequence | 125 * Add Function object mimics to FunctionSequence |
| 122 * @private | 126 * @private |
| 127 * @param {*} obj //TODO(JSDOC). |
| 128 * @param {Array.*} args /TODO(JSDOC). |
| 123 */ | 129 */ |
| 124 FunctionSequence.prototype.apply_ = function(obj, args) { | 130 FunctionSequence.prototype.apply_ = function(obj, args) { |
| 125 this.start.apply(this, args); | 131 this.start.apply(this, args); |
| 126 }; | 132 }; |
| OLD | NEW |