-
Notifications
You must be signed in to change notification settings - Fork 139
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
refactor: ts in plotArea.js #478
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThis update refactors the Changes
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
src/simulator/src/plotArea.ts (1)
451-479
: 🛠️ Refactor suggestionImprove event listener management and mouse event handling.
- Add cleanup for event listeners to prevent memory leaks
- Add error handling for mouse events
- Consider using a debounce for mousemove events
export function setupTimingListeners() { + const plotAreaElement = document.getElementById('plotArea') + if (!plotAreaElement) { + console.error('Plot area element not found') + return + } + + const handleMouseDown = (e: MouseEvent) => { const rect = plotArea.canvas!.getBoundingClientRect() const x = sh(e.clientX - rect.left) // ... rest of the mousedown logic - }) + } + + plotAreaElement.addEventListener('mousedown', handleMouseDown) // ... similar for other event listeners + + // Return cleanup function + return () => { + plotAreaElement.removeEventListener('mousedown', handleMouseDown) + // ... remove other listeners + } }
🧹 Nitpick comments (4)
src/simulator/src/plotArea.ts (4)
23-28
: Convert mutable variables to constants.These variables don't appear to be modified after initialization. Consider converting them to
const
for better immutability and code safety.-let timeLineHeight = sh(20) -let padding = sh(2) -let plotHeight = sh(20) -let waveFormPadding = sh(5) -let waveFormHeight = plotHeight - 2 * waveFormPadding -let flagLabelWidth = sh(75) +const timeLineHeight = sh(20) +const padding = sh(2) +const plotHeight = sh(20) +const waveFormPadding = sh(5) +const waveFormHeight = plotHeight - 2 * waveFormPadding +const flagLabelWidth = sh(75)
50-86
: Add JSDoc comments to the PlotArea interface.Consider adding JSDoc comments to document the purpose and usage of each property and method in the interface. This will improve code maintainability and developer experience.
+/** + * Interface representing the plot area for timing diagrams. + */ interface PlotArea { + /** Current cycle offset for scrolling */ cycleOffset: number + /** Device pixel ratio */ DPR: number + /** Canvas element reference */ canvas: HTMLCanvasElement | null // ... add comments for other properties and methods }
139-146
: Add error handling to the download method.The method could fail silently if the canvas is null or if there are issues with creating/downloading the image.
download() { - if (!this.canvas) return + if (!this.canvas) { + console.warn('Cannot download: Canvas is not initialized') + return + } const img = this.canvas.toDataURL('image/png') const anchor = document.createElement('a') anchor.href = img anchor.download = 'waveform.png' - anchor.click() + try { + anchor.click() + } catch (error) { + console.error('Failed to download waveform:', error) + } }
191-217
: Extract magic numbers into named constants.The utilization thresholds (90, 10) and the recommended unit multiplier (3) should be extracted into named constants for better maintainability.
+const UTILIZATION_HIGH_THRESHOLD = 90 +const UTILIZATION_LOW_THRESHOLD = 10 +const MIN_RECOMMENDED_UNITS = 20 +const RECOMMENDED_UNIT_MULTIPLIER = 3 update() { // ... - if (utilization >= 90 || utilization <= 10) { - const recommendedUnit = Math.max(20, Math.round(unitUsed * 3)) + if (utilization >= UTILIZATION_HIGH_THRESHOLD || utilization <= UTILIZATION_LOW_THRESHOLD) { + const recommendedUnit = Math.max(MIN_RECOMMENDED_UNITS, Math.round(unitUsed * RECOMMENDED_UNIT_MULTIPLIER)) // ...
@vedant-jain03 @Arnabdaz Please review changes!! |
@@ -1,12 +1,13 @@ | |||
import { simulationArea } from './simulationArea' | |||
import { globalScope } from './globalScope' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no such file as globalscope
Why have you done this ?
let timeLineHeight = sh(20) | ||
let padding = sh(2) | ||
let plotHeight = sh(20) | ||
let waveFormPadding = sh(5) | ||
let waveFormHeight = plotHeight - 2 * waveFormPadding | ||
let flagLabelWidth = sh(75) | ||
let cycleWidth = sh(30) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using let reduces the type-safety, this defeats the purpose of TS
@gitsofaryan Have you used AI tools for converting to TS files ?, since AI would work well on singleton files but the code will break when we try to combine all the files, You should review it first |
Fixes: #414
Description
plotArea
, improving organization and maintainability.PlotArea
for type safety and consistency.Summary by CodeRabbit
These internal improvements help deliver a more stable experience while maintaining the same user interface.