perf: optimize device & alarm query

This commit is contained in:
yangsy
2025-09-12 11:19:22 +08:00
parent e74d04d24b
commit 2ddab88a92
3 changed files with 35 additions and 6 deletions

View File

@@ -11,6 +11,7 @@ import { sleepFrame } from '@/utils/sleep';
import type { Station } from '@/apis/domains';
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
import type { StationAlarmCounts } from './domains';
import { runTask } from '@/utils/run-task';
const createEmptyStationAlarmCounts = () => {
return {
@@ -92,9 +93,12 @@ function useStationAlarmCountsMutation() {
stationAlarmCounts.unclassified = parseInt(total);
return stationAlarmCounts;
},
onSuccess: async (stationAlarmCounts, { station }) => {
onSuccess: (stationAlarmCounts, { station }) => {
// lineAlarmCounts.value[station.code] = stationAlarmCounts;
// await sleepFrame();
runTask(() => {
lineAlarmCounts.value[station.code] = stationAlarmCounts;
await sleepFrame();
});
},
onError: (error, { station }) => {
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);

View File

@@ -10,6 +10,7 @@ import { LINE_DEVICES_QUERY_KEY } from '@/constants';
import { ndmClient } from '@/apis/client';
import { sleepFrame } from '@/utils/sleep';
import type { Station } from '@/apis/domains';
import { runTask } from '@/utils/run-task';
const createEmptyStationDevices = (): StationDevices => {
return {
@@ -74,9 +75,13 @@ function useStationDevicesMutation() {
}
return await getNdmDevicesAll(station.code, signal);
},
onSuccess: async (stationDevices, { station }) => {
onSuccess: (stationDevices, { station }) => {
// TODO: 优化性能,避免阻塞主线程(待测试)
// lineDevices.value[station.code] = stationDevices;
// await sleepFrame();
runTask(() => {
lineDevices.value[station.code] = stationDevices;
await sleepFrame();
});
},
onError: (error, { station }) => {
console.error(`获取车站 ${station.name} 设备数据失败:`, error);

20
src/utils/run-task.ts Normal file
View File

@@ -0,0 +1,20 @@
export const runTask = (task: () => void) => {
// 方案一rIC
requestIdleCallback((idle) => {
if (idle.timeRemaining() > 0) {
task();
} else {
runTask(task);
}
});
// 方案二rAF + 时间判断
// const start = Date.now();
// requestAnimationFrame(() => {
// const now = Date.now();
// if (now - start > 16.6) {
// task();
// } else {
// runTask(task);
// }
// });
};