Skip to content

refactor : typescript integration in src/simulator/src/app.ts #431

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

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 9 additions & 6 deletions src/simulator/src/app.js → src/simulator/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { setup } from './setup'

import { setup } from './setup';
import { CircuitConfiguration }from './types/app.types'
document.addEventListener('DOMContentLoaded', () => {
setup()
var js = {
setup();

type DeviceType = 'Input' | 'Output' | 'Memory';

const js: CircuitConfiguration = {
devices: {
dev0: {
type: 'Input',
Expand Down Expand Up @@ -206,5 +209,5 @@ document.addEventListener('DOMContentLoaded', () => {
},
],
subcircuits: {},
}
})
};
});
60 changes: 0 additions & 60 deletions src/simulator/src/hotkey_binder/view/panel.ui.js

This file was deleted.

79 changes: 79 additions & 0 deletions src/simulator/src/hotkey_binder/view/panel.ui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { setUserKeys } from '../model/actions'

/**
* Update the hotkey panel UI with the currently set configuration
* @param mode User preferred configuration or default keys
*/
export const updateHTML = (mode: 'user' | 'default'): void => {
const preferenceContainer = document.getElementById('preference')
if (!preferenceContainer) return

if (mode === 'user') {
const userKeys = localStorage.get('userKeys') as Record<string, string>
const children = preferenceContainer.children

for (let x = 0; x < children.length; x++) {
const child = children[x] as HTMLElement
const keyElement = child.querySelector('.key-name') as HTMLElement
const valueElement = child.querySelector('.key-value') as HTMLElement

if (keyElement && valueElement) {
valueElement.innerText = userKeys[keyElement.innerText] || ''
}
}
} else if (mode === 'default') {
const defaultKeys = localStorage.get('defaultKeys') as Record<string, string>
const children = preferenceContainer.children

for (let x = 0; x < children.length; x++) {
const child = children[x] as HTMLElement
const keyElement = child.querySelector('.key-name') as HTMLElement
const valueElement = child.querySelector('.key-value') as HTMLElement

if (keyElement && valueElement) {
valueElement.innerText = defaultKeys[keyElement.innerText] || ''
}
}
}
}

/**
* Override key of duplicate entries
* @param combo Key combination to override
*/
export const override = (combo: string): void => {
const preferenceContainer = document.getElementById('preference')
if (!preferenceContainer) return

const children = preferenceContainer.children
for (let x = 0; x < children.length; x++) {
const child = children[x] as HTMLElement
const valueElement = child.querySelector('.key-value') as HTMLElement

if (valueElement && valueElement.innerText === combo) {
valueElement.innerText = ''
}
}
}

/**
* Close the edit interface
*/
export const closeEdit = (): void => {
const pressedKeysElement = document.getElementById('pressedKeys')
const editElement = document.getElementById('edit')

if (pressedKeysElement) pressedKeysElement.textContent = ''
if (editElement) editElement.style.display = 'none'
}

/**
* Submit user key configurations
*/
export const submit = (): void => {
const editElement = document.getElementById('edit')
if (editElement) editElement.style.display = 'none'

setUserKeys()
updateHTML('user')
}
32 changes: 32 additions & 0 deletions src/simulator/src/types/app.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type DeviceType = 'Input' | 'Output' | 'Memory';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify if the DeviceType union type covers all possible device types.

Please confirm that these are all the possible device types in the circuit simulator. Common device types that might be missing: 'Gate', 'FlipFlop', 'Counter', etc.


🏁 Script executed:

#!/bin/bash
# Search for device type strings in the codebase
rg -g '*.{ts,js}' -i "type['\"]?\s*[:=]\s*['\"](?:Input|Output|Memory|Gate|FlipFlop|Counter)" src/simulator/

Length of output: 1144


DeviceType union missing at least one reported device type.

  • The union type in src/simulator/src/types/app.types.ts is defined as 'Input' | 'Output' | 'Memory'.
  • Our search identified that Counter is being used (in src/simulator/src/modules/Counter.js) as an object type, yet it isn’t included in the union.
  • Verify if other device types (e.g., 'Gate', 'FlipFlop') should also be added.


interface Device {
type: DeviceType;
net?: string;
order?: number;
bits: number;
label?: string;
abits?: number;
words?: number;
offset?: number;
rdports?: Array<{clock_polarity?: boolean}>;
wrports?: Array<{clock_polarity?: boolean}>;
memdata?: (number | string)[];
}

interface Endpoint {
id: string;
port: string;
}

interface Connector {
to: Endpoint;
from: Endpoint;
name: string;
}

export interface CircuitConfiguration {
devices: {[key: string]: Device};
connectors: Connector[];
subcircuits: Record<string, unknown>;
}
Comment on lines +28 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve type safety of the CircuitConfiguration interface.

Two potential improvements:

  1. Consider using a more specific key type for devices if possible
  2. The subcircuits type is too permissive with unknown

Consider this refactor:

// If you have a specific set of device IDs
type DeviceId = string; // Or a more specific union type if applicable

// If subcircuits have a specific structure
interface Subcircuit {
    // Add appropriate properties
}

export interface CircuitConfiguration {
    devices: Record<DeviceId, Device>;
    connectors: Connector[];
    subcircuits: Record<string, Subcircuit>;
}

Loading