Skip to content

Commit

Permalink
Fix: SubCircuit i/o nodes not consistent with localscope i/o nodes
Browse files Browse the repository at this point in the history
SC - SubCircuit

The update() call was reinstated since it remakes the subcircuit
(and localscope) just before saving. It is important because
it makes sure that SC's i/o is consistent with localscope's i/o
(see comment) and localscope is consistent with it's referenced
actual scope.

We call rebuildCircuit() (thru update) while saving to rebuild SC's
localscope. This function is ONLY called if SC's localscope's
actual circuit's lastUpdated timestamp is after subcircuit's
last updated timestamp. Which means that circuit has changed and
SC's localscope is stale. So localscope is updated.

Another issue was that any scope's timstamp was only updating
during scheduleBackup(). schedBackup() is called at certain
time intervals and user might save circuit after making a change
before a scheduleUpdate is called and so the subcircuit will be
saved with a stale localscope.
  • Loading branch information
gr455 committed Apr 20, 2024
1 parent c31612b commit 4633151
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
15 changes: 9 additions & 6 deletions simulator/src/data/save.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { scopeList } from '../circuit';
import { resetup } from '../setup';
import { update } from '../engine';
import { update, updateSubcircuitSet } from '../engine';
import { stripTags, showMessage } from '../utils';
import { backUp } from './backupCircuit';
import simulationArea from '../simulationArea';
Expand Down Expand Up @@ -105,11 +105,14 @@ export function generateSaveData(name, setName = true) {

completed[id] = true;

// Removed: no such check is required. saveScope should be strictly read only and
// should not change state
// update might change state.

// update(scopeList[id]); // For any pending integrity checks on subcircuits

// This update is very important.
// if a scope's input/output changes and the user saves without going
// to circuits where this circuit is used as a subcircuit. It will
// break the code since the Subcircuit will have different number of
// in/out nodes compared to the localscope input/output objects.
updateSubcircuitSet(true);
update(scopeList[id]); // For any pending integrity checks on subcircuits
data.scopes.push(backUp(scopeList[id]));
}

Expand Down
14 changes: 13 additions & 1 deletion simulator/src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function replace(node, index) {
node.parent = parent;
parent.nodeList.push(node);
node.updateRotation();
node.scope.timeStamp = new Date().getTime();
return node;
}
function rotate(x1, y1, dir) {
Expand Down Expand Up @@ -161,6 +162,7 @@ export default class Node {
this.hover = false;
this.wasClicked = false;
this.scope = this.parent.scope;
this.scope.timeStamp = new Date().getTime();
/**
* @type {string}
* value of this.prev is
Expand Down Expand Up @@ -265,6 +267,7 @@ export default class Node {
for (var i = 0; i < this.connections.length; i++) {
this.connections[i].connections.clean(this);
}
this.scope.timeStamp = new Date().getTime();
this.connections = [];
}

Expand Down Expand Up @@ -315,6 +318,8 @@ export default class Node {
this.connections.push(n);
n.connections.push(this);

this.scope.timeStamp = new Date().getTime();

updateCanvasSet(true);
updateSimulationSet(true);
scheduleUpdate();
Expand All @@ -329,7 +334,9 @@ export default class Node {
this.connections.push(n);
n.connections.push(this);

updateCanvasSet(true);
this.scope.timeStamp = new Date().getTime();

// updateCanvasSet(true);
updateSimulationSet(true);
scheduleUpdate();
}
Expand All @@ -340,6 +347,8 @@ export default class Node {
disconnectWireLess(n) {
this.connections.clean(n);
n.connections.clean(this);

this.scope.timeStamp = new Date().getTime();
}

/**
Expand Down Expand Up @@ -734,6 +743,9 @@ export default class Node {
this.connections[i].connections.clean(this);
this.connections[i].checkDeleted();
}

this.scope.timeStamp = new Date().getTime();

wireToBeCheckedSet(1);
forceResetNodesSet(true);
scheduleUpdate();
Expand Down
11 changes: 5 additions & 6 deletions simulator/src/subcircuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ export default class SubCircuit extends CircuitElement {
}

/**
* rebuilds the subcircuit if any change to localscope is made
* If the circuit referenced by localscope is changed, then the localscope
* needs to be updated. This function does that.
*/
reBuildCircuit() {
this.data = JSON.parse(scheduleBackup(scopeList[this.id]));
Expand Down Expand Up @@ -467,15 +468,13 @@ export default class SubCircuit extends CircuitElement {
this.outputNodes.push(a);
}
}

// console.log(subcircuitScope.name, subcircuitScope.timeStamp, this.lastUpdated)
if (subcircuitScope.timeStamp > this.lastUpdated) {
this.reBuildCircuit();
}

// Should this be done here or only when this.reBuildCircuit() is called?
{
// console.log("rebuild called")
this.localScope.reset();
updateSimulationSet(true);
// Why is forceResetNodes required here?
forceResetNodesSet(true);
}

Expand Down
2 changes: 2 additions & 0 deletions simulator/src/wire.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,7 @@ export default class Wire {
this.scope.wires.clean(this);
this.node1.checkDeleted();
this.node2.checkDeleted();

this.scope.timeStamp = new Date().getTime();
}
}

0 comments on commit 4633151

Please sign in to comment.