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 { ref, computed } from 'vue'
|
||||||
import { dashboardApi } from '@/utils/api'
|
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
|
* 数据看板聚合 composable
|
||||||
* - 7 个接口并发(Promise.allSettled,单失败不阻塞)
|
* - 7 个接口并发(Promise.allSettled,单失败不阻塞)
|
||||||
@ -69,15 +80,11 @@ export function useDashboardData({ starId = null } = {}) {
|
|||||||
if (!force && Date.now() - lastFetched.value < STALE_MS) return
|
if (!force && Date.now() - lastFetched.value < STALE_MS) return
|
||||||
loading.value.overall = true
|
loading.value.overall = true
|
||||||
try {
|
try {
|
||||||
await Promise.allSettled([
|
await Promise.allSettled(
|
||||||
loadSection('today', dashboardApi.getTodayOverview),
|
Object.entries(SECTION_FETCHERS).map(([section, fetcher]) =>
|
||||||
loadSection('curve', dashboardApi.get7DayIncomeCurve),
|
loadSection(section, fetcher)
|
||||||
loadSection('exhibition', dashboardApi.getExhibitionSummary),
|
)
|
||||||
loadSection('likeIncome', dashboardApi.getLikeIncomeByLevel),
|
)
|
||||||
loadSection('topAssets', dashboardApi.getTopAssets),
|
|
||||||
loadSection('levels', dashboardApi.getLevelDistribution),
|
|
||||||
loadSection('upgrades', dashboardApi.getUpgradeProgress),
|
|
||||||
])
|
|
||||||
lastFetched.value = Date.now()
|
lastFetched.value = Date.now()
|
||||||
} finally {
|
} finally {
|
||||||
loading.value.overall = false
|
loading.value.overall = false
|
||||||
@ -87,17 +94,9 @@ export function useDashboardData({ starId = null } = {}) {
|
|||||||
// 局部刷新:refresh('curve') 只刷一个;refresh() 全量 cache-aware;refresh(true) 全量强制
|
// 局部刷新:refresh('curve') 只刷一个;refresh() 全量 cache-aware;refresh(true) 全量强制
|
||||||
async function refresh(section, force = false) {
|
async function refresh(section, force = false) {
|
||||||
if (section) {
|
if (section) {
|
||||||
const fetcherMap = {
|
const fetcher = SECTION_FETCHERS[section]
|
||||||
today: dashboardApi.getTodayOverview,
|
if (!fetcher) return
|
||||||
curve: dashboardApi.get7DayIncomeCurve,
|
return loadSection(section, fetcher)
|
||||||
exhibition: dashboardApi.getExhibitionSummary,
|
|
||||||
likeIncome: dashboardApi.getLikeIncomeByLevel,
|
|
||||||
topAssets: dashboardApi.getTopAssets,
|
|
||||||
levels: dashboardApi.getLevelDistribution,
|
|
||||||
upgrades: dashboardApi.getUpgradeProgress,
|
|
||||||
}
|
|
||||||
if (!fetcherMap[section]) return
|
|
||||||
return loadSection(section, fetcherMap[section])
|
|
||||||
}
|
}
|
||||||
return loadAll(force)
|
return loadAll(force)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user