浏览代码

⚡️ perf(progress): optimize build completion detection logic

- 添加非加载状态快速退出机制,避免无效检查
- 优化混合模式完成条件,增加基于资源完成度的提前完成策略:
  - 95%资源完成+0.8秒超时提前完成
  - 98%资源完成+0.6秒超时提前完成
- 实现动态检查间隔算法,根据当前进度状态调整检查频率
- 服务端完成后提高检查频率,资源接近完成时缩短检查间隔
- 修复条件未满足时的重新调度逻辑,确保进度检查持续进行
yourname 7 月之前
父节点
当前提交
0eaead418e
共有 1 个文件被更改,包括 59 次插入3 次删除
  1. 59 3
      vite-plugin-compile-progress.js

+ 59 - 3
vite-plugin-compile-progress.js

@@ -148,6 +148,12 @@ export function progressTrackingPlugin() {
                     console.log('timeSinceLastResource:', timeSinceLastResource);  
                     console.log('isLoading:', isLoading);  
                       
+                    // 如果已经不在加载状态,直接返回  
+                    if (!isLoading) {  
+                      console.log('⚠️ Not loading anymore, exiting checkCompletion');  
+                      return;  
+                    }  
+                      
                     // 根据服务端活动情况调整完成条件  
                     if (!hasServerActivity) {  
                       console.log('Checking client-only completion conditions...');  
@@ -177,9 +183,23 @@ export function progressTrackingPlugin() {
                     } else {  
                       console.log('Checking mixed mode completion conditions...');  
                         
-                      // 有服务端编译时的完成条件  
+                      // 有服务端编译时的完成条件 - 添加多个完成条件  
                       if (serverProgress >= 1.0 && timeSinceLastResource > 1500) {  
-                        console.log('✅ Server + client loading complete');  
+                        console.log('✅ Server + client loading complete (1.5s timeout)');  
+                        forceComplete();  
+                        return;  
+                      }  
+                        
+                      // 添加基于资源完成度的提前完成条件  
+                      if (serverProgress >= 1.0 && resourcesCompleted >= estimatedTotal * 0.95 && timeSinceLastResource > 800) {  
+                        console.log('✅ Server + client loading complete (95% resources + 0.8s)');  
+                        forceComplete();  
+                        return;  
+                      }  
+                        
+                      // 如果服务端完成且资源接近完成,更短的等待时间  
+                      if (serverProgress >= 1.0 && resourcesCompleted >= estimatedTotal * 0.98 && timeSinceLastResource > 600) {  
+                        console.log('✅ Server + client loading complete (98% resources + 0.6s)');  
                         forceComplete();  
                         return;  
                       }  
@@ -187,17 +207,53 @@ export function progressTrackingPlugin() {
                       console.log('❌ Mixed mode conditions not met');  
                       console.log('serverProgress >= 1.0:', serverProgress >= 1.0);  
                       console.log('timeSinceLastResource > 1500:', timeSinceLastResource > 1500);  
+                      console.log('resourcesCompleted >= estimatedTotal * 0.95:', resourcesCompleted >= estimatedTotal * 0.95);  
                     }  
                       
                     // 超时强制完成 - 进一步缩短超时时间  
                     if (timeSinceLastResource > 2000) {  
                       console.log('⏰ Force completing due to timeout (2s)');  
                       forceComplete();  
+                      return;  
+                    }  
+                      
+                    // 关键修复:如果条件未满足,重新调度检查  
+                    // 根据当前状态动态计算下次检查的时间间隔  
+                    let nextCheckDelay;  
+                      
+                    if (hasServerActivity && serverProgress >= 1.0) {  
+                      // 服务端已完成,更频繁地检查  
+                      const remainingTimeFor1500 = Math.max(1500 - timeSinceLastResource, 0);  
+                      const remainingTimeFor800 = Math.max(800 - timeSinceLastResource, 0);  
+                        
+                      if (resourcesCompleted >= estimatedTotal * 0.95) {  
+                        nextCheckDelay = Math.max(remainingTimeFor800, 200);  
+                      } else {  
+                        nextCheckDelay = Math.max(remainingTimeFor1500, 300);  
+                      }  
+                    } else if (!hasServerActivity) {  
+                      // 客户端模式,根据资源完成度调整检查频率  
+                      if (resourcesCompleted >= estimatedTotal * 0.97) {  
+                        nextCheckDelay = Math.max(500 - timeSinceLastResource, 200);  
+                      } else if (resourcesCompleted >= estimatedTotal * 0.95) {  
+                        nextCheckDelay = Math.max(1000 - timeSinceLastResource, 300);  
+                      } else {  
+                        nextCheckDelay = Math.max(1000 - timeSinceLastResource, 500);  
+                      }  
                     } else {  
-                      console.log('⏳ Waiting for timeout, remaining:', 2000 - timeSinceLastResource, 'ms');  
+                      // 服务端还在处理中,较长的检查间隔  
+                      nextCheckDelay = 500;  
                     }  
                       
+                    // 确保最小检查间隔  
+                    nextCheckDelay = Math.max(nextCheckDelay, 200);  
+                      
+                    console.log('⏳ Rescheduling completion check in:', nextCheckDelay, 'ms');  
                     console.log('=== End checkCompletion Debug ===');  
+                      
+                    // 重新调度下次检查  
+                    if (completionTimer) clearTimeout(completionTimer);  
+                    completionTimer = setTimeout(checkCompletion, nextCheckDelay);  
                   }
   
                   // 强制完成