src/js/algorithm/algorithm-result.js
/**
* Stores the results generated by running an Algorithm. An instance will be stored in each Algorithm object.
* @class AlgorithmResult
*/
class AlgorithmResult {
/**
* Array of Step objects.
* @type {Array.<Step>}
*/
timeline;
/**
* Current index in the timeline.
* @type {number}
*/
stepIndex;
/**
* Constructs an instance of AlgorithmResult.
* @param {Array.<string>} nodeFields - Array of fields to store changes for in Node objects.
* @param {Array.<string>} edgeFields - Array of fields to store changes for in Edge objects.
* @constructs AlgorithmResult
*/
constructor() {
this.timeline = [];
this.stepIndex = -1;
}
/**
* Move to the next step in the timeline. Does nothing if already at the end.
*/
stepForward() {
if (this.stepIndex === this.timeline.length) {
return;
}
if (this.stepIndex > -1) {
let finishingStep = this.timeline[this.stepIndex];
finishingStep.applyPost();
}
if (this.stepIndex < this.timeline.length - 1) {
let nextStep = this.timeline[this.stepIndex + 1];
nextStep.applyDuring();
this.stepIndex++;
return;
}
this.stepIndex++;
}
/**
* Move to the previous step in the timeline. Does nothing if already at the beginning.
*/
stepBackward() {
if (this.stepIndex === -1) {
return;
}
if (this.stepIndex < this.timeline.length) {
let redactingStep = this.timeline[this.stepIndex];
redactingStep.applyPre();
}
if (this.stepIndex > 0) {
let previousStep = this.timeline[this.stepIndex - 1];
previousStep.applyDuring();
this.stepIndex--;
return;
}
this.stepIndex--;
}
/**
* Add a Step to the AlgorithmResult.
* @param {Step} step - The step to add.
*/
addStep(step) {
this.timeline.push(step);
}
}
export { AlgorithmResult };
export default AlgorithmResult;