const updateThreadsCount = (type: number) => { props.cpuCounts.forEach(set => {…
- User
const updateThreadsCount = (type: number) => { props.cpuCounts.forEach(set => { console.log(set, 'set') console.log(set?.source?.cpuCount, 'set?.source?.cpuCount') const cpuCount = set?.source?.cpuCount || 0
for (const idx in unref(pairsForm)) { console.log(cpuCount, 'cpuCount') let updatedCount = unref(pairsForm)[idx].threads.threadscount if (type === 0) { // auto updatedCount = calculateAutoThreads(cpuCount) } else if (type === 1) { // max updatedCount = calculateMaxThreads(cpuCount) } else if (type === 2) { // num updatedCount = Math.min(threadscount.value, cpuCount) } unref(pairsForm)[idx].threads.threadscount = updatedCount threadscount.value = updatedCount } }) } const validateThreadCount = (value: number) => { props.cpuCounts.forEach(set => { const cpuCount = set?.source?.cpuCount || 0 if (value > cpuCount) { threadscount.value = cpuCount // the number of threads cannot be greater than the number of CPUs } }) }как оптимизировать повторяющийся код props.cpuCounts.forEach(set => ...
- Assistant
To optimize the repeated code segment
props.cpuCounts.forEach(set => ...), you can refactor it into a reusable function or simplify the logic if applicable. This helps in reducing redundancy, making the code cleaner and more maintainable. Here's a refactored version that extracts the repeated logic into a utility function:const processCpuCounts = (callback) => { props.cpuCounts.forEach(set => { const cpuCount = set?.source?.cpuCount || 0; callback(cpuCount); }); }; const updateThreadsCount = (type: number) => { processCpuCounts((cpuCount) => { console.log(cpuCount, 'cpuCount'); for (const idx in unref(pairsForm)) { let updatedCount = unref(pairsForm)[idx].threads.threadscount; if (type === 0) { // auto updatedCount = calculateAutoThreads(cpuCount); } else if (type === 1) { // max updatedCount = calculateMaxThreads(cpuCount); } else if (type === 2) { // num updatedCount = Math.min(threadscount.value, cpuCount); } unref(pairsForm)[idx].threads.threadscount = updatedCount; threadscount.value = updatedCount; } }); }; const validateThreadCount = (value: number) => { processCpuCounts((cpuCount) => { if (value > cpuCount) { threadscount.value = cpuCount; // the number of threads cannot be greater than the number of CPUs } }); }; - User
columnHelper.display({ id: 'threads', header: t('set.configuration.advanced.threads.title').toString(), cell: ({ row: { original } }) => { const currentType = computed(() => unref(pairsFormModel)[original.id].threads.type)
props.cpuCounts.forEach(set => { const cpuCount = set?.source?.cpuCount || 0 let updatedCount = unref(pairsFormModel)[original.id].threads.threadscount if (currentType.value === 0) { // auto updatedCount = Math.max(1, Math.min(8, Math.floor(cpuCount * 0.5))) } else if (currentType.value === 1) { // max updatedCount = Math.min(8, cpuCount) } else if (currentType.value === 2) { // num updatedCount = Math.min(unref(pairsFormModel)[original.id].threads.threadscount, cpuCount) } unref(pairsFormModel)[original.id].threads.threadscount = updatedCount }) return h('div', [ h('el-select', { on: { change: (val) => { unref(pairsFormModel)[original.id].threads.type = val } }, style: 'width: 230px;', props: { value: currentType.value, placeholder: t('set.configuration.advanced.threads.select') } }, unref(threadsOptions).map(option => h('el-option', { props: { key: option.value, label: option.label, value: option.value } } ))), currentType.value === 2 ? h('el-input', { on: { input: (val) => { pairsFormModel.value[original.id].threads.threadscount = val } }, style: { width: '230px', paddingTop: '3px' }, attrs: { placeholder: t('set.configuration.advanced.threads.placeholder') }, props: { value: unref(pairsFormModel)[original.id].threads.threadscount, type: 'number' } }) : null ]) }, minSize: 210 }), - Assistant