Skip to content

Commit

Permalink
fix: 重写抽离store中的fetch方法并导出
Browse files Browse the repository at this point in the history
  • Loading branch information
Ximu-Luya committed Mar 22, 2024
1 parent b3ec8e4 commit 54a3665
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions stores/latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export const useLatestLogs = defineStore('latest_logs', () => {
const transactionMap = ref<Map<string, Log>>(new Map());

const startGetData = async () => {
await fetch();

// 每隔 10 秒重新获取数据
setTimeout(startGetData, 10 * 1000);
};

const fetch = async () => {
const resp = await $fetch(`/api/product`, { retry: 3 });
logs.value = resp.latest.map((log) => ({ ...log, uploadedAt: new Date(log.uploadedAt) }));
// 将数据转换为 Map 方便查询
Expand All @@ -13,16 +20,14 @@ export const useLatestLogs = defineStore('latest_logs', () => {
const key = `${log.name}-from${log.sourceCity}to${log.targetCity}`;
transactionMap.value.set(key, log);
});

// 每隔 10 秒重新获取数据
setTimeout(startGetData, 10 * 1000);
};
}

return {
logs,
getLatestLog(sourceCity: string, productName: string, targetCity: string) {
return transactionMap.value.get(`${productName}-from${sourceCity}to${targetCity}`);
},
fetch,
startGetData
};
});

0 comments on commit 54a3665

Please sign in to comment.