style(api): 统一代码风格并修复语法问题
- 统一import语句的空格格式 - 修复分号缺失问题 - 调整函数参数换行格式以符合规范 - 删除多余空行保持代码整洁 - 修复字符串拼接的换行格式问题
This commit is contained in:
@@ -17,35 +17,35 @@ const PRELOAD_ROUTES = [
|
||||
export class RoutePerformanceOptimizer {
|
||||
private router: Router;
|
||||
private preloadTimer: number | null = null;
|
||||
|
||||
|
||||
constructor(router: Router) {
|
||||
this.router = router;
|
||||
this.setupOptimizations();
|
||||
}
|
||||
|
||||
|
||||
private setupOptimizations() {
|
||||
// 路由性能监控
|
||||
this.setupPerformanceMonitoring();
|
||||
|
||||
|
||||
// 路由预加载
|
||||
this.setupRoutePreloading();
|
||||
|
||||
|
||||
// 路由缓存优化
|
||||
this.setupRouteCaching();
|
||||
}
|
||||
|
||||
|
||||
// 设置性能监控
|
||||
private setupPerformanceMonitoring() {
|
||||
this.router.beforeEach((to, from) => {
|
||||
routePerformanceMonitor.startRouteTimer();
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
this.router.afterEach((to, from) => {
|
||||
routePerformanceMonitor.endRouteTimer(to.path);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 设置路由预加载
|
||||
private setupRoutePreloading() {
|
||||
this.router.afterEach((to) => {
|
||||
@@ -53,32 +53,36 @@ export class RoutePerformanceOptimizer {
|
||||
if (this.preloadTimer) {
|
||||
clearTimeout(this.preloadTimer);
|
||||
}
|
||||
|
||||
|
||||
this.preloadTimer = window.setTimeout(() => {
|
||||
this.preloadRelatedRoutes(to);
|
||||
}, 2000); // 2秒后开始预加载
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 预加载相关路由
|
||||
private preloadRelatedRoutes(currentRoute: RouteLocationNormalized) {
|
||||
const routesToPreload = this.getRelatedRoutes(currentRoute.path);
|
||||
|
||||
routesToPreload.forEach(routePath => {
|
||||
|
||||
routesToPreload.forEach((routePath) => {
|
||||
const route = this.router.resolve(routePath);
|
||||
if (route.matched.length > 0) {
|
||||
const component = route.matched[route.matched.length - 1].components?.default;
|
||||
const component =
|
||||
route.matched[route.matched.length - 1].components?.default;
|
||||
if (component && typeof component === 'function') {
|
||||
componentPreloader.preload(routePath, component as () => Promise<any>);
|
||||
componentPreloader.preload(
|
||||
routePath,
|
||||
component as () => Promise<any>
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 获取相关路由
|
||||
private getRelatedRoutes(currentPath: string): string[] {
|
||||
const related: string[] = [];
|
||||
|
||||
|
||||
// 根据当前路径推断可能访问的路由
|
||||
if (currentPath === '/') {
|
||||
related.push(...PRELOAD_ROUTES);
|
||||
@@ -89,16 +93,16 @@ export class RoutePerformanceOptimizer {
|
||||
} else if (currentPath.startsWith('/bszx')) {
|
||||
related.push('/bszx/dashboard', '/bszx/ranking');
|
||||
}
|
||||
|
||||
return related.filter(path => path !== currentPath);
|
||||
|
||||
return related.filter((path) => path !== currentPath);
|
||||
}
|
||||
|
||||
|
||||
// 设置路由缓存
|
||||
private setupRouteCaching() {
|
||||
// 这里可以实现路由级别的缓存策略
|
||||
// 例如缓存路由组件、路由数据等
|
||||
}
|
||||
|
||||
|
||||
// 清理资源
|
||||
destroy() {
|
||||
if (this.preloadTimer) {
|
||||
@@ -115,7 +119,7 @@ export function optimizedLazyRoute(loader: () => Promise<any>) {
|
||||
if (preloaded) {
|
||||
return preloaded;
|
||||
}
|
||||
|
||||
|
||||
// 正常加载
|
||||
return loader();
|
||||
};
|
||||
@@ -126,16 +130,16 @@ export class SmartRoutePrefetcher {
|
||||
private router: Router;
|
||||
private prefetchQueue: Set<string> = new Set();
|
||||
private isIdle = true;
|
||||
|
||||
|
||||
constructor(router: Router) {
|
||||
this.router = router;
|
||||
this.setupIdleDetection();
|
||||
}
|
||||
|
||||
|
||||
// 设置空闲检测
|
||||
private setupIdleDetection() {
|
||||
let idleTimer: number;
|
||||
|
||||
|
||||
const resetIdleTimer = () => {
|
||||
this.isIdle = false;
|
||||
clearTimeout(idleTimer);
|
||||
@@ -144,45 +148,51 @@ export class SmartRoutePrefetcher {
|
||||
this.processPrefetchQueue();
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
|
||||
// 监听用户活动
|
||||
['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart'].forEach(event => {
|
||||
document.addEventListener(event, resetIdleTimer, true);
|
||||
});
|
||||
|
||||
['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart'].forEach(
|
||||
(event) => {
|
||||
document.addEventListener(event, resetIdleTimer, true);
|
||||
}
|
||||
);
|
||||
|
||||
resetIdleTimer();
|
||||
}
|
||||
|
||||
|
||||
// 添加到预取队列
|
||||
addToPrefetchQueue(routePath: string) {
|
||||
this.prefetchQueue.add(routePath);
|
||||
|
||||
|
||||
if (this.isIdle) {
|
||||
this.processPrefetchQueue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 处理预取队列
|
||||
private async processPrefetchQueue() {
|
||||
if (!this.isIdle || this.prefetchQueue.size === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const routePath = this.prefetchQueue.values().next().value;
|
||||
this.prefetchQueue.delete(routePath);
|
||||
|
||||
|
||||
try {
|
||||
const route = this.router.resolve(routePath);
|
||||
if (route.matched.length > 0) {
|
||||
const component = route.matched[route.matched.length - 1].components?.default;
|
||||
const component =
|
||||
route.matched[route.matched.length - 1].components?.default;
|
||||
if (component && typeof component === 'function') {
|
||||
await componentPreloader.preload(routePath, component as () => Promise<any>);
|
||||
await componentPreloader.preload(
|
||||
routePath,
|
||||
component as () => Promise<any>
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to prefetch route ${routePath}:`, error);
|
||||
}
|
||||
|
||||
|
||||
// 继续处理队列
|
||||
if (this.isIdle && this.prefetchQueue.size > 0) {
|
||||
setTimeout(() => this.processPrefetchQueue(), 100);
|
||||
@@ -194,49 +204,49 @@ export class SmartRoutePrefetcher {
|
||||
export class RoutePerformanceAnalyzer {
|
||||
private router: Router;
|
||||
private performanceData: Map<string, number[]> = new Map();
|
||||
|
||||
|
||||
constructor(router: Router) {
|
||||
this.router = router;
|
||||
this.setupAnalysis();
|
||||
}
|
||||
|
||||
|
||||
private setupAnalysis() {
|
||||
let startTime: number;
|
||||
|
||||
|
||||
this.router.beforeEach((to) => {
|
||||
startTime = performance.now();
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
this.router.afterEach((to) => {
|
||||
const duration = performance.now() - startTime;
|
||||
this.recordPerformance(to.path, duration);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private recordPerformance(path: string, duration: number) {
|
||||
if (!this.performanceData.has(path)) {
|
||||
this.performanceData.set(path, []);
|
||||
}
|
||||
|
||||
|
||||
const data = this.performanceData.get(path)!;
|
||||
data.push(duration);
|
||||
|
||||
|
||||
// 只保留最近10次记录
|
||||
if (data.length > 10) {
|
||||
data.shift();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 获取性能报告
|
||||
getPerformanceReport() {
|
||||
const report: Record<string, any> = {};
|
||||
|
||||
|
||||
this.performanceData.forEach((durations, path) => {
|
||||
const avg = durations.reduce((sum, d) => sum + d, 0) / durations.length;
|
||||
const min = Math.min(...durations);
|
||||
const max = Math.max(...durations);
|
||||
|
||||
|
||||
report[path] = {
|
||||
average: Math.round(avg),
|
||||
min: Math.round(min),
|
||||
@@ -244,21 +254,21 @@ export class RoutePerformanceAnalyzer {
|
||||
count: durations.length
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
|
||||
// 获取慢路由
|
||||
getSlowRoutes(threshold: number = 1000) {
|
||||
getSlowRoutes(threshold = 1000) {
|
||||
const slowRoutes: Array<{ path: string; avgTime: number }> = [];
|
||||
|
||||
|
||||
this.performanceData.forEach((durations, path) => {
|
||||
const avg = durations.reduce((sum, d) => sum + d, 0) / durations.length;
|
||||
if (avg > threshold) {
|
||||
slowRoutes.push({ path, avgTime: Math.round(avg) });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return slowRoutes.sort((a, b) => b.avgTime - a.avgTime);
|
||||
}
|
||||
}
|
||||
@@ -267,45 +277,45 @@ export class RoutePerformanceAnalyzer {
|
||||
export class RouteCacheManager {
|
||||
private cache = new Map<string, any>();
|
||||
private maxSize: number;
|
||||
|
||||
constructor(maxSize: number = 20) {
|
||||
|
||||
constructor(maxSize = 20) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
|
||||
// 缓存路由数据
|
||||
set(key: string, data: any) {
|
||||
if (this.cache.size >= this.maxSize) {
|
||||
const firstKey = this.cache.keys().next().value;
|
||||
this.cache.delete(firstKey);
|
||||
}
|
||||
|
||||
|
||||
this.cache.set(key, {
|
||||
data,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 获取缓存数据
|
||||
get(key: string, maxAge: number = 5 * 60 * 1000) {
|
||||
const cached = this.cache.get(key);
|
||||
|
||||
|
||||
if (!cached) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (Date.now() - cached.timestamp > maxAge) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return cached.data;
|
||||
}
|
||||
|
||||
|
||||
// 清除缓存
|
||||
clear() {
|
||||
this.cache.clear();
|
||||
}
|
||||
|
||||
|
||||
// 删除特定缓存
|
||||
delete(key: string) {
|
||||
return this.cache.delete(key);
|
||||
|
||||
Reference in New Issue
Block a user