perf: optimize device & alarm query
This commit is contained in:
@@ -11,6 +11,7 @@ import { sleepFrame } from '@/utils/sleep';
|
|||||||
import type { Station } from '@/apis/domains';
|
import type { Station } from '@/apis/domains';
|
||||||
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
|
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
|
||||||
import type { StationAlarmCounts } from './domains';
|
import type { StationAlarmCounts } from './domains';
|
||||||
|
import { runTask } from '@/utils/run-task';
|
||||||
|
|
||||||
const createEmptyStationAlarmCounts = () => {
|
const createEmptyStationAlarmCounts = () => {
|
||||||
return {
|
return {
|
||||||
@@ -92,9 +93,12 @@ function useStationAlarmCountsMutation() {
|
|||||||
stationAlarmCounts.unclassified = parseInt(total);
|
stationAlarmCounts.unclassified = parseInt(total);
|
||||||
return stationAlarmCounts;
|
return stationAlarmCounts;
|
||||||
},
|
},
|
||||||
onSuccess: async (stationAlarmCounts, { station }) => {
|
onSuccess: (stationAlarmCounts, { station }) => {
|
||||||
|
// lineAlarmCounts.value[station.code] = stationAlarmCounts;
|
||||||
|
// await sleepFrame();
|
||||||
|
runTask(() => {
|
||||||
lineAlarmCounts.value[station.code] = stationAlarmCounts;
|
lineAlarmCounts.value[station.code] = stationAlarmCounts;
|
||||||
await sleepFrame();
|
});
|
||||||
},
|
},
|
||||||
onError: (error, { station }) => {
|
onError: (error, { station }) => {
|
||||||
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { LINE_DEVICES_QUERY_KEY } from '@/constants';
|
|||||||
import { ndmClient } from '@/apis/client';
|
import { ndmClient } from '@/apis/client';
|
||||||
import { sleepFrame } from '@/utils/sleep';
|
import { sleepFrame } from '@/utils/sleep';
|
||||||
import type { Station } from '@/apis/domains';
|
import type { Station } from '@/apis/domains';
|
||||||
|
import { runTask } from '@/utils/run-task';
|
||||||
|
|
||||||
const createEmptyStationDevices = (): StationDevices => {
|
const createEmptyStationDevices = (): StationDevices => {
|
||||||
return {
|
return {
|
||||||
@@ -74,9 +75,13 @@ function useStationDevicesMutation() {
|
|||||||
}
|
}
|
||||||
return await getNdmDevicesAll(station.code, signal);
|
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;
|
lineDevices.value[station.code] = stationDevices;
|
||||||
await sleepFrame();
|
});
|
||||||
},
|
},
|
||||||
onError: (error, { station }) => {
|
onError: (error, { station }) => {
|
||||||
console.error(`获取车站 ${station.name} 设备数据失败:`, error);
|
console.error(`获取车站 ${station.name} 设备数据失败:`, error);
|
||||||
|
|||||||
20
src/utils/run-task.ts
Normal file
20
src/utils/run-task.ts
Normal 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);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user