Skip to content

Commit 9c38656

Browse files
committed
Convert backupCircuit.js to TypeScript
1 parent 0921d26 commit 9c38656

File tree

2 files changed

+122
-85
lines changed

2 files changed

+122
-85
lines changed

src/simulator/src/data/backupCircuit.js

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { projectSavedSet } from './project';
2+
import { moduleList, updateOrder } from '../metadata';
3+
4+
/* eslint-disable no-param-reassign */
5+
6+
// ------------------
7+
// Type Declarations
8+
// ------------------
9+
10+
interface NodeLike {
11+
saveObject: () => Record<string, any>;
12+
}
13+
14+
interface SubCircuitLike {
15+
removeConnections: () => void;
16+
makeConnections: () => void;
17+
}
18+
19+
interface Scope {
20+
SubCircuit: SubCircuitLike[];
21+
allNodes: NodeLike[];
22+
nodes: NodeLike[];
23+
layout: Record<string, any>;
24+
verilogMetadata: Record<string, any>;
25+
testbenchData: Record<string, any>;
26+
id: number;
27+
name: string;
28+
restrictedCircuitElementsUsed: Record<string, any>;
29+
backups: string[];
30+
history: string[];
31+
timeStamp: number;
32+
[key: string]: any; // For dynamic moduleList entries
33+
}
34+
35+
// Global declaration (CircuitVerse uses globalScope)
36+
declare const globalScope: Scope;
37+
38+
// ------------------
39+
// Helper Functions
40+
// ------------------
41+
42+
function extract(obj: NodeLike): Record<string, any> {
43+
return obj.saveObject();
44+
}
45+
46+
/**
47+
* Check if backup is available
48+
* @param scope Circuit scope
49+
* @returns true if any data exists for backup
50+
* @category data
51+
*/
52+
export function checkIfBackup(scope: Scope): boolean {
53+
for (let i = 0; i < updateOrder.length; i++) {
54+
const key = updateOrder[i];
55+
if (scope[key]?.length) return true;
56+
}
57+
return false;
58+
}
59+
60+
/**
61+
* Create a serialized backup of the current circuit state.
62+
* @param scope Current working scope (defaults to globalScope)
63+
* @returns JSON-compatible backup data object
64+
*/
65+
export function backUp(scope: Scope = globalScope): Record<string, any> {
66+
// Disconnect subcircuits before saving
67+
for (const sub of scope.SubCircuit) {
68+
sub.removeConnections();
69+
}
70+
71+
const data: Record<string, any> = {
72+
layout: scope.layout,
73+
verilogMetadata: scope.verilogMetadata,
74+
allNodes: scope.allNodes.map(extract),
75+
testbenchData: scope.testbenchData,
76+
id: scope.id,
77+
name: scope.name,
78+
};
79+
80+
// Save all module objects dynamically
81+
for (const moduleName of moduleList) {
82+
if (scope[moduleName]?.length) {
83+
data[moduleName] = scope[moduleName].map(extract);
84+
}
85+
}
86+
87+
data.restrictedCircuitElementsUsed = scope.restrictedCircuitElementsUsed;
88+
89+
// Save intermediate nodes (wire nodes)
90+
data.nodes = scope.nodes.map(node =>
91+
scope.allNodes.indexOf(node)
92+
);
93+
94+
// Reconnect subcircuits after saving
95+
for (const sub of scope.SubCircuit) {
96+
sub.makeConnections();
97+
}
98+
99+
return data;
100+
}
101+
102+
/**
103+
* Schedule a new backup in the current scope.
104+
* Ensures duplicates aren’t stored consecutively.
105+
* @param scope Current scope (default = globalScope)
106+
* @returns The serialized backup string
107+
*/
108+
export function scheduleBackup(scope: Scope = globalScope): string {
109+
const backup = JSON.stringify(backUp(scope));
110+
111+
if (
112+
scope.backups.length === 0 ||
113+
scope.backups[scope.backups.length - 1] !== backup
114+
) {
115+
scope.backups.push(backup);
116+
scope.history = [];
117+
scope.timeStamp = Date.now();
118+
projectSavedSet(false);
119+
}
120+
121+
return backup;
122+
}

0 commit comments

Comments
 (0)