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