hejianhao
2026-04-02 0940d56748a73525790c350c393a5be960aab62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * 智能数据加载器
 * 通过检测项目变化来决定是否重新加载数据
 */
 
import { ref, watch, computed, onMounted, onBeforeUnmount } from 'vue'
import { useProjectStore, useTrainingDataStore } from '@store'
 
export function useSmartDataLoader(dataType = 'all') {
  const projectStore = useProjectStore()
  const trainingDataStore = useTrainingDataStore()
  
  // 本地状态
  const isLoading = ref(false)
  const error = ref(null)
  const lastLoadTime = ref(null)
  
  // 监听器清理函数
  let unwatchProject = null
  
  /**
   * 智能加载数据
   * 只在项目变化或数据过期时重新加载
   */
  const smartLoadData = async (forceReload = false) => {
    try {
      isLoading.value = true
      error.value = null
      
      // 检查项目是否变化
      const projectChanged = trainingDataStore.isProjectChanged
      
      if (projectChanged) {
        console.log('[SmartDataLoader] 检测到项目变化,重新加载数据')
      }
      
      // 根据数据类型加载相应数据
      const loadPromises = []
      
      if (dataType === 'all' || dataType === 'splits') {
        if (forceReload || trainingDataStore.shouldReloadSplits) {
          console.log('[SmartDataLoader] 加载拆分数据')
          loadPromises.push(trainingDataStore.loadSplits(forceReload))
        }
      }
      
      if (dataType === 'all' || dataType === 'models') {
        if (forceReload || trainingDataStore.shouldReloadModels) {
          console.log('[SmartDataLoader] 加载模型数据')
          loadPromises.push(trainingDataStore.loadModels(forceReload))
        }
      }
      
      if (dataType === 'all' || dataType === 'config') {
        if (forceReload || projectChanged || !trainingDataStore.trainingConfig) {
          console.log('[SmartDataLoader] 加载训练配置')
          loadPromises.push(trainingDataStore.loadTrainingConfig(forceReload))
        }
      }
      
      if (loadPromises.length > 0) {
        await Promise.all(loadPromises)
        lastLoadTime.value = Date.now()
        console.log('[SmartDataLoader] 数据加载完成')
      } else {
        console.log('[SmartDataLoader] 使用缓存数据,无需重新加载')
      }
      
      return {
        splits: trainingDataStore.splits,
        models: trainingDataStore.models,
        config: trainingDataStore.trainingConfig
      }
      
    } catch (err) {
      console.error('[SmartDataLoader] 加载数据失败:', err)
      error.value = err
      throw err
    } finally {
      isLoading.value = false
    }
  }
  
  /**
   * 强制刷新数据
   */
  const refreshData = async () => {
    console.log('[SmartDataLoader] 强制刷新数据')
    return smartLoadData(true)
  }
  
  /**
   * 设置项目变化监听
   */
  const setupProjectWatcher = () => {
    // 监听项目变化
    unwatchProject = watch(
      () => projectStore.getProject,
      async (newProject, oldProject) => {
        if (!newProject) return
        
        // 获取项目标识
        const newId = newProject.id || newProject.project?.id
        const oldId = oldProject?.id || oldProject?.project?.id
        
        // 检查是否真的切换了项目
        if (oldId && newId !== oldId) {
          console.log('[SmartDataLoader] 项目切换:', oldId, '->', newId)
          // 项目切换时自动重新加载数据
          await smartLoadData()
        } else if (newProject.updatedAt !== oldProject?.updatedAt) {
          console.log('[SmartDataLoader] 项目更新')
          // 项目更新时根据需要重新加载
          await smartLoadData()
        }
      },
      { deep: true }
    )
  }
  
  /**
   * 检查数据是否过期
   * 现在数据永不过期,只在项目变化时才需要重新加载
   */
  const isDataStale = computed(() => {
    // 数据永不过期,只检查是否加载过
    return !lastLoadTime.value
  })
  
  /**
   * 获取缓存的数据
   */
  const getCachedData = () => {
    return {
      splits: trainingDataStore.splits,
      models: trainingDataStore.models,
      config: trainingDataStore.trainingConfig,
      completedModels: trainingDataStore.completedModels
    }
  }
  
  // 组件挂载时设置监听
  onMounted(() => {
    setupProjectWatcher()
  })
  
  // 组件卸载时清理
  onBeforeUnmount(() => {
    if (unwatchProject) {
      unwatchProject()
    }
  })
  
  return {
    // 状态
    isLoading,
    error,
    isDataStale,
    
    // 方法
    smartLoadData,
    refreshData,
    getCachedData,
    
    // 直接访问Store数据
    splits: computed(() => trainingDataStore.splits),
    models: computed(() => trainingDataStore.models),
    completedModels: computed(() => trainingDataStore.completedModels),
    config: computed(() => trainingDataStore.trainingConfig),
    
    // Store引用
    trainingDataStore
  }
}