Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js to Ts : simulator/src/backupCircuits.ts #417

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions src/simulator/src/data/backupCircuit.js

This file was deleted.

144 changes: 144 additions & 0 deletions src/simulator/src/data/backupCircuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { projectSavedSet } from './project';
import { moduleList, updateOrder } from '../metadata';


// Define interfaces for the core types
interface Node {
saveObject(): any;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved


interface SubCircuit {
removeConnections(): void;
makeConnections(): void;
}


interface VerilogMetadata {
[key: string]: any;
}


interface Layout {
[key: string]: any;
}


interface TestbenchData {
[key: string]: any;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved


interface Scope {
layout: Layout;
verilogMetadata: VerilogMetadata;
allNodes: Node[];
testbenchData: TestbenchData;
id: string | number;
name: string;
SubCircuit: SubCircuit[];
nodes: Node[];
restrictedCircuitElementsUsed: string[];
backups: string[];
history: any[];
timeStamp: number;
[key: string]: any; // For dynamic module access
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved


interface BackupData {
layout: Layout;
verilogMetadata: VerilogMetadata;
allNodes: any[];
testbenchData: TestbenchData;
id: string | number;
name: string;
nodes: number[];
restrictedCircuitElementsUsed: string[];
[key: string]: any; // For dynamic module properties
}


/* eslint-disable no-param-reassign */
function extract(obj: { saveObject: () => any }): any {
return obj.saveObject();
}


// Check if there is anything to backup - to be deprecated
/**
* Check if backup is available
* @param {Scope} scope - The scope to check for backup
* @return {boolean} - Whether backup is available
* @category data
*/
export function checkIfBackup(scope: Scope): boolean {
for (let i = 0; i < updateOrder.length; i++) {
if (scope[updateOrder[i]] && scope[updateOrder[i]].length) return true;
}
return false;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved


export function backUp(scope: Scope = globalScope): BackupData {
// Disconnection of subcircuits are needed because these are the connections between nodes
// in current scope and those in the subcircuit's scope
for (let i = 0; i < scope.SubCircuit.length; i++) {
scope.SubCircuit[i].removeConnections();
}


const data: BackupData = {
layout: scope.layout,
verilogMetadata: scope.verilogMetadata,
allNodes: scope.allNodes.map(extract),
testbenchData: scope.testbenchData,
id: scope.id,
name: scope.name,
nodes: [],
restrictedCircuitElementsUsed: scope.restrictedCircuitElementsUsed
};


// Storing details of all module objects
for (let i = 0; i < moduleList.length; i++) {
if (scope[moduleList[i]] && scope[moduleList[i]].length) {
data[moduleList[i]] = scope[moduleList[i]].map(extract);
}
}


// Storing intermediate nodes (nodes in wires)
data.nodes = scope.nodes.map(node => scope.allNodes.indexOf(node));


// Restoring the connections
for (let i = 0; i < scope.SubCircuit.length; i++) {
scope.SubCircuit[i].makeConnections();
}


return data;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved


export function scheduleBackup(scope: Scope = globalScope): string {
const backup = JSON.stringify(backUp(scope));
if (
scope.backups.length === 0 ||
scope.backups[scope.backups.length - 1] !== backup
) {
scope.backups.push(backup);
scope.history = [];
Fixed Show fixed Hide fixed
scope.timeStamp = new Date().getTime();
Fixed Show fixed Hide fixed
projectSavedSet(false);
}


return backup;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved


// Add global type declaration
declare const globalScope: Scope;