Commit f6bc2704 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

Files app: ES6 class for metadata_cache_item.js, function_sequence.js and function_parallel.js

Bug: 778674
Change-Id: I37b5d58f5f3b5d7cb2f30ada3e383c655ec69978
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1750464
Auto-Submit: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarAustin Tankiang <austinct@chromium.org>
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Cr-Commit-Position: refs/heads/master@{#686328}
parent ad34509a
......@@ -3,79 +3,81 @@
// found in the LICENSE file.
/**
* @class FunctionSequence to invoke steps in sequence
*
* @param {string} name Name of the function.
* @param {Array<Function>} steps Array of functions to invoke in parallel.
* @param {MetadataParser} logger Logger object.
* @param {function()} callback Callback to invoke on success.
* @param {function(string)} failureCallback Callback to invoke on failure.
* @constructor
* @struct
* @class FunctionParallel to invoke steps in parallel.
*/
function FunctionParallel(name, steps, logger, callback, failureCallback) {
// Private variables hidden in closure
this.currentStepIdx_ = -1;
this.failed_ = false;
this.steps_ = steps;
this.callback_ = callback;
this.failureCallback_ = failureCallback;
this.logger = logger;
this.name = name;
class FunctionParallel {
/**
* @param {string} name Name of the function.
* @param {Array<Function>} steps Array of functions to invoke in parallel.
* @param {MetadataParser} logger Logger object.
* @param {function()} callback Callback to invoke on success.
* @param {function(string)} failureCallback Callback to invoke on failure.
*/
constructor(name, steps, logger, callback, failureCallback) {
// Private variables hidden in closure
this.currentStepIdx_ = -1;
this.failed_ = false;
this.steps_ = steps;
this.callback_ = callback;
this.failureCallback_ = failureCallback;
this.logger = logger;
this.name = name;
this.remaining = this.steps_.length;
this.remaining = this.steps_.length;
this.nextStep = this.nextStep_.bind(this);
this.onError = this.onError_.bind(this);
this.apply = this.start.bind(this);
}
/**
* Error handling function, which fires error callback.
*
* @param {string} err Error message.
* @private
*/
FunctionParallel.prototype.onError_ = function(err) {
if (!this.failed_) {
this.failed_ = true;
this.failureCallback_(err);
this.nextStep = this.nextStep_.bind(this);
this.onError = this.onError_.bind(this);
this.apply = this.start.bind(this);
}
};
/**
* Advances to next step. This method should not be used externally. In external
* cases should be used nextStep function, which is defined in closure and thus
* has access to internal variables of functionsequence.
*
* @private
*/
FunctionParallel.prototype.nextStep_ = function() {
if (--this.remaining == 0 && !this.failed_) {
this.callback_();
/**
* Error handling function, which fires error callback.
*
* @param {string} err Error message.
* @private
*/
onError_(err) {
if (!this.failed_) {
this.failed_ = true;
this.failureCallback_(err);
}
}
};
/**
* This function should be called only once on start, so start all the children
* at once
* @param {...} var_args Arguments to be passed to all the steps.
*/
FunctionParallel.prototype.start = function(var_args) {
this.logger.vlog(
'Starting [' + this.steps_.length + '] parallel tasks ' +
'with ' + arguments.length + ' argument(s)');
if (this.logger.verbose) {
for (let j = 0; j < arguments.length; j++) {
this.logger.vlog(arguments[j]);
/**
* Advances to next step. This method should not be used externally. In
* external cases should be used nextStep function, which is defined in
* closure and thus has access to internal variables of functionsequence.
*
* @private
*/
nextStep_() {
if (--this.remaining == 0 && !this.failed_) {
this.callback_();
}
}
for (let i = 0; i < this.steps_.length; i++) {
this.logger.vlog('Attempting to start step [' + this.steps_[i].name + ']');
try {
this.steps_[i].apply(this, arguments);
} catch (e) {
this.onError(e.toString());
/**
* This function should be called only once on start, so start all the
* children at once
* @param {...} var_args Arguments to be passed to all the steps.
*/
start(var_args) {
this.logger.vlog(
'Starting [' + this.steps_.length + '] parallel tasks ' +
'with ' + arguments.length + ' argument(s)');
if (this.logger.verbose) {
for (let j = 0; j < arguments.length; j++) {
this.logger.vlog(arguments[j]);
}
}
for (let i = 0; i < this.steps_.length; i++) {
this.logger.vlog(
'Attempting to start step [' + this.steps_[i].name + ']');
try {
this.steps_[i].apply(this, arguments);
} catch (e) {
this.onError(e.toString());
}
}
}
};
}
......@@ -3,129 +3,135 @@
// found in the LICENSE file.
/**
* @class FunctionSequence to invoke steps in sequence
*
* @param {string} name Name of the function.
* @param {Array} steps Array of functions to invoke in sequence.
* @param {MetadataParser} logger Logger object.
* @param {function()} callback Callback to invoke on success.
* @param {function(string)} failureCallback Callback to invoke on failure.
* @constructor
* @class FunctionSequence to invoke steps in sequence.
*/
function FunctionSequence(name, steps, logger, callback, failureCallback) {
// Private variables hidden in closure
this.currentStepIdx_ = -1;
this.failed_ = false;
this.steps_ = steps;
this.callback_ = callback;
this.failureCallback_ = failureCallback;
this.logger = logger;
this.name = name;
class FunctionSequence {
/**
* @param {string} name Name of the function.
* @param {Array} steps Array of functions to invoke in sequence.
* @param {MetadataParser} logger Logger object.
* @param {function()} callback Callback to invoke on success.
* @param {function(string)} failureCallback Callback to invoke on failure.
*/
constructor(name, steps, logger, callback, failureCallback) {
// Private variables hidden in closure
this.currentStepIdx_ = -1;
this.failed_ = false;
this.steps_ = steps;
this.callback_ = callback;
this.failureCallback_ = failureCallback;
this.logger = logger;
this.name = name;
/** @public {boolean} */
this.started = false;
this.onError = this.onError_.bind(this);
this.finish = this.finish_.bind(this);
this.nextStep = this.nextStep_.bind(this);
this.apply = this.apply_.bind(this);
}
this.onError = this.onError_.bind(this);
this.finish = this.finish_.bind(this);
this.nextStep = this.nextStep_.bind(this);
this.apply = this.apply_.bind(this);
}
/**
* Sets new callback
*
* @param {function()} callback New callback to call on succeed.
*/
FunctionSequence.prototype.setCallback = function(callback) {
this.callback_ = callback;
};
/**
* Sets new callback
*
* @param {function()} callback New callback to call on succeed.
*/
setCallback(callback) {
this.callback_ = callback;
}
/**
* Sets new error callback
*
* @param {function(string)} failureCallback New callback to call on failure.
*/
FunctionSequence.prototype.setFailureCallback = function(failureCallback) {
this.failureCallback_ = failureCallback;
};
/**
* Sets new error callback
*
* @param {function(string)} failureCallback New callback to call on failure.
*/
setFailureCallback(failureCallback) {
this.failureCallback_ = failureCallback;
}
/**
* Error handling function, which traces current error step, stops sequence
* advancing and fires error callback.
*
* @param {string} err Error message.
* @private
*/
FunctionSequence.prototype.onError_ = function(err) {
this.logger.vlog(
'Failed step: ' + this.steps_[this.currentStepIdx_].name + ': ' + err);
if (!this.failed_) {
this.failed_ = true;
this.failureCallback_(err);
/**
* Error handling function, which traces current error step, stops sequence
* advancing and fires error callback.
*
* @param {string} err Error message.
* @private
*/
onError_(err) {
this.logger.vlog(
'Failed step: ' + this.steps_[this.currentStepIdx_].name + ': ' + err);
if (!this.failed_) {
this.failed_ = true;
this.failureCallback_(err);
}
}
};
/**
* Finishes sequence processing and jumps to the last step.
* This method should not be used externally. In external
* cases should be used finish function, which is defined in closure and thus
* has access to internal variables of functionsequence.
* @private
*/
FunctionSequence.prototype.finish_ = function() {
if (!this.failed_ && this.currentStepIdx_ < this.steps_.length) {
this.currentStepIdx_ = this.steps_.length;
this.callback_();
/**
* Finishes sequence processing and jumps to the last step.
* This method should not be used externally. In external
* cases should be used finish function, which is defined in closure and thus
* has access to internal variables of functionsequence.
* @private
*/
finish_() {
if (!this.failed_ && this.currentStepIdx_ < this.steps_.length) {
this.currentStepIdx_ = this.steps_.length;
this.callback_();
}
}
};
/**
* Advances to next step.
* This method should not be used externally. In external
* cases should be used nextStep function, which is defined in closure and thus
* has access to internal variables of functionsequence.
* @param {...} var_args Arguments to be passed to the next step.
* @private
*/
FunctionSequence.prototype.nextStep_ = function(var_args) {
if (this.failed_) {
return;
}
/**
* Advances to next step.
* This method should not be used externally. In external
* cases should be used nextStep function, which is defined in closure and
* thus has access to internal variables of functionsequence.
* @param {...} var_args Arguments to be passed to the next step.
* @private
*/
nextStep_(var_args) {
if (this.failed_) {
return;
}
if (++this.currentStepIdx_ >= this.steps_.length) {
this.logger.vlog('Sequence ended');
this.callback_.apply(this, arguments);
} else {
this.logger.vlog(
'Attempting to start step [' + this.steps_[this.currentStepIdx_].name +
']');
try {
this.steps_[this.currentStepIdx_].apply(this, arguments);
} catch (e) {
this.onError(e.toString());
if (++this.currentStepIdx_ >= this.steps_.length) {
this.logger.vlog('Sequence ended');
this.callback_.apply(this, arguments);
} else {
this.logger.vlog(
'Attempting to start step [' +
this.steps_[this.currentStepIdx_].name + ']');
try {
this.steps_[this.currentStepIdx_].apply(this, arguments);
} catch (e) {
this.onError(e.toString());
}
}
}
};
/**
* This function should be called only once on start, so start sequence pipeline
* @param {...} var_args Arguments to be passed to the first step.
*/
FunctionSequence.prototype.start = function(var_args) {
if (this.started) {
throw new Error('"Start" method of FunctionSequence was called twice');
}
/**
* This function should be called only once on start, so start sequence
* pipeline
* @param {...} var_args Arguments to be passed to the first step.
*/
start(var_args) {
if (this.started) {
throw new Error('"Start" method of FunctionSequence was called twice');
}
this.logger.log('Starting sequence with ' + arguments.length + ' arguments');
this.logger.log(
'Starting sequence with ' + arguments.length + ' arguments');
this.started = true;
this.nextStep.apply(this, arguments);
};
this.started = true;
this.nextStep.apply(this, arguments);
}
/**
* Add Function object mimics to FunctionSequence
* @private
* @param {*} obj Object.
* @param {Array} args Arguments.
*/
FunctionSequence.prototype.apply_ = function(obj, args) {
this.start.apply(this, args);
};
/**
* Add Function object mimics to FunctionSequence
* @private
* @param {*} obj Object.
* @param {Array} args Arguments.
*/
apply_(obj, args) {
this.start.apply(this, args);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment