/**
|
* 智能数据加载器
|
* 通过检测项目变化来决定是否重新加载数据
|
*/
|
|
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
|
}
|
}
|