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 | |
7 * @class FunctionSequence to invoke steps in sequence | 6 * @class FunctionSequence to invoke steps in sequence |
8 * | 7 * |
9 * @param {string} name //TODO(JSDOC). | 8 * @param {string} name //TODO(JSDOC). |
10 * @param steps array of functions to invoke in parallel. | 9 * @param {Array.<function>} steps Array of functions to invoke in parallel. |
11 * @param {Object} logger //TODO(JSDOC). | 10 * @param {Object} logger //TODO(JSDOC). |
12 * @param callback callback to invoke on success. | 11 * @param {function()} callback Callback to invoke on success. |
13 * @param failureCallback callback to invoke on failure. | 12 * @param {function(string)} failureCallback Callback to invoke on failure. |
| 13 * @constructor |
14 */ | 14 */ |
15 function FunctionParallel(name, steps, logger, callback, failureCallback) { | 15 function FunctionParallel(name, steps, logger, callback, failureCallback) { |
16 // Private variables hidden in closure | 16 // Private variables hidden in closure |
17 this.currentStepIdx_ = -1; | 17 this.currentStepIdx_ = -1; |
18 this.failed_ = false; | 18 this.failed_ = false; |
19 this.steps_ = steps; | 19 this.steps_ = steps; |
20 this.callback_ = callback; | 20 this.callback_ = callback; |
21 this.failureCallback_ = failureCallback; | 21 this.failureCallback_ = failureCallback; |
22 this.logger = logger; | 22 this.logger = logger; |
23 this.name = name; | 23 this.name = name; |
24 | 24 |
25 this.remaining = this.steps_.length; | 25 this.remaining = this.steps_.length; |
26 | 26 |
27 this.nextStep = this.nextStep_.bind(this); | 27 this.nextStep = this.nextStep_.bind(this); |
28 this.onError = this.onError_.bind(this); | 28 this.onError = this.onError_.bind(this); |
29 this.apply = this.start.bind(this); | 29 this.apply = this.start.bind(this); |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 /** | 33 /** |
34 * Error handling function, which fires error callback. | 34 * Error handling function, which fires error callback. |
35 * | 35 * |
| 36 * @param {string} err Error message |
36 * @private | 37 * @private |
37 * @param err error message. | |
38 */ | 38 */ |
39 FunctionParallel.prototype.onError_ = function(err) { | 39 FunctionParallel.prototype.onError_ = function(err) { |
40 if (!this.failed_) { | 40 if (!this.failed_) { |
41 this.failed_ = true; | 41 this.failed_ = true; |
42 this.failureCallback_(err); | 42 this.failureCallback_(err); |
43 } | 43 } |
44 }; | 44 }; |
45 | 45 |
46 /** | 46 /** |
47 * Advances to next step. This method should not be used externally. In external | 47 * Advances to next step. This method should not be used externally. In external |
48 * cases should be used nextStep function, which is defined in closure and thus | 48 * cases should be used nextStep function, which is defined in closure and thus |
49 * has access to internal variables of functionsequence. | 49 * has access to internal variables of functionsequence. |
| 50 * |
50 * @private | 51 * @private |
51 */ | 52 */ |
52 FunctionParallel.prototype.nextStep_ = function() { | 53 FunctionParallel.prototype.nextStep_ = function() { |
53 if (--this.remaining == 0 && !this.failed_) { | 54 if (--this.remaining == 0 && !this.failed_) { |
54 this.callback_(); | 55 this.callback_(); |
55 } | 56 } |
56 }; | 57 }; |
57 | 58 |
58 /** | 59 /** |
59 * This function should be called only once on start, so start all the children | 60 * This function should be called only once on start, so start all the children |
(...skipping 10 matching lines...) Expand all Loading... |
70 } | 71 } |
71 for (var i = 0; i < this.steps_.length; i++) { | 72 for (var i = 0; i < this.steps_.length; i++) { |
72 this.logger.vlog('Attempting to start step [' + this.steps_[i].name + ']'); | 73 this.logger.vlog('Attempting to start step [' + this.steps_[i].name + ']'); |
73 try { | 74 try { |
74 this.steps_[i].apply(this, arguments); | 75 this.steps_[i].apply(this, arguments); |
75 } catch (e) { | 76 } catch (e) { |
76 this.onError(e.toString()); | 77 this.onError(e.toString()); |
77 } | 78 } |
78 } | 79 } |
79 }; | 80 }; |
OLD | NEW |