refactor(dashboard): useDashboardData 提取 SECTION_FETCHERS 模块级单例
代码评审建议:loadAll 与 refresh 各有一份相同的 7 项 fetcher 映射, 且 refresh 每次调用都重建对象。提取为模块级单例后: - 消除重复(单一信源) - 节省每次 refresh 的对象分配 - loadAll 用 Object.entries 驱动而非手写 7 行
This commit is contained in:
parent
ea62b609af
commit
3676fc977e
@ -1,6 +1,17 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import { dashboardApi } from '@/utils/api'
|
||||
|
||||
// section 名 → fetcher 映射,模块级单例,loadAll 与 refresh 复用
|
||||
const SECTION_FETCHERS = {
|
||||
today: dashboardApi.getTodayOverview,
|
||||
curve: dashboardApi.get7DayIncomeCurve,
|
||||
exhibition: dashboardApi.getExhibitionSummary,
|
||||
likeIncome: dashboardApi.getLikeIncomeByLevel,
|
||||
topAssets: dashboardApi.getTopAssets,
|
||||
levels: dashboardApi.getLevelDistribution,
|
||||
upgrades: dashboardApi.getUpgradeProgress,
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据看板聚合 composable
|
||||
* - 7 个接口并发(Promise.allSettled,单失败不阻塞)
|
||||
@ -69,15 +80,11 @@ export function useDashboardData({ starId = null } = {}) {
|
||||
if (!force && Date.now() - lastFetched.value < STALE_MS) return
|
||||
loading.value.overall = true
|
||||
try {
|
||||
await Promise.allSettled([
|
||||
loadSection('today', dashboardApi.getTodayOverview),
|
||||
loadSection('curve', dashboardApi.get7DayIncomeCurve),
|
||||
loadSection('exhibition', dashboardApi.getExhibitionSummary),
|
||||
loadSection('likeIncome', dashboardApi.getLikeIncomeByLevel),
|
||||
loadSection('topAssets', dashboardApi.getTopAssets),
|
||||
loadSection('levels', dashboardApi.getLevelDistribution),
|
||||
loadSection('upgrades', dashboardApi.getUpgradeProgress),
|
||||
])
|
||||
await Promise.allSettled(
|
||||
Object.entries(SECTION_FETCHERS).map(([section, fetcher]) =>
|
||||
loadSection(section, fetcher)
|
||||
)
|
||||
)
|
||||
lastFetched.value = Date.now()
|
||||
} finally {
|
||||
loading.value.overall = false
|
||||
@ -87,17 +94,9 @@ export function useDashboardData({ starId = null } = {}) {
|
||||
// 局部刷新:refresh('curve') 只刷一个;refresh() 全量 cache-aware;refresh(true) 全量强制
|
||||
async function refresh(section, force = false) {
|
||||
if (section) {
|
||||
const fetcherMap = {
|
||||
today: dashboardApi.getTodayOverview,
|
||||
curve: dashboardApi.get7DayIncomeCurve,
|
||||
exhibition: dashboardApi.getExhibitionSummary,
|
||||
likeIncome: dashboardApi.getLikeIncomeByLevel,
|
||||
topAssets: dashboardApi.getTopAssets,
|
||||
levels: dashboardApi.getLevelDistribution,
|
||||
upgrades: dashboardApi.getUpgradeProgress,
|
||||
}
|
||||
if (!fetcherMap[section]) return
|
||||
return loadSection(section, fetcherMap[section])
|
||||
const fetcher = SECTION_FETCHERS[section]
|
||||
if (!fetcher) return
|
||||
return loadSection(section, fetcher)
|
||||
}
|
||||
return loadAll(force)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user