Parcourir la source

perf(base-S-T6): deviceCenter 设备列表3s轮询与行展开1s轮询共用timer拆分为listTimer/expandTimer

86132 il y a 3 jours
Parent
commit
1b4ea726b6
1 fichiers modifiés avec 25 ajouts et 18 suppressions
  1. 25 18
      src/views/vent/dataCenter/deviceCenter/index.vue

+ 25 - 18
src/views/vent/dataCenter/deviceCenter/index.vue

@@ -317,15 +317,15 @@ async function getDeviceType(type?) {
 async function getDeviceList(deviceTypeVal?: any) {
   // 1. 如果没有设备类型值,停止定时器并返回(不再重复请求)
   if (!deviceTypeVal) {
-    if (timer) {
-      clearInterval(timer);
-      timer = undefined;
+    if (listTimer) {
+      clearInterval(listTimer);
+      listTimer = undefined;
     }
     return;
   }
-  if (timer) {
-    clearInterval(timer);
-    timer = undefined;
+  if (listTimer) {
+    clearInterval(listTimer);
+    listTimer = undefined;
   }
   loading.value = true;
   const fetchDeviceData = async () => {
@@ -343,7 +343,7 @@ async function getDeviceList(deviceTypeVal?: any) {
     }
   };
   await fetchDeviceData();
-  timer = setInterval(fetchDeviceData, 3000);
+  listTimer = setInterval(fetchDeviceData, 3000);
 }
 
 // 外层表格列配置
@@ -421,9 +421,9 @@ const toggleExpand = (deviceId) => {
   if (index > -1) {
     // 如果已经展开,则关闭
     expandedRowKeys.value.splice(index, 1);
-    if (timer) {
-      clearInterval(timer);
-      timer = undefined;
+    if (expandTimer) {
+      clearInterval(expandTimer);
+      expandTimer = undefined;
     }
   } else {
     // 如果未展开,则打开
@@ -431,18 +431,20 @@ const toggleExpand = (deviceId) => {
     loadMonitoringData(deviceId);
   }
 };
+// [perf-base-v1] 设备列表 3s 轮询与行展开 1s 轮询原共用同一 timer 变量会互相覆盖导致泄漏,拆分为两个独立句柄
+let listTimer: null | NodeJS.Timeout = null;
+let expandTimer: null | NodeJS.Timeout = null;
 // 加载监测数据
-let timer: null | NodeJS.Timeout = null;
 async function loadMonitoringData(deviceId: string) {
   // 先清除之前的定时器
-  if (timer) {
-    clearInterval(timer);
-    timer = null;
+  if (expandTimer) {
+    clearInterval(expandTimer);
+    expandTimer = null;
   }
   // 定时器会在1秒后开始,所以先手动加载一次
   refreshData(deviceId);
   // 设置新的定时器
-  timer = setInterval(() => {
+  expandTimer = setInterval(() => {
     refreshData(deviceId);
   }, 1000);
 }
@@ -465,9 +467,14 @@ onMounted(() => {
   }
 });
 onUnmounted(() => {
-  if (timer) {
-    clearInterval(timer);
-    timer = undefined;
+  // [perf-base-v1] 两个轮询句柄均需清理
+  if (listTimer) {
+    clearInterval(listTimer);
+    listTimer = undefined;
+  }
+  if (expandTimer) {
+    clearInterval(expandTimer);
+    expandTimer = undefined;
   }
 });
 // 监听分页变化