Skip to content

Commit 5e544c0

Browse files
committed
Refactor: Replace setTimeout and setInterval with Vue Lifecycle Hooks for Improved Performance
1 parent 9d95a2a commit 5e544c0

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

src/components/DialogBox/CombinationalAnalysis.vue

+52-51
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import { stripTags } from '#/simulator/src/utils'
2424
import { useState } from '#/store/SimulatorStore/state'
2525
import messageBox from '@/MessageBox/messageBox.vue'
26-
import { ref } from 'vue'
27-
import { onMounted } from 'vue'
26+
import { ref, onMounted, watch } from 'vue'
2827
2928
/* imports from combinationalAnalysis.js */
3029
import { GenerateCircuit, solveBooleanFunction } from '#/simulator/src/combinationalAnalysis'
@@ -200,62 +199,17 @@ function createLogicTable() {
200199
alertType.value = 'info'
201200
alertMessage.value =
202201
'Enter Input / Output Variable(s) OR Boolean Function!'
203-
setTimeout(() => {
202+
watch(showAlert, () => {
204203
showAlert.value = false
205-
}, 2000)
204+
}, { timeout: 2000 })
206205
} else {
207206
showAlert.value = true
208207
alertType.value = 'warning'
209208
alertMessage.value =
210209
'Use Either Combinational Analysis Or Boolean Function To Generate Circuit!'
211-
setTimeout(() => {
210+
watch(showAlert, () => {
212211
showAlert.value = false
213-
}, 2000)
214-
}
215-
}
216-
217-
function printBooleanTable() {
218-
// Create a new div to hold the table content
219-
const tableContent = document.createElement('div')
220-
tableContent.innerHTML = tableHeader.value.map((header, index) => `
221-
<tr>
222-
<th>${header}</th>
223-
</tr>
224-
`).join('') +
225-
tableBody.value.map(row => `
226-
<tr>
227-
${row.map(cell => `<td>${cell}</td>`).join('')}
228-
</tr>
229-
`).join('')
230-
231-
const style = `
232-
<style>
233-
table {font: 40px Calibri;}
234-
table, th, td {border: solid 1px #DDD; border-collapse: collapse;}
235-
tbody {padding: 2px 3px; text-align: center;}
236-
</style>
237-
`.replace(/\n/g, '')
238-
239-
const win = window.open('', '', 'height=700,width=700')
240-
if (win) {
241-
const htmlBody = `
242-
<html>
243-
<head>
244-
<title>Boolean Logic Table</title>
245-
${style}
246-
</head>
247-
<body>
248-
<center>
249-
<table>
250-
${tableContent.innerHTML}
251-
</table>
252-
</center>
253-
</body>
254-
</html>
255-
`
256-
win.document.write(htmlBody)
257-
win.document.close()
258-
win.print()
212+
}, { timeout: 2000 })
259213
}
260214
}
261215
@@ -334,6 +288,53 @@ function createBooleanPrompt(inputList, outputList, scope = globalScope) {
334288
},
335289
]
336290
}
291+
292+
function printBooleanTable() {
293+
// Get the table content using Vue refs
294+
const messageBoxContent = document.querySelector('.messageBox .v-card-text')?.innerHTML || ''
295+
296+
const style = `
297+
<style>
298+
table { font: 40px Calibri; }
299+
table, th, td { border: solid 1px #DDD; border-collapse: collapse; }
300+
tbody { padding: 2px 3px; text-align: center; }
301+
</style>
302+
`.replace(/\n\s*/g, '')
303+
304+
const htmlBody = `
305+
<!DOCTYPE html>
306+
<html>
307+
<head>
308+
<title>Boolean Logic Table</title>
309+
${style}
310+
</head>
311+
<body>
312+
<center>${messageBoxContent}</center>
313+
</body>
314+
</html>
315+
`
316+
317+
// Create a new window for printing
318+
const printWindow = window.open('', '_blank', 'height=700,width=700')
319+
if (printWindow) {
320+
printWindow.document.write(htmlBody)
321+
printWindow.document.close()
322+
// Wait for resources to load before printing
323+
printWindow.onload = () => {
324+
printWindow.print()
325+
printWindow.onafterprint = () => {
326+
printWindow.close()
327+
}
328+
}
329+
} else {
330+
showAlert.value = true
331+
alertType.value = 'error'
332+
alertMessage.value = 'Please allow popups to print the truth table'
333+
watch(showAlert, () => {
334+
showAlert.value = false
335+
}, { timeout: 3000 })
336+
}
337+
}
337338
</script>
338339

339340
<style scoped>

0 commit comments

Comments
 (0)