Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/simulator/src/types/scope.types.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>
pending?: any[]
[key: string]: any
}