diff --git a/src/simulator/src/data/backupCircuit.js b/src/simulator/src/data/backupCircuit.ts similarity index 58% rename from src/simulator/src/data/backupCircuit.js rename to src/simulator/src/data/backupCircuit.ts index e15f5e444..e1c214001 100644 --- a/src/simulator/src/data/backupCircuit.js +++ b/src/simulator/src/data/backupCircuit.ts @@ -1,33 +1,72 @@ import { projectSavedSet } from './project' import { moduleList, updateOrder } from '../metadata' +import { Scope } from '../types/scope.types' -/* eslint-disable no-param-reassign */ -function extract(obj) { +interface Saveable { + saveObject(): any +} + +interface BackupData { + layout: { + width: number + height: number + title_x: number + title_y: number + titleEnabled: boolean + } + verilogMetadata: { + isVerilogCircuit: boolean + isMainCircuit: boolean + code: string + subCircuitScopeIds: string[] + } + allNodes: any[] + testbenchData: any + id: number | string + name: string + nodes: number[] + restrictedCircuitElementsUsed: any[] + [key: string]: any +} + +declare const globalScope: Scope + +/** + * Extract save object from an object + * @param obj - Object with saveObject method + * @returns Result of saveObject method + */ +function extract(obj: Saveable): any { return obj.saveObject() } // Check if there is anything to backup - to be deprecated /** * Check if backup is available - * @param {Scope} scope - * @return {boolean} + * @param scope - Circuit scope + * @return True if backup is available * @category data */ -export function checkIfBackup(scope) { +export function checkIfBackup(scope: Scope): boolean { for (let i = 0; i < updateOrder.length; i++) { if (scope[updateOrder[i]].length) return true } return false } -export function backUp(scope = globalScope) { +/** + * Creates a backup of the circuit + * @param scope - Circuit scope, defaults to globalScope + * @returns Backup data object + */ +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() } - var data = {} + const data: BackupData = {} as BackupData // Storing layout data.layout = scope.layout @@ -69,8 +108,14 @@ export function backUp(scope = globalScope) { return data } -export function scheduleBackup(scope = globalScope) { - var backup = JSON.stringify(backUp(scope)) +/** + * Schedules a backup of the circuit + * @param scope - Circuit scope, defaults to globalScope + * @returns Stringified backup + */ +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 diff --git a/src/simulator/src/types/scope.types.ts b/src/simulator/src/types/scope.types.ts new file mode 100644 index 000000000..355622df3 --- /dev/null +++ b/src/simulator/src/types/scope.types.ts @@ -0,0 +1,42 @@ +import CircuitElement from '../circuitElement' +import Node from '../node' + +export interface SubCircuit { + removeConnections(): void + makeConnections(): void +} + +export interface Scope { + id: number | string + name: string + root: CircuitElement + timeStamp: number + backups: string[] + history: string[] + layout: { + width: number + height: number + title_x: number + title_y: number + titleEnabled: boolean + } + verilogMetadata: { + isVerilogCircuit: boolean + isMainCircuit: boolean + code: string + subCircuitScopeIds: string[] + } + restrictedCircuitElementsUsed: any[] + CircuitElement: any[] + nodes: Node[] + allNodes: Node[] + SubCircuit: SubCircuit[] + testbenchData: any + ox: number + oy: number + scale: number + stack: any[] + tunnelList?: Record + pending?: any[] + [key: string]: any +}