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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 * @param err error message | 53 * @param err error message |
54 */ | 54 */ |
55 FunctionSequence.prototype.onError_ = function(err) { | 55 FunctionSequence.prototype.onError_ = function(err) { |
56 this.logger.vlog('Failed step: ' + this.steps_[this.currentStepIdx_].name | 56 this.logger.vlog('Failed step: ' + this.steps_[this.currentStepIdx_].name + |
57 + ': ' | 57 ': ' + err); |
58 + err); | |
59 if (!this.failed_) { | 58 if (!this.failed_) { |
60 this.failed_ = true; | 59 this.failed_ = true; |
61 this.failureCallback_(err); | 60 this.failureCallback_(err); |
62 } | 61 } |
63 }; | 62 }; |
64 | 63 |
65 /** | 64 /** |
66 * Finishes sequence processing and jumps to the last step. | 65 * Finishes sequence processing and jumps to the last step. |
67 * This method should not be used externally. In external | 66 * This method should not be used externally. In external |
68 * cases should be used finish function, which is defined in closure and thus | 67 * cases should be used finish function, which is defined in closure and thus |
(...skipping 14 matching lines...) Expand all Loading... |
83 */ | 82 */ |
84 FunctionSequence.prototype.nextStep_ = function(var_args) { | 83 FunctionSequence.prototype.nextStep_ = function(var_args) { |
85 if (this.failed_) { | 84 if (this.failed_) { |
86 return; | 85 return; |
87 } | 86 } |
88 | 87 |
89 if (++this.currentStepIdx_ >= this.steps_.length) { | 88 if (++this.currentStepIdx_ >= this.steps_.length) { |
90 this.logger.vlog('Sequence ended'); | 89 this.logger.vlog('Sequence ended'); |
91 this.callback_.apply(this, arguments); | 90 this.callback_.apply(this, arguments); |
92 } else { | 91 } else { |
93 this.logger.vlog('Attempting to start step [' | 92 this.logger.vlog('Attempting to start step [' + |
94 + this.steps_[this.currentStepIdx_].name | 93 this.steps_[this.currentStepIdx_].name + |
95 + ']'); | 94 ']'); |
96 try { | 95 try { |
97 this.steps_[this.currentStepIdx_].apply(this, arguments); | 96 this.steps_[this.currentStepIdx_].apply(this, arguments); |
98 } catch(e) { | 97 } catch (e) { |
99 this.onError(e.toString()); | 98 this.onError(e.toString()); |
100 } | 99 } |
101 } | 100 } |
102 }; | 101 }; |
103 | 102 |
104 /** | 103 /** |
105 * This function should be called only once on start, so start sequence pipeline | 104 * This function should be called only once on start, so start sequence pipeline |
106 */ | 105 */ |
107 FunctionSequence.prototype.start = function(var_args) { | 106 FunctionSequence.prototype.start = function(var_args) { |
108 if (this.started) { | 107 if (this.started) { |
109 throw new Error('"Start" method of FunctionSequence was called twice'); | 108 throw new Error('"Start" method of FunctionSequence was called twice'); |
110 } | 109 } |
111 | 110 |
112 this.logger.log("Starting sequence with " + arguments.length + " arguments"); | 111 this.logger.log('Starting sequence with ' + arguments.length + ' arguments'); |
113 | 112 |
114 this.started = true; | 113 this.started = true; |
115 this.nextStep.apply(this, arguments); | 114 this.nextStep.apply(this, arguments); |
116 }; | 115 }; |
117 | 116 |
118 /** | 117 /** |
119 * Add Function object mimics to FunctionSequence | 118 * Add Function object mimics to FunctionSequence |
120 */ | 119 */ |
121 FunctionSequence.prototype.apply_ = function(obj, args) { | 120 FunctionSequence.prototype.apply_ = function(obj, args) { |
122 this.start.apply(this, args); | 121 this.start.apply(this, args); |
123 }; | 122 }; |
124 | 123 |
OLD | NEW |