| 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 {Array} steps array of functions to invoke in sequence. | 9 * @param {Array} steps array of functions to invoke in sequence. |
| 10 * @param {Object} logger logger. | 10 * @param {Object} logger logger. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 */ | 43 */ |
| 44 FunctionSequence.prototype.setFailureCallback = function(failureCallback) { | 44 FunctionSequence.prototype.setFailureCallback = function(failureCallback) { |
| 45 this.failureCallback_ = failureCallback; | 45 this.failureCallback_ = failureCallback; |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * Error handling function, which traces current error step, stops sequence | 50 * Error handling function, which traces current error step, stops sequence |
| 51 * advancing and fires error callback. | 51 * advancing and fires error callback. |
| 52 * | 52 * |
| 53 * @private |
| 53 * @param err error message. | 54 * @param err error message. |
| 54 */ | 55 */ |
| 55 FunctionSequence.prototype.onError_ = function(err) { | 56 FunctionSequence.prototype.onError_ = function(err) { |
| 56 this.logger.vlog('Failed step: ' + this.steps_[this.currentStepIdx_].name + | 57 this.logger.vlog('Failed step: ' + this.steps_[this.currentStepIdx_].name + |
| 57 ': ' + err); | 58 ': ' + err); |
| 58 if (!this.failed_) { | 59 if (!this.failed_) { |
| 59 this.failed_ = true; | 60 this.failed_ = true; |
| 60 this.failureCallback_(err); | 61 this.failureCallback_(err); |
| 61 } | 62 } |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 /** | 65 /** |
| 65 * Finishes sequence processing and jumps to the last step. | 66 * Finishes sequence processing and jumps to the last step. |
| 66 * This method should not be used externally. In external | 67 * This method should not be used externally. In external |
| 67 * cases should be used finish function, which is defined in closure and thus | 68 * cases should be used finish function, which is defined in closure and thus |
| 68 * has access to internal variables of functionsequence. | 69 * has access to internal variables of functionsequence. |
| 70 * @private |
| 69 */ | 71 */ |
| 70 FunctionSequence.prototype.finish_ = function() { | 72 FunctionSequence.prototype.finish_ = function() { |
| 71 if (!this.failed_ && this.currentStepIdx_ < this.steps_.length) { | 73 if (!this.failed_ && this.currentStepIdx_ < this.steps_.length) { |
| 72 this.currentStepIdx_ = this.steps_.length; | 74 this.currentStepIdx_ = this.steps_.length; |
| 73 this.callback_(); | 75 this.callback_(); |
| 74 } | 76 } |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 /** | 79 /** |
| 78 * Advances to next step. | 80 * Advances to next step. |
| 79 * This method should not be used externally. In external | 81 * This method should not be used externally. In external |
| 80 * cases should be used nextStep function, which is defined in closure and thus | 82 * cases should be used nextStep function, which is defined in closure and thus |
| 81 * has access to internal variables of functionsequence. | 83 * has access to internal variables of functionsequence. |
| 84 * @private |
| 82 */ | 85 */ |
| 83 FunctionSequence.prototype.nextStep_ = function(var_args) { | 86 FunctionSequence.prototype.nextStep_ = function(var_args) { |
| 84 if (this.failed_) { | 87 if (this.failed_) { |
| 85 return; | 88 return; |
| 86 } | 89 } |
| 87 | 90 |
| 88 if (++this.currentStepIdx_ >= this.steps_.length) { | 91 if (++this.currentStepIdx_ >= this.steps_.length) { |
| 89 this.logger.vlog('Sequence ended'); | 92 this.logger.vlog('Sequence ended'); |
| 90 this.callback_.apply(this, arguments); | 93 this.callback_.apply(this, arguments); |
| 91 } else { | 94 } else { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 109 } | 112 } |
| 110 | 113 |
| 111 this.logger.log('Starting sequence with ' + arguments.length + ' arguments'); | 114 this.logger.log('Starting sequence with ' + arguments.length + ' arguments'); |
| 112 | 115 |
| 113 this.started = true; | 116 this.started = true; |
| 114 this.nextStep.apply(this, arguments); | 117 this.nextStep.apply(this, arguments); |
| 115 }; | 118 }; |
| 116 | 119 |
| 117 /** | 120 /** |
| 118 * Add Function object mimics to FunctionSequence | 121 * Add Function object mimics to FunctionSequence |
| 122 * @private |
| 119 */ | 123 */ |
| 120 FunctionSequence.prototype.apply_ = function(obj, args) { | 124 FunctionSequence.prototype.apply_ = function(obj, args) { |
| 121 this.start.apply(this, args); | 125 this.start.apply(this, args); |
| 122 }; | 126 }; |
| 123 | |
| OLD | NEW |